Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

96 lignes
2.0KB

  1. #include "mov-writer.h"
  2. #include "mov-format.h"
  3. #include "mpeg4-aac.h"
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. extern "C" const struct mov_buffer_t* mov_file_buffer(void);
  9. static uint8_t s_buffer[2 * 1024 * 1024];
  10. static uint8_t s_extra_data[64 * 1024];
  11. struct mov_adts_test_t
  12. {
  13. mov_writer_t* mov;
  14. struct mpeg4_aac_t aac;
  15. int track;
  16. uint32_t pts, dts;
  17. const uint8_t* ptr;
  18. int vcl;
  19. };
  20. static uint8_t* file_read(const char* file, long* size)
  21. {
  22. FILE* fp = fopen(file, "rb");
  23. if (fp)
  24. {
  25. fseek(fp, 0, SEEK_END);
  26. *size = ftell(fp);
  27. fseek(fp, 0, SEEK_SET);
  28. uint8_t* ptr = (uint8_t*)malloc(*size);
  29. fread(ptr, 1, *size, fp);
  30. fclose(fp);
  31. return ptr;
  32. }
  33. return NULL;
  34. }
  35. static void adts_reader(struct mov_adts_test_t* ctx, const uint8_t* ptr, size_t bytes)
  36. {
  37. int64_t pts = 0;
  38. int64_t samples = 0;
  39. while(ptr && bytes > 7)
  40. {
  41. int n = mpeg4_aac_adts_frame_length(ptr, bytes);
  42. if (n < 0)
  43. break;
  44. if (n > bytes)
  45. break;
  46. if (-1 == ctx->track)
  47. {
  48. uint8_t asc[16];
  49. assert(7 == mpeg4_aac_adts_load(ptr, bytes, &ctx->aac));
  50. int len = mpeg4_aac_audio_specific_config_save(&ctx->aac, asc, sizeof(asc));
  51. assert(len > 0 && len <= sizeof(asc));
  52. ctx->track = mov_writer_add_audio(ctx->mov, MOV_OBJECT_AAC, ctx->aac.channels, 16, ctx->aac.sampling_frequency, asc, len);
  53. assert(ctx->track >= 0);
  54. assert(ctx->aac.sampling_frequency > 0);
  55. }
  56. assert(0 == mov_writer_write(ctx->mov, ctx->track, ptr + 7, n - 7, pts, pts, 0));
  57. ptr += n;
  58. bytes -= n;
  59. samples += 1024;
  60. pts = samples * 1000 / ctx->aac.sampling_frequency;
  61. }
  62. }
  63. void mov_writer_adts_test(const char* file)
  64. {
  65. struct mov_adts_test_t ctx;
  66. memset(&ctx, 0, sizeof(ctx));
  67. ctx.track = -1;
  68. long bytes = 0;
  69. uint8_t* ptr = file_read(file, &bytes);
  70. if (NULL == ptr) return;
  71. ctx.ptr = ptr;
  72. FILE* fp = fopen("adts.mp4", "wb+");
  73. ctx.mov = mov_writer_create(mov_file_buffer(), fp, 0);
  74. adts_reader(&ctx, ptr, bytes);
  75. mov_writer_destroy(ctx.mov);
  76. fclose(fp);
  77. free(ptr);
  78. }