You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.5KB

  1. #include "mov-internal.h"
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. // 8.7.3.2 Sample Size Box (p57)
  7. int mov_read_stsz(struct mov_t* mov, const struct mov_box_t* box)
  8. {
  9. uint32_t i = 0, sample_size, sample_count;
  10. struct mov_track_t* track = mov->track;
  11. mov_buffer_r8(&mov->io); /* version */
  12. mov_buffer_r24(&mov->io); /* flags */
  13. sample_size = mov_buffer_r32(&mov->io);
  14. sample_count = mov_buffer_r32(&mov->io);
  15. assert(0 == track->sample_count && NULL == track->samples); // duplicated STSZ atom
  16. if (track->sample_count < sample_count)
  17. {
  18. void* p = realloc(track->samples, sizeof(struct mov_sample_t) * (sample_count + 1));
  19. if (NULL == p) return -ENOMEM;
  20. track->samples = (struct mov_sample_t*)p;
  21. memset(track->samples, 0, sizeof(struct mov_sample_t) * (sample_count + 1));
  22. }
  23. track->sample_count = sample_count;
  24. if (0 == sample_size)
  25. {
  26. for (i = 0; i < sample_count; i++)
  27. track->samples[i].bytes = mov_buffer_r32(&mov->io); // uint32_t entry_size
  28. }
  29. else
  30. {
  31. for (i = 0; i < sample_count; i++)
  32. track->samples[i].bytes = sample_size;
  33. }
  34. (void)box;
  35. return mov_buffer_error(&mov->io);
  36. }
  37. // 8.7.3.3 Compact Sample Size Box (p57)
  38. int mov_read_stz2(struct mov_t* mov, const struct mov_box_t* box)
  39. {
  40. uint32_t i, v, field_size, sample_count;
  41. struct mov_track_t* track = mov->track;
  42. mov_buffer_r8(&mov->io); /* version */
  43. mov_buffer_r24(&mov->io); /* flags */
  44. // unsigned int(24) reserved = 0;
  45. mov_buffer_r24(&mov->io); /* reserved */
  46. field_size = mov_buffer_r8(&mov->io);
  47. sample_count = mov_buffer_r32(&mov->io);
  48. assert(4 == field_size || 8 == field_size || 16 == field_size);
  49. assert(0 == track->sample_count && NULL == track->samples); // duplicated STSZ atom
  50. if (track->sample_count < sample_count)
  51. {
  52. void* p = realloc(track->samples, sizeof(struct mov_sample_t) * (sample_count + 1));
  53. if (NULL == p) return -ENOMEM;
  54. track->samples = (struct mov_sample_t*)p;
  55. memset(track->samples, 0, sizeof(struct mov_sample_t) * (sample_count + 1));
  56. }
  57. track->sample_count = sample_count;
  58. if (4 == field_size)
  59. {
  60. for (i = 0; i < sample_count/2; i++)
  61. {
  62. v = mov_buffer_r8(&mov->io);
  63. track->samples[i * 2].bytes = (v >> 4) & 0x0F;
  64. track->samples[i * 2 + 1].bytes = v & 0x0F;
  65. }
  66. if (sample_count % 2)
  67. {
  68. v = mov_buffer_r8(&mov->io);
  69. track->samples[i * 2].bytes = (v >> 4) & 0x0F;
  70. }
  71. }
  72. else if (8 == field_size)
  73. {
  74. for (i = 0; i < sample_count; i++)
  75. track->samples[i].bytes = mov_buffer_r8(&mov->io);
  76. }
  77. else if (16 == field_size)
  78. {
  79. for (i = 0; i < sample_count; i++)
  80. track->samples[i].bytes = mov_buffer_r16(&mov->io);
  81. }
  82. else
  83. {
  84. i = 0;
  85. assert(0);
  86. }
  87. (void)box;
  88. return mov_buffer_error(&mov->io);
  89. }
  90. size_t mov_write_stsz(const struct mov_t* mov)
  91. {
  92. uint32_t size, i;
  93. const struct mov_track_t* track = mov->track;
  94. for(i = 1; i < track->sample_count; i++)
  95. {
  96. if(track->samples[i].bytes != track->samples[i-1].bytes)
  97. break;
  98. }
  99. size = 12/* full box */ + 8 + (i < track->sample_count ? 4 * track->sample_count : 0);
  100. mov_buffer_w32(&mov->io, size); /* size */
  101. mov_buffer_write(&mov->io, "stsz", 4);
  102. mov_buffer_w32(&mov->io, 0); /* version & flags */
  103. if(i < track->sample_count)
  104. {
  105. mov_buffer_w32(&mov->io, 0);
  106. mov_buffer_w32(&mov->io, track->sample_count);
  107. for(i = 0; i < track->sample_count; i++)
  108. mov_buffer_w32(&mov->io, track->samples[i].bytes);
  109. }
  110. else
  111. {
  112. mov_buffer_w32(&mov->io, track->sample_count < 1 ? 0 : track->samples[0].bytes);
  113. mov_buffer_w32(&mov->io, track->sample_count);
  114. }
  115. return size;
  116. }