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.

пре 8 месеци
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #include "mov-internal.h"
  2. #include <assert.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. static int mp4_read_tag(struct mov_t* mov, uint64_t bytes);
  6. // ISO/IEC 14496-1:2010(E)
  7. // 7.2.2 Common data structures
  8. // Table-1 List of Class Tags for Descriptors (p31)
  9. enum {
  10. ISO_ObjectDescrTag = 0x01,
  11. ISO_InitialObjectDescrTag = 0x02,
  12. ISO_ESDescrTag = 0x03,
  13. ISO_DecoderConfigDescrTag = 0x04,
  14. ISO_DecSpecificInfoTag = 0x05,
  15. ISO_SLConfigDescrTag = 0x06,
  16. ISO_ContentIdentDescrTag = 0x07,
  17. ISO_SupplContentIdentDescrTag = 0x08,
  18. ISO_IPI_DescrPointerTag = 0x09,
  19. ISO_IPMP_DescrPointerTag = 0x0A,
  20. ISO_IPMP_DescrTag = 0x0B,
  21. ISO_QoS_DescrTag = 0x0C,
  22. ISO_RegistrationDescrTag = 0x0D,
  23. ISO_ES_ID_IncTag = 0x0E,
  24. ISO_ES_ID_RefTag = 0x0F,
  25. ISO_MP4_IOD_Tag = 0x10,
  26. ISO_MP4_OD_Tag = 0x11,
  27. };
  28. // ISO/IEC 14496-1:2010(E)
  29. // 7.2.2.3 BaseCommand
  30. // Table-2 List of Class Tags for Commands (p33)
  31. enum {
  32. ISO_ObjectDescrUpdateTag = 0x01,
  33. ISO_ObjectDescrRemoveTag = 0x02,
  34. ISO_ES_DescrUpdateTag = 0x03,
  35. ISO_ES_DescrRemoveTag = 0x04,
  36. ISO_IPMP_DescrUpdateTag = 0x05,
  37. ISO_IPMP_DescrRemoveTag = 0x06,
  38. ISO_ES_DescrRemoveRefTag = 0x07,
  39. ISO_ObjectDescrExecuteTag = 0x08,
  40. ISO_User_Private = 0xC0,
  41. };
  42. // ISO/IEC 14496-1:2010(E) 7.2.2.2 BaseDescriptor (p32)
  43. // ISO/IEC 14496-1:2010(E) 8.3.3 Expandable classes (p116)
  44. /*
  45. abstract aligned(8) expandable(2^28-1) class BaseDescriptor : bit(8) tag=0 {
  46. // empty. To be filled by classes extending this class.
  47. }
  48. int sizeOfInstance = 0;
  49. bit(1) nextByte;
  50. bit(7) sizeOfInstance;
  51. while(nextByte) {
  52. bit(1) nextByte;
  53. bit(7) sizeByte;
  54. sizeOfInstance = sizeOfInstance<<7 | sizeByte;
  55. }
  56. */
  57. static int mov_read_base_descr(struct mov_t* mov, int bytes, int* tag, int* len)
  58. {
  59. int i;
  60. uint32_t c;
  61. *tag = mov_buffer_r8(&mov->io);
  62. *len = 0;
  63. c = 0x80;
  64. for (i = 0; i < 4 && i + 1 < bytes && 0 != (c & 0x80); i++)
  65. {
  66. c = mov_buffer_r8(&mov->io);
  67. *len = (*len << 7) | (c & 0x7F);
  68. //if (0 == (c & 0x80))
  69. // break;
  70. }
  71. return 1 + i;
  72. }
  73. static uint32_t mov_write_base_descr(const struct mov_t* mov, uint8_t tag, uint32_t len)
  74. {
  75. mov_buffer_w8(&mov->io, tag);
  76. mov_buffer_w8(&mov->io, (uint8_t)(0x80 | (len >> 21)));
  77. mov_buffer_w8(&mov->io, (uint8_t)(0x80 | (len >> 14)));
  78. mov_buffer_w8(&mov->io, (uint8_t)(0x80 | (len >> 7)));
  79. mov_buffer_w8(&mov->io, (uint8_t)(0x7F & len));
  80. return 5;
  81. }
  82. // ISO/IEC 14496-1:2010(E) 7.2.6.5 ES_Descriptor (p47)
  83. /*
  84. class ES_Descriptor extends BaseDescriptor : bit(8) tag=ES_DescrTag {
  85. bit(16) ES_ID;
  86. bit(1) streamDependenceFlag;
  87. bit(1) URL_Flag;
  88. bit(1) OCRstreamFlag;
  89. bit(5) streamPriority;
  90. if (streamDependenceFlag)
  91. bit(16) dependsOn_ES_ID;
  92. if (URL_Flag) {
  93. bit(8) URLlength;
  94. bit(8) URLstring[URLlength];
  95. }
  96. if (OCRstreamFlag)
  97. bit(16) OCR_ES_Id;
  98. DecoderConfigDescriptor decConfigDescr;
  99. if (ODProfileLevelIndication==0x01) //no SL extension.
  100. {
  101. SLConfigDescriptor slConfigDescr;
  102. }
  103. else // SL extension is possible.
  104. {
  105. SLConfigDescriptor slConfigDescr;
  106. }
  107. IPI_DescrPointer ipiPtr[0 .. 1];
  108. IP_IdentificationDataSet ipIDS[0 .. 255];
  109. IPMP_DescriptorPointer ipmpDescrPtr[0 .. 255];
  110. LanguageDescriptor langDescr[0 .. 255];
  111. QoS_Descriptor qosDescr[0 .. 1];
  112. RegistrationDescriptor regDescr[0 .. 1];
  113. ExtensionDescriptor extDescr[0 .. 255];
  114. }
  115. */
  116. static int mp4_read_es_descriptor(struct mov_t* mov, uint64_t bytes)
  117. {
  118. uint64_t p1, p2;
  119. p1 = mov_buffer_tell(&mov->io);
  120. /*uint32_t ES_ID = */mov_buffer_r16(&mov->io);
  121. uint32_t flags = mov_buffer_r8(&mov->io);
  122. if (flags & 0x80) //streamDependenceFlag
  123. mov_buffer_r16(&mov->io);
  124. if (flags & 0x40) { //URL_Flag
  125. uint32_t n = mov_buffer_r8(&mov->io);
  126. mov_buffer_skip(&mov->io, n);
  127. }
  128. if (flags & 0x20) //OCRstreamFlag
  129. mov_buffer_r16(&mov->io);
  130. p2 = mov_buffer_tell(&mov->io);
  131. return mp4_read_tag(mov, bytes - (p2 - p1));
  132. }
  133. // ISO/IEC 14496-1:2010(E) 7.2.6.7 DecoderSpecificInfo (p51)
  134. /*
  135. abstract class DecoderSpecificInfo extends BaseDescriptor : bit(8)
  136. tag=DecSpecificInfoTag
  137. {
  138. // empty. To be filled by classes extending this class.
  139. }
  140. */
  141. static int mp4_read_decoder_specific_info(struct mov_t* mov, int len)
  142. {
  143. struct mov_track_t* track = mov->track;
  144. struct mov_sample_entry_t* entry = track->stsd.current;
  145. if (entry->extra_data_size < len)
  146. {
  147. void* p = realloc(entry->extra_data, len);
  148. if (NULL == p) return -ENOMEM;
  149. entry->extra_data = p;
  150. }
  151. mov_buffer_read(&mov->io, entry->extra_data, len);
  152. entry->extra_data_size = len;
  153. return mov_buffer_error(&mov->io);
  154. }
  155. static int mp4_write_decoder_specific_info(const struct mov_t* mov)
  156. {
  157. const struct mov_sample_entry_t* entry = mov->track->stsd.current;
  158. mov_write_base_descr(mov, ISO_DecSpecificInfoTag, entry->extra_data_size);
  159. mov_buffer_write(&mov->io, entry->extra_data, entry->extra_data_size);
  160. return entry->extra_data_size;
  161. }
  162. // ISO/IEC 14496-1:2010(E) 7.2.6.6 DecoderConfigDescriptor (p48)
  163. /*
  164. class DecoderConfigDescriptor extends BaseDescriptor : bit(8) tag=DecoderConfigDescrTag {
  165. bit(8) objectTypeIndication;
  166. bit(6) streamType;
  167. bit(1) upStream;
  168. const bit(1) reserved=1;
  169. bit(24) bufferSizeDB;
  170. bit(32) maxBitrate;
  171. bit(32) avgBitrate;
  172. DecoderSpecificInfo decSpecificInfo[0 .. 1];
  173. profileLevelIndicationIndexDescriptor profileLevelIndicationIndexDescr[0..255];
  174. }
  175. */
  176. static int mp4_read_decoder_config_descriptor(struct mov_t* mov, int len)
  177. {
  178. struct mov_sample_entry_t* entry = mov->track->stsd.current;
  179. entry->object_type_indication = (uint8_t)mov_buffer_r8(&mov->io); /* objectTypeIndication */
  180. entry->stream_type = (uint8_t)mov_buffer_r8(&mov->io) >> 2; /* stream type */
  181. /*uint32_t bufferSizeDB = */mov_buffer_r24(&mov->io); /* buffer size db */
  182. /*uint32_t max_rate = */mov_buffer_r32(&mov->io); /* max bit-rate */
  183. /*uint32_t bit_rate = */mov_buffer_r32(&mov->io); /* avg bit-rate */
  184. return mp4_read_tag(mov, (uint64_t)len - 13); // mp4_read_decoder_specific_info
  185. }
  186. static int mp4_write_decoder_config_descriptor(const struct mov_t* mov)
  187. {
  188. const struct mov_sample_entry_t* entry = mov->track->stsd.current;
  189. int size = 13 + (entry->extra_data_size > 0 ? entry->extra_data_size + 5 : 0);
  190. mov_write_base_descr(mov, ISO_DecoderConfigDescrTag, size);
  191. mov_buffer_w8(&mov->io, entry->object_type_indication);
  192. mov_buffer_w8(&mov->io, 0x01/*reserved*/ | (entry->stream_type << 2));
  193. mov_buffer_w24(&mov->io, 0); /* buffer size db */
  194. mov_buffer_w32(&mov->io, 88360); /* max bit-rate */
  195. mov_buffer_w32(&mov->io, 88360); /* avg bit-rate */
  196. if (entry->extra_data_size > 0)
  197. mp4_write_decoder_specific_info(mov);
  198. return size;
  199. }
  200. // ISO/IEC 14496-1:2010(E) 7.3.2.3 SL Packet Header Configuration (p92)
  201. /*
  202. class SLConfigDescriptor extends BaseDescriptor : bit(8) tag=SLConfigDescrTag {
  203. bit(8) predefined;
  204. if (predefined==0) {
  205. bit(1) useAccessUnitStartFlag;
  206. bit(1) useAccessUnitEndFlag;
  207. bit(1) useRandomAccessPointFlag;
  208. bit(1) hasRandomAccessUnitsOnlyFlag;
  209. bit(1) usePaddingFlag;
  210. bit(1) useTimeStampsFlag;
  211. bit(1) useIdleFlag;
  212. bit(1) durationFlag;
  213. bit(32) timeStampResolution;
  214. bit(32) OCRResolution;
  215. bit(8) timeStampLength; // must be 64
  216. bit(8) OCRLength; // must be 64
  217. bit(8) AU_Length; // must be 32
  218. bit(8) instantBitrateLength;
  219. bit(4) degradationPriorityLength;
  220. bit(5) AU_seqNumLength; // must be 16
  221. bit(5) packetSeqNumLength; // must be 16
  222. bit(2) reserved=0b11;
  223. }
  224. if (durationFlag) {
  225. bit(32) timeScale;
  226. bit(16) accessUnitDuration;
  227. bit(16) compositionUnitDuration;
  228. }
  229. if (!useTimeStampsFlag) {
  230. bit(timeStampLength) startDecodingTimeStamp;
  231. bit(timeStampLength) startCompositionTimeStamp;
  232. }
  233. }
  234. class ExtendedSLConfigDescriptor extends SLConfigDescriptor : bit(8)
  235. tag=ExtSLConfigDescrTag {
  236. SLExtensionDescriptor slextDescr[1..255];
  237. }
  238. */
  239. static int mp4_read_sl_config_descriptor(struct mov_t* mov)
  240. {
  241. int flags = 0;
  242. int predefined = mov_buffer_r8(&mov->io);
  243. if (0 == predefined)
  244. {
  245. flags = mov_buffer_r8(&mov->io);
  246. /*uint32_t timeStampResolution = */mov_buffer_r32(&mov->io);
  247. /*uint32_t OCRResolution = */mov_buffer_r32(&mov->io);
  248. /*int timeStampLength = */mov_buffer_r8(&mov->io);
  249. /*int OCRLength = */mov_buffer_r8(&mov->io);
  250. /*int AU_Length = */mov_buffer_r8(&mov->io);
  251. /*int instantBitrateLength = */mov_buffer_r8(&mov->io);
  252. /*uint16_t length = */mov_buffer_r16(&mov->io);
  253. }
  254. else if (1 == predefined) // null SL packet header
  255. {
  256. flags = 0x00;
  257. //int TimeStampResolution = 1000;
  258. //int timeStampLength = 32;
  259. }
  260. else if (2 == predefined) // Reserved for use in MP4 files
  261. {
  262. // Table 14 Detailed predefined SLConfigDescriptor values (p93)
  263. flags = 0x04;
  264. }
  265. // durationFlag
  266. if (flags & 0x01)
  267. {
  268. /*uint32_t timeScale = */mov_buffer_r32(&mov->io);
  269. /*uint16_t accessUnitDuration = */mov_buffer_r16(&mov->io);
  270. /*uint16_t compositionUnitDuration = */mov_buffer_r16(&mov->io);
  271. }
  272. // useTimeStampsFlag
  273. if (0 == (flags & 0x04))
  274. {
  275. //uint64_t startDecodingTimeStamp = 0; // file_reader_rb8(timeStampLength / 8)
  276. //uint64_t startCompositionTimeStamp = 0; // file_reader_rb8(timeStampLength / 8)
  277. }
  278. return mov_buffer_error(&mov->io);
  279. }
  280. static size_t mp4_write_sl_config_descriptor(const struct mov_t* mov)
  281. {
  282. size_t size = 1;
  283. size += mov_write_base_descr(mov, ISO_SLConfigDescrTag, 1);
  284. mov_buffer_w8(&mov->io, 0x02);
  285. return size;
  286. }
  287. static int mp4_read_tag(struct mov_t* mov, uint64_t bytes)
  288. {
  289. int tag, len;
  290. uint64_t p1, p2, offset;
  291. for (offset = 0; offset < bytes; offset += len)
  292. {
  293. tag = len = 0;
  294. offset += mov_read_base_descr(mov, (int)(bytes - offset), &tag, &len);
  295. if (offset + len > bytes)
  296. break;
  297. p1 = mov_buffer_tell(&mov->io);
  298. switch (tag)
  299. {
  300. case ISO_ESDescrTag:
  301. mp4_read_es_descriptor(mov, len);
  302. break;
  303. case ISO_DecoderConfigDescrTag:
  304. mp4_read_decoder_config_descriptor(mov, len);
  305. break;
  306. case ISO_DecSpecificInfoTag:
  307. mp4_read_decoder_specific_info(mov, len);
  308. break;
  309. case ISO_SLConfigDescrTag:
  310. mp4_read_sl_config_descriptor(mov);
  311. break;
  312. default:
  313. break;
  314. }
  315. p2 = mov_buffer_tell(&mov->io);
  316. mov_buffer_skip(&mov->io, len - (p2 - p1));
  317. }
  318. return mov_buffer_error(&mov->io);
  319. }
  320. // ISO/IEC 14496-14:2003(E) 5.6 Sample Description Boxes (p15)
  321. int mov_read_esds(struct mov_t* mov, const struct mov_box_t* box)
  322. {
  323. mov_buffer_r8(&mov->io); /* version */
  324. mov_buffer_r24(&mov->io); /* flags */
  325. return mp4_read_tag(mov, box->size - 4);
  326. }
  327. static size_t mp4_write_es_descriptor(const struct mov_t* mov)
  328. {
  329. uint32_t size = 3; // mp4_write_decoder_config_descriptor
  330. const struct mov_sample_entry_t* entry = mov->track->stsd.current;
  331. size += 5 + 13 + (entry->extra_data_size > 0 ? entry->extra_data_size + 5 : 0); // mp4_write_decoder_config_descriptor
  332. size += 5 + 1; // mp4_write_sl_config_descriptor
  333. size += mov_write_base_descr(mov, ISO_ESDescrTag, size);
  334. mov_buffer_w16(&mov->io, (uint16_t)mov->track->tkhd.track_ID); // ES_ID
  335. mov_buffer_w8(&mov->io, 0x00); // flags (= no flags)
  336. mp4_write_decoder_config_descriptor(mov);
  337. mp4_write_sl_config_descriptor(mov);
  338. return size;
  339. }
  340. size_t mov_write_esds(const struct mov_t* mov)
  341. {
  342. size_t size;
  343. uint64_t offset;
  344. size = 12 /* full box */;
  345. offset = mov_buffer_tell(&mov->io);
  346. mov_buffer_w32(&mov->io, 0); /* size */
  347. mov_buffer_write(&mov->io, "esds", 4);
  348. mov_buffer_w32(&mov->io, 0); /* version & flags */
  349. size += mp4_write_es_descriptor(mov);
  350. mov_write_size(mov, offset, size); /* update size */
  351. return size;
  352. }