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.

2605 lines
62 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*******************************************************************************
  2. mp4_reader.c - A library for reading MPEG4.
  3. Copyright (C) 2007-2009 CodeShop B.V.
  4. http://www.code-shop.com
  5. For licensing see the LICENSE file
  6. ******************************************************************************/
  7. #ifdef HAVE_CONFIG_H
  8. #include "config.h"
  9. #endif
  10. #ifdef __cplusplus
  11. #define __STDC_FORMAT_MACROS // C++ should define this for PRIu64
  12. #define __STDC_LIMIT_MACROS // C++ should define this for UINT64_MAX
  13. #endif
  14. #include "mp4_reader.h"
  15. #include "mp4_io.h"
  16. #include <stdlib.h>
  17. #include <string.h>
  18. struct atom_t
  19. {
  20. uint32_t type_;
  21. uint32_t short_size_;
  22. uint64_t size_;
  23. unsigned char* start_;
  24. unsigned char* end_;
  25. };
  26. typedef struct atom_t atom_t;
  27. static void atom_print(mp4_context_t const* mp4_context, atom_t const* atom)
  28. {
  29. MP4_INFO("Atom(%c%c%c%c,%" PRIu64 ")\n",
  30. atom->type_ >> 24,
  31. atom->type_ >> 16,
  32. atom->type_ >> 8,
  33. atom->type_,
  34. atom->size_);
  35. }
  36. static unsigned char*
  37. atom_read_header(mp4_context_t const* mp4_context, unsigned char* buffer,
  38. atom_t* atom)
  39. {
  40. atom->start_ = buffer;
  41. atom->short_size_ = read_32(buffer);
  42. atom->type_ = read_32(buffer + 4);
  43. if(atom->short_size_ == 1)
  44. atom->size_ = read_64(buffer + 8);
  45. else
  46. atom->size_ = atom->short_size_;
  47. atom->end_ = atom->start_ + atom->size_;
  48. atom_print(mp4_context, atom);
  49. if(atom->size_ < ATOM_PREAMBLE_SIZE)
  50. {
  51. MP4_ERROR("%s", "Error: invalid atom size\n");
  52. return 0;
  53. }
  54. return buffer + ATOM_PREAMBLE_SIZE + (atom->short_size_ == 1 ? 8 : 0);
  55. }
  56. static struct unknown_atom_t* unknown_atom_add_atom(struct unknown_atom_t* parent, void* atom)
  57. {
  58. size_t size = read_32((const unsigned char*)atom);
  59. unknown_atom_t* unknown = unknown_atom_init();
  60. unknown->atom_ = malloc(size);
  61. memcpy(unknown->atom_, atom, size);
  62. {
  63. unknown_atom_t** adder = &parent;
  64. while(*adder != NULL)
  65. {
  66. adder = &(*adder)->next_;
  67. }
  68. *adder = unknown;
  69. }
  70. return parent;
  71. }
  72. extern int atom_reader(struct mp4_context_t const* mp4_context,
  73. struct atom_read_list_t* atom_read_list,
  74. unsigned int atom_read_list_size,
  75. void* parent,
  76. unsigned char* buffer, uint64_t size)
  77. {
  78. atom_t leaf_atom;
  79. unsigned char* buffer_start = buffer;
  80. while(buffer < buffer_start + size)
  81. {
  82. unsigned int i;
  83. buffer = atom_read_header(mp4_context, buffer, &leaf_atom);
  84. if(buffer == NULL)
  85. {
  86. return 0;
  87. }
  88. for(i = 0; i != atom_read_list_size; ++i)
  89. {
  90. if(leaf_atom.type_ == atom_read_list[i].type_)
  91. {
  92. break;
  93. }
  94. }
  95. if(i == atom_read_list_size)
  96. {
  97. // add to unkown chunks
  98. (*(unknown_atom_t**)parent) =
  99. unknown_atom_add_atom(*(unknown_atom_t**)(parent), buffer - ATOM_PREAMBLE_SIZE);
  100. }
  101. else
  102. {
  103. void* child =
  104. atom_read_list[i].reader_(mp4_context, parent, buffer,
  105. leaf_atom.size_ - ATOM_PREAMBLE_SIZE);
  106. if(!child)
  107. break;
  108. if(!atom_read_list[i].destination_(mp4_context, parent, child))
  109. break;
  110. }
  111. buffer = leaf_atom.end_;
  112. }
  113. if(buffer < buffer_start + size)
  114. {
  115. return 0;
  116. }
  117. return 1;
  118. }
  119. static void* ctts_read(mp4_context_t const* UNUSED(mp4_context),
  120. void* UNUSED(parent),
  121. unsigned char* buffer, uint64_t size)
  122. {
  123. unsigned int i;
  124. ctts_t* atom;
  125. if(size < 8)
  126. return 0;
  127. atom = ctts_init();
  128. atom->version_ = read_8(buffer + 0);
  129. atom->flags_ = read_24(buffer + 1);
  130. atom->entries_ = read_32(buffer + 4);
  131. if(size < 8 + atom->entries_ * sizeof(ctts_table_t))
  132. return 0;
  133. buffer += 8;
  134. atom->table_ = (ctts_table_t*)(malloc(atom->entries_ * sizeof(ctts_table_t)));
  135. for(i = 0; i != atom->entries_; ++i)
  136. {
  137. atom->table_[i].sample_count_ = read_32(buffer + 0);
  138. atom->table_[i].sample_offset_ = read_32(buffer + 4);
  139. buffer += 8;
  140. }
  141. return atom;
  142. }
  143. static void* stco_read(mp4_context_t const* UNUSED(mp4_context),
  144. void* UNUSED(parent),
  145. unsigned char* buffer, uint64_t size)
  146. {
  147. unsigned int i;
  148. stco_t* atom;
  149. if(size < 8)
  150. return 0;
  151. atom = stco_init();
  152. atom->version_ = read_8(buffer + 0);
  153. atom->flags_ = read_24(buffer + 1);
  154. atom->entries_ = read_32(buffer + 4);
  155. buffer += 8;
  156. if(size < 8 + atom->entries_ * sizeof(uint32_t))
  157. return 0;
  158. atom->chunk_offsets_ = (uint64_t*)malloc(atom->entries_ * sizeof(uint64_t));
  159. for(i = 0; i != atom->entries_; ++i)
  160. {
  161. atom->chunk_offsets_[i] = read_32(buffer);
  162. buffer += 4;
  163. }
  164. return atom;
  165. }
  166. static void* co64_read(mp4_context_t const* UNUSED(mp4_context),
  167. void* UNUSED(parent),
  168. unsigned char* buffer, uint64_t size)
  169. {
  170. unsigned int i;
  171. stco_t* atom;
  172. if(size < 8)
  173. return 0;
  174. atom = stco_init();
  175. atom->version_ = read_8(buffer + 0);
  176. atom->flags_ = read_24(buffer + 1);
  177. atom->entries_ = read_32(buffer + 4);
  178. buffer += 8;
  179. if(size < 8 + atom->entries_ * sizeof(uint64_t))
  180. return 0;
  181. atom->chunk_offsets_ = (uint64_t*)malloc(atom->entries_ * sizeof(uint64_t));
  182. for(i = 0; i != atom->entries_; ++i)
  183. {
  184. atom->chunk_offsets_[i] = read_64(buffer);
  185. buffer += 8;
  186. }
  187. return atom;
  188. }
  189. static void* stsz_read(mp4_context_t const* mp4_context,
  190. void* UNUSED(parent),
  191. unsigned char* buffer, uint64_t size)
  192. {
  193. unsigned int i;
  194. stsz_t* atom;
  195. if(size < 12)
  196. {
  197. MP4_ERROR("%s", "Error: not enough bytes for stsz atom\n");
  198. return 0;
  199. }
  200. atom = stsz_init();
  201. atom->version_ = read_8(buffer + 0);
  202. atom->flags_ = read_24(buffer + 1);
  203. atom->sample_size_ = read_32(buffer + 4);
  204. atom->entries_ = read_32(buffer + 8);
  205. buffer += 12;
  206. // fix for clayton.mp4, it mistakenly says there is 1 entry
  207. // if(atom->sample_size_ && atom->entries_)
  208. // atom->entries_ = 0;
  209. if(!atom->sample_size_)
  210. {
  211. if(size < 12 + atom->entries_ * sizeof(uint32_t))
  212. {
  213. MP4_ERROR("%s", "Error: stsz.entries don't match with size\n");
  214. stsz_exit(atom);
  215. return 0;
  216. }
  217. atom->sample_sizes_ = (uint32_t*)malloc(atom->entries_ * sizeof(uint32_t));
  218. for(i = 0; i != atom->entries_; ++i)
  219. {
  220. atom->sample_sizes_[i] = read_32(buffer);
  221. buffer += 4;
  222. }
  223. }
  224. return atom;
  225. }
  226. static void* stsc_read(mp4_context_t const* UNUSED(mp4_context),
  227. void* UNUSED(parent),
  228. unsigned char* buffer, uint64_t size)
  229. {
  230. unsigned int i;
  231. stsc_t* atom;
  232. if(size < 8)
  233. return 0;
  234. atom = stsc_init();
  235. atom->version_ = read_8(buffer + 0);
  236. atom->flags_ = read_24(buffer + 1);
  237. atom->entries_ = read_32(buffer + 4);
  238. if(size < 8 + atom->entries_ * sizeof(stsc_table_t))
  239. return 0;
  240. buffer += 8;
  241. // reserve space for one extra entry as when splitting the video we may have to
  242. // split the first entry
  243. atom->table_ = (stsc_table_t*)(malloc((atom->entries_ + 1) * sizeof(stsc_table_t)));
  244. for(i = 0; i != atom->entries_; ++i)
  245. {
  246. atom->table_[i].chunk_ = read_32(buffer + 0) - 1; // Note: we use zero based
  247. atom->table_[i].samples_ = read_32(buffer + 4);
  248. atom->table_[i].id_ = read_32(buffer + 8);
  249. buffer += 12;
  250. }
  251. return atom;
  252. }
  253. static void* stss_read(mp4_context_t const* UNUSED(mp4_context),
  254. void* UNUSED(parent),
  255. unsigned char* buffer, uint64_t size)
  256. {
  257. unsigned int i;
  258. stss_t* atom;
  259. if(size < 8)
  260. return 0;
  261. atom = stss_init();
  262. atom->version_ = read_8(buffer + 0);
  263. atom->flags_ = read_24(buffer + 1);
  264. atom->entries_ = read_32(buffer + 4);
  265. if(size < 8 + atom->entries_ * sizeof(uint32_t))
  266. return 0;
  267. buffer += 8;
  268. atom->sample_numbers_ = (uint32_t*)malloc(atom->entries_ * sizeof(uint32_t));
  269. for(i = 0; i != atom->entries_; ++i)
  270. {
  271. atom->sample_numbers_[i] = read_32(buffer);
  272. buffer += 4;
  273. }
  274. return atom;
  275. }
  276. static int mp4_read_desc_len(unsigned char** buffer)
  277. {
  278. uint32_t len = 0;
  279. unsigned int bytes = 0;
  280. for(;;)
  281. {
  282. unsigned int c = read_8(*buffer + bytes);
  283. len <<= 7;
  284. len |= (c & 0x7f);
  285. if(++bytes == 4)
  286. break;
  287. if(!(c & 0x80))
  288. break;
  289. }
  290. *buffer += bytes;
  291. return len;
  292. }
  293. static int esds_read(mp4_context_t const* mp4_context,
  294. sample_entry_t* sample_entry,
  295. unsigned char* buffer, uint64_t size)
  296. {
  297. unsigned int tag;
  298. unsigned int len;
  299. uint16_t stream_id;
  300. unsigned int stream_priority;
  301. unsigned int object_type_id;
  302. unsigned int stream_type;
  303. unsigned int buffer_size_db;
  304. if(size < 9)
  305. return 0;
  306. /* unsigned char version = */ read_8(buffer);
  307. /* unsigned int flags = */ read_24(buffer + 1);
  308. buffer += 4;
  309. // ES_DescrTag
  310. tag = read_8(buffer);
  311. buffer += 1;
  312. if(tag == MP4_ELEMENTARY_STREAM_DESCRIPTOR_TAG)
  313. {
  314. len = mp4_read_desc_len(&buffer);
  315. MP4_INFO("Elementary Stream Descriptor: len=%u\n", len);
  316. stream_id = read_16(buffer + 0);
  317. stream_priority = read_8(buffer + 2);
  318. buffer += 3;
  319. }
  320. else
  321. {
  322. MP4_INFO("Elementary Stream Descriptor: len=%u\n", 2);
  323. stream_id = read_16(buffer + 0);
  324. buffer += 2;
  325. }
  326. tag = read_8(buffer);
  327. buffer += 1;
  328. len = mp4_read_desc_len(&buffer);
  329. MP4_INFO("MPEG: tag=%u len=%u\n", tag, len);
  330. // Decoder config descr Tag
  331. if(tag != MP4_DECODER_CONFIG_DESCRIPTOR_TAG)
  332. {
  333. MP4_INFO("Decoder Config Descriptor: len=%u\n", len);
  334. return 0;
  335. }
  336. object_type_id = read_8(buffer);
  337. buffer += 1; // object_type_id
  338. stream_type = read_8(buffer);
  339. buffer += 1; // stream_type
  340. buffer_size_db = read_24(buffer);
  341. buffer += 3; // buffer_size_db
  342. sample_entry->max_bitrate_ = read_32(buffer);
  343. buffer += 4;
  344. sample_entry->avg_bitrate_ = read_32(buffer);
  345. buffer += 4; // avg_bitrate
  346. MP4_INFO("%s", "Decoder Configuration Descriptor:\n");
  347. MP4_INFO(" object_type_id=$%02x\n", object_type_id);
  348. MP4_INFO(" stream_type=%u\n", stream_type);
  349. MP4_INFO(" buffer_size_db=%u\n", buffer_size_db);
  350. MP4_INFO(" max_bitrate=%u\n", sample_entry->max_bitrate_);
  351. MP4_INFO(" avg_bitrate=%u\n", sample_entry->avg_bitrate_);
  352. switch(object_type_id)
  353. {
  354. case MP4_MPEG2AudioMain:
  355. case MP4_MPEG2AudioLowComplexity:
  356. case MP4_MPEG2AudioScaleableSamplingRate:
  357. case MP4_MPEG4Audio:
  358. sample_entry->wFormatTag = 0x00ff; // WAVE_FORMAT_RAW_AAC1
  359. break;
  360. case MP4_MPEG1Audio:
  361. case MP4_MPEG2AudioPart3:
  362. sample_entry->wFormatTag = 0x0055; // WAVE_FORMAT_MP3
  363. break;
  364. default:
  365. break;
  366. }
  367. if(!sample_entry->nAvgBytesPerSec)
  368. {
  369. unsigned int bitrate = sample_entry->avg_bitrate_;
  370. if(!bitrate)
  371. bitrate = sample_entry->max_bitrate_;
  372. sample_entry->nAvgBytesPerSec = bitrate / 8;
  373. }
  374. tag = read_8(buffer);
  375. buffer += 1;
  376. len = mp4_read_desc_len(&buffer);
  377. MP4_INFO("MPEG: tag=%u len=%u\n", tag, len);
  378. // Decoder specific descr Tag
  379. if(tag == MP4_DECODER_SPECIFIC_DESCRIPTOR_TAG)
  380. {
  381. MP4_INFO("Decoder Specific Info Descriptor: len=%u\n", len);
  382. sample_entry->codec_private_data_length_ = len;
  383. sample_entry->codec_private_data_ = buffer;
  384. }
  385. return 1;
  386. }
  387. static int
  388. stsd_parse_vide(mp4_context_t const* mp4_context,
  389. trak_t* UNUSED(trak),
  390. sample_entry_t* sample_entry)
  391. {
  392. unsigned char* buffer = sample_entry->buf_;
  393. unsigned char* buffer_start = buffer;
  394. uint64_t size = sample_entry->len_;
  395. unsigned int i;
  396. if(size < 78)
  397. {
  398. MP4_ERROR("%s", "invalid stsd video size\n");
  399. return 0;
  400. }
  401. // 'ovc1' is immediately followed by additional data and ends with the codec
  402. // private data, *not* by additional atoms.
  403. if(sample_entry->fourcc_ == FOURCC('o', 'v', 'c', '1'))
  404. {
  405. // TODO: get start of ovc1 codec private data (25 00 00 01 0F CB 6C 1A ..)
  406. sample_entry->codec_private_data_ = buffer + 190;
  407. sample_entry->codec_private_data_length_ = (unsigned int)(size - 190);
  408. return 1;
  409. }
  410. if(size >= 78 + ATOM_PREAMBLE_SIZE)
  411. {
  412. atom_t atom;
  413. buffer += 78;
  414. while(buffer < buffer_start + size - ATOM_PREAMBLE_SIZE)
  415. {
  416. buffer = atom_read_header(mp4_context, buffer, &atom);
  417. if(buffer == NULL)
  418. {
  419. return 0;
  420. }
  421. switch(atom.type_)
  422. {
  423. case FOURCC('a', 'v', 'c', 'C'):
  424. {
  425. unsigned int sequence_parameter_sets;
  426. unsigned int picture_parameter_sets;
  427. unsigned int configuration_version;
  428. unsigned int profile_indication;
  429. unsigned int profile_compatibility;
  430. unsigned int level_indication;
  431. sample_entry->codec_private_data_ = buffer;
  432. configuration_version = read_8(buffer + 0);
  433. profile_indication = read_8(buffer + 1);
  434. profile_compatibility = read_8(buffer + 2);
  435. level_indication = read_8(buffer + 3);
  436. sample_entry->nal_unit_length_ = (read_8(buffer + 4) & 3) + 1;
  437. sequence_parameter_sets = read_8(buffer + 5) & 0x1f;
  438. buffer += 6;
  439. for(i = 0; i != sequence_parameter_sets; ++i)
  440. {
  441. unsigned int sps_length = read_16(buffer);
  442. buffer += 2;
  443. sample_entry->sps_ = buffer;
  444. sample_entry->sps_length_ = sps_length;
  445. buffer += sps_length;
  446. }
  447. picture_parameter_sets = read_8(buffer);
  448. buffer += 1;
  449. for(i = 0; i != picture_parameter_sets; ++i)
  450. {
  451. unsigned int pps_length = read_16(buffer);
  452. buffer += 2;
  453. sample_entry->pps_ = buffer;
  454. sample_entry->pps_length_ = pps_length;
  455. buffer += pps_length;
  456. }
  457. sample_entry->codec_private_data_length_ =
  458. (unsigned int)(buffer - sample_entry->codec_private_data_);
  459. }
  460. break;
  461. case FOURCC('e', 's', 'd', 's'):
  462. if(!esds_read(mp4_context, sample_entry, buffer, atom.size_ - ATOM_PREAMBLE_SIZE))
  463. {
  464. return 0;
  465. }
  466. break;
  467. default:
  468. break;
  469. }
  470. buffer = atom.end_;
  471. }
  472. }
  473. return 1;
  474. }
  475. static int wave_read(mp4_context_t const* mp4_context,
  476. sample_entry_t* sample_entry,
  477. unsigned char* buffer, uint64_t size)
  478. {
  479. unsigned char* end = buffer + size;
  480. while(buffer < end)
  481. {
  482. atom_t atom;
  483. buffer = atom_read_header(mp4_context, buffer, &atom);
  484. if(buffer == NULL)
  485. {
  486. return 0;
  487. }
  488. switch(atom.type_)
  489. {
  490. case FOURCC('e', 's', 'd', 's'):
  491. if(!esds_read(mp4_context, sample_entry, buffer, atom.size_ - ATOM_PREAMBLE_SIZE))
  492. {
  493. return 0;
  494. }
  495. break;
  496. }
  497. buffer = atom.end_;
  498. }
  499. return 1;
  500. }
  501. static int
  502. stsd_parse_soun(mp4_context_t const* mp4_context,
  503. trak_t* UNUSED(trak),
  504. sample_entry_t* sample_entry)
  505. {
  506. unsigned char* buffer = sample_entry->buf_;
  507. unsigned char* buffer_start = buffer;
  508. uint64_t size = sample_entry->len_;
  509. unsigned int data_reference_index;
  510. unsigned int version;
  511. unsigned int revision;
  512. unsigned int vendor;
  513. unsigned int compression_id;
  514. unsigned int packet_size;
  515. if(sample_entry->len_ < 28)
  516. return 0;
  517. data_reference_index = read_16(buffer + 6);
  518. version = read_16(buffer + 8);
  519. revision = read_16(buffer + 10);
  520. vendor = read_32(buffer + 12);
  521. sample_entry->nChannels = read_16(buffer + 16);
  522. if(sample_entry->nChannels == 3)
  523. sample_entry->nChannels = 6;
  524. sample_entry->wBitsPerSample = read_16(buffer + 18);
  525. compression_id = read_16(buffer + 20);
  526. packet_size = read_16(buffer + 22);
  527. // samplerate = {timescale of media} << 16
  528. sample_entry->samplerate_hi_ = read_16(buffer + 24);
  529. sample_entry->samplerate_lo_ = read_16(buffer + 26);
  530. sample_entry->nSamplesPerSec = sample_entry->samplerate_hi_;
  531. MP4_INFO("%s", "Sample Entry:\n");
  532. MP4_INFO(" data_reference_index=%u\n", data_reference_index);
  533. MP4_INFO(" version=%u\n", version);
  534. MP4_INFO(" revision=%u\n", revision);
  535. MP4_INFO(" vendor=%08x\n", vendor);
  536. MP4_INFO(" channel_count=%u\n", sample_entry->nChannels);
  537. MP4_INFO(" sample_size=%u\n", sample_entry->wBitsPerSample);
  538. MP4_INFO(" compression_id=%u\n", compression_id);
  539. MP4_INFO(" packet_size=%u\n", packet_size);
  540. MP4_INFO(" samplerate_hi=%u\n", sample_entry->samplerate_hi_);
  541. MP4_INFO(" samplerate_lo=%u\n", sample_entry->samplerate_lo_);
  542. buffer += 28;
  543. // 'owma' is immediately followed by the codec private data, *not* by
  544. // additional atoms.
  545. if(sample_entry->fourcc_ == FOURCC('o', 'w', 'm', 'a'))
  546. {
  547. sample_entry->codec_private_data_ = buffer;
  548. sample_entry->codec_private_data_length_ = (unsigned int)(size - 28);
  549. // sample_entry->wFormatTag = read_16(buffer + 0);
  550. // ...
  551. return 1;
  552. }
  553. if(version >= 1)
  554. {
  555. unsigned int samples_per_packet;
  556. unsigned int bytes_per_packet;
  557. unsigned int bytes_per_frame;
  558. unsigned int bytes_per_sample;
  559. if(version == 1 && size < 28 + 16)
  560. {
  561. return 0;
  562. }
  563. if(version == 2 && size < 28 + 36)
  564. {
  565. return 0;
  566. }
  567. if(version > 2)
  568. {
  569. return 0;
  570. }
  571. samples_per_packet = read_32(buffer + 0);
  572. bytes_per_packet = read_32(buffer + 4);
  573. bytes_per_frame = read_32(buffer + 8);
  574. bytes_per_sample = read_32(buffer + 12);
  575. MP4_INFO(" samples_per_packet=%u\n", samples_per_packet);
  576. MP4_INFO(" bytes_per_packet=%u\n", bytes_per_packet);
  577. MP4_INFO(" bytes_per_frame=%u\n", bytes_per_frame);
  578. MP4_INFO(" bytes_per_sample=%u\n", bytes_per_sample);
  579. if(samples_per_packet > 0)
  580. {
  581. sample_entry->nAvgBytesPerSec =
  582. (sample_entry->nChannels * sample_entry->nSamplesPerSec *
  583. bytes_per_packet + samples_per_packet / 2) / samples_per_packet;
  584. sample_entry->nBlockAlign = (uint16_t)bytes_per_frame;
  585. }
  586. else
  587. {
  588. sample_entry->nAvgBytesPerSec =
  589. sample_entry->nChannels * sample_entry->nSamplesPerSec *
  590. sample_entry->wBitsPerSample / 8;
  591. }
  592. buffer += version == 1 ? 16 : 36;
  593. }
  594. if(buffer - buffer_start >= ATOM_PREAMBLE_SIZE)
  595. {
  596. atom_t atom;
  597. while(buffer < buffer_start + size - ATOM_PREAMBLE_SIZE)
  598. {
  599. buffer = atom_read_header(mp4_context, buffer, &atom);
  600. if(buffer == NULL)
  601. {
  602. return 0;
  603. }
  604. switch(atom.type_)
  605. {
  606. case FOURCC('w', 'a', 'v', 'e'):
  607. if(!wave_read(mp4_context, sample_entry, buffer, atom.size_ - ATOM_PREAMBLE_SIZE))
  608. {
  609. return 0;
  610. }
  611. break;
  612. case FOURCC('f', 'r', 'm', 'a'):
  613. break;
  614. case FOURCC('e', 's', 'd', 's'):
  615. if(!esds_read(mp4_context, sample_entry, buffer, atom.size_ - ATOM_PREAMBLE_SIZE))
  616. {
  617. return 0;
  618. }
  619. break;
  620. default:
  621. break;
  622. }
  623. buffer = atom.end_;
  624. }
  625. }
  626. return 1;
  627. }
  628. static int stsd_parse(mp4_context_t const* mp4_context,
  629. trak_t* trak, stsd_t* stsd)
  630. {
  631. unsigned int i;
  632. for(i = 0; i != stsd->entries_; ++i)
  633. {
  634. sample_entry_t* sample_entry = &stsd->sample_entries_[i];
  635. switch(trak->mdia_->hdlr_->handler_type_)
  636. {
  637. case FOURCC('v', 'i', 'd', 'e'):
  638. if(!stsd_parse_vide(mp4_context, trak, sample_entry))
  639. {
  640. return 0;
  641. }
  642. break;
  643. case FOURCC('s', 'o', 'u', 'n'):
  644. if(!stsd_parse_soun(mp4_context, trak, sample_entry))
  645. {
  646. return 0;
  647. }
  648. break;
  649. case FOURCC('h', 'i', 'n', 't'):
  650. default:
  651. return 1;
  652. }
  653. }
  654. return 1;
  655. }
  656. static void* stts_read(mp4_context_t const* UNUSED(mp4_context),
  657. void* UNUSED(parent),
  658. unsigned char* buffer, uint64_t size)
  659. {
  660. unsigned int i;
  661. stts_t* atom;
  662. if(size < 8)
  663. return 0;
  664. atom = stts_init();
  665. atom->version_ = read_8(buffer + 0);
  666. atom->flags_ = read_24(buffer + 1);
  667. atom->entries_ = read_32(buffer + 4);
  668. if(size < 8 + atom->entries_ * sizeof(stts_table_t))
  669. return 0;
  670. buffer += 8;
  671. atom->table_ = (stts_table_t*)(malloc(atom->entries_ * sizeof(stts_table_t)));
  672. for(i = 0; i != atom->entries_; ++i)
  673. {
  674. atom->table_[i].sample_count_ = read_32(buffer + 0);
  675. atom->table_[i].sample_duration_ = read_32(buffer + 4);
  676. buffer += 8;
  677. }
  678. return atom;
  679. }
  680. static void* stsd_read(mp4_context_t const* UNUSED(mp4_context),
  681. void* UNUSED(parent),
  682. unsigned char* buffer, uint64_t size)
  683. {
  684. unsigned int i;
  685. stsd_t* atom;
  686. if(size < 8)
  687. return 0;
  688. atom = stsd_init();
  689. atom->version_ = read_8(buffer + 0);
  690. atom->flags_ = read_24(buffer + 1);
  691. atom->entries_ = read_32(buffer + 4);
  692. buffer += 8;
  693. atom->sample_entries_ = (sample_entry_t*)(malloc(atom->entries_ * sizeof(sample_entry_t)));
  694. for(i = 0; i != atom->entries_; ++i)
  695. {
  696. unsigned int j;
  697. sample_entry_t* sample_entry = &atom->sample_entries_[i];
  698. sample_entry_init(sample_entry);
  699. sample_entry->len_ = read_32(buffer) - 8;
  700. sample_entry->fourcc_ = read_32(buffer + 4);
  701. sample_entry->buf_ = (unsigned char*)malloc(sample_entry->len_);
  702. buffer += 8;
  703. for(j = 0; j != sample_entry->len_; ++j)
  704. {
  705. sample_entry->buf_[j] = (unsigned char)read_8(buffer + j);
  706. }
  707. buffer += j;
  708. }
  709. return atom;
  710. }
  711. static int stbl_add_stsd(mp4_context_t const* UNUSED(mp4_context),
  712. void* parent, void* child)
  713. {
  714. stbl_t* stbl = (stbl_t*)parent;
  715. stbl->stsd_ = (stsd_t*)child;
  716. return 1;
  717. }
  718. static int stbl_add_stts(mp4_context_t const* UNUSED(mp4_context),
  719. void* parent, void* child)
  720. {
  721. stbl_t* stbl = (stbl_t*)parent;
  722. stbl->stts_ = (stts_t*)child;
  723. return 1;
  724. }
  725. static int stbl_add_stss(mp4_context_t const* UNUSED(mp4_context),
  726. void* parent, void* child)
  727. {
  728. stbl_t* stbl = (stbl_t*)parent;
  729. stbl->stss_ = (stss_t*)child;
  730. return 1;
  731. }
  732. static int stbl_add_stsc(mp4_context_t const* UNUSED(mp4_context),
  733. void* parent, void* child)
  734. {
  735. stbl_t* stbl = (stbl_t*)parent;
  736. stbl->stsc_ = (stsc_t*)child;
  737. return 1;
  738. }
  739. static int stbl_add_stsz(mp4_context_t const* UNUSED(mp4_context),
  740. void* parent, void* child)
  741. {
  742. stbl_t* stbl = (stbl_t*)parent;
  743. stbl->stsz_ = (stsz_t*)child;
  744. return 1;
  745. }
  746. static int stbl_add_stco(mp4_context_t const* UNUSED(mp4_context),
  747. void* parent, void* child)
  748. {
  749. stbl_t* stbl = (stbl_t*)parent;
  750. stbl->stco_ = (stco_t*)child;
  751. return 1;
  752. }
  753. static int stbl_add_ctts(mp4_context_t const* UNUSED(mp4_context),
  754. void* parent, void* child)
  755. {
  756. stbl_t* stbl = (stbl_t*)parent;
  757. stbl->ctts_ = (ctts_t*)child;
  758. return 1;
  759. }
  760. static void* stbl_read(mp4_context_t const* mp4_context,
  761. void* UNUSED(parent),
  762. unsigned char* buffer, uint64_t size)
  763. {
  764. stbl_t* atom = stbl_init();
  765. atom_read_list_t atom_read_list[] = {
  766. { FOURCC('s', 't', 's', 'd'), &stbl_add_stsd, &stsd_read },
  767. { FOURCC('s', 't', 't', 's'), &stbl_add_stts, &stts_read },
  768. { FOURCC('s', 't', 's', 's'), &stbl_add_stss, &stss_read },
  769. { FOURCC('s', 't', 's', 'c'), &stbl_add_stsc, &stsc_read },
  770. { FOURCC('s', 't', 's', 'z'), &stbl_add_stsz, &stsz_read },
  771. { FOURCC('s', 't', 'c', 'o'), &stbl_add_stco, &stco_read },
  772. { FOURCC('c', 'o', '6', '4'), &stbl_add_stco, &co64_read },
  773. { FOURCC('c', 't', 't', 's'), &stbl_add_ctts, &ctts_read },
  774. };
  775. int result = atom_reader(mp4_context,
  776. atom_read_list,
  777. sizeof(atom_read_list) / sizeof(atom_read_list[0]),
  778. atom,
  779. buffer, size);
  780. // check for mandatory atoms
  781. if(!atom->stsd_)
  782. {
  783. MP4_ERROR("%s", "stbl: missing mandatory stsd\n");
  784. result = 0;
  785. }
  786. if(!atom->stts_)
  787. {
  788. MP4_ERROR("%s", "stbl: missing mandatory stts\n");
  789. result = 0;
  790. }
  791. // Expression Encoder doesn't write mandatory stsz atom
  792. if(!atom->stsc_)
  793. {
  794. MP4_ERROR("%s", "stbl: missing mandatory stsc\n");
  795. // result = 0;
  796. }
  797. // Expression Encoder doesn't write mandatory stsz atom
  798. if(!atom->stsz_)
  799. {
  800. MP4_ERROR("%s", "stbl: missing mandatory stsz\n");
  801. // result = 0;
  802. }
  803. // Expression Encoder doesn't write mandatory stco atom
  804. if(!atom->stco_)
  805. {
  806. MP4_ERROR("%s", "stbl: missing mandatory stco\n");
  807. // result = 0;
  808. }
  809. if(!result)
  810. {
  811. stbl_exit(atom);
  812. return 0;
  813. }
  814. return atom;
  815. }
  816. static void* hdlr_read(mp4_context_t const* UNUSED(mp4_context),
  817. void* UNUSED(parent),
  818. unsigned char* buffer, uint64_t size)
  819. {
  820. hdlr_t* atom;
  821. if(size < 8)
  822. return 0;
  823. atom = hdlr_init();
  824. atom->version_ = read_8(buffer + 0);
  825. atom->flags_ = read_24(buffer + 1);
  826. atom->predefined_ = read_32(buffer + 4);
  827. atom->handler_type_ = read_32(buffer + 8);
  828. atom->reserved1_ = read_32(buffer + 12);
  829. atom->reserved2_ = read_32(buffer + 16);
  830. atom->reserved3_ = read_32(buffer + 20);
  831. buffer += 24;
  832. size -= 24;
  833. if(size > 0)
  834. {
  835. size_t length = (size_t)size;
  836. atom->name_ = (char*)malloc(length + 1);
  837. if(atom->predefined_ == FOURCC('m', 'h', 'l', 'r'))
  838. {
  839. length = read_8(buffer);
  840. buffer += 1;
  841. if(size < length)
  842. length = (size_t)size;
  843. }
  844. memcpy(atom->name_, buffer, length);
  845. atom->name_[length] = '\0';
  846. }
  847. return atom;
  848. }
  849. static void* vmhd_read(mp4_context_t const* UNUSED(mp4_context),
  850. void* UNUSED(parent),
  851. unsigned char* buffer, uint64_t size)
  852. {
  853. unsigned int i;
  854. vmhd_t* atom;
  855. if(size < 12)
  856. return 0;
  857. atom = vmhd_init();
  858. atom->version_ = read_8(buffer + 0);
  859. atom->flags_ = read_24(buffer + 1);
  860. atom->graphics_mode_ = read_16(buffer + 4);
  861. buffer += 6;
  862. for(i = 0; i != 3; ++i)
  863. {
  864. atom->opcolor_[i] = read_16(buffer);
  865. buffer += 2;
  866. }
  867. return atom;
  868. }
  869. static void* smhd_read(mp4_context_t const* UNUSED(mp4_context),
  870. void* UNUSED(parent),
  871. unsigned char* buffer, uint64_t size)
  872. {
  873. smhd_t* atom;
  874. if(size < 8)
  875. return 0;
  876. atom = smhd_init();
  877. atom->version_ = read_8(buffer + 0);
  878. atom->flags_ = read_24(buffer + 1);
  879. atom->balance_ = read_16(buffer + 4);
  880. atom->reserved_ = read_16(buffer + 6);
  881. return atom;
  882. }
  883. static int dinf_add_dref(mp4_context_t const* UNUSED(mp4_context),
  884. void* parent, void* child)
  885. {
  886. dinf_t* dinf = (dinf_t*)parent;
  887. dinf->dref_ = (dref_t*)child;
  888. return 1;
  889. }
  890. static void* dref_read(mp4_context_t const* UNUSED(mp4_context),
  891. void* UNUSED(parent),
  892. unsigned char* buffer, uint64_t size)
  893. {
  894. unsigned int i;
  895. dref_t* atom;
  896. if(size < 20)
  897. return 0;
  898. atom = dref_init();
  899. atom->version_ = read_8(buffer + 0);
  900. atom->flags_ = read_24(buffer + 1);
  901. atom->entry_count_ = read_32(buffer + 4);
  902. atom->table_ = atom->entry_count_ == 0 ? NULL : (dref_table_t*)malloc(atom->entry_count_ * sizeof(dref_table_t));
  903. buffer += 8;
  904. for(i = 0; i != atom->entry_count_; ++i)
  905. {
  906. dref_table_t* entry = &atom->table_[i];
  907. uint32_t entry_size = read_32(buffer + 0);
  908. uint32_t type = read_32(buffer + 4);
  909. uint32_t flags = read_32(buffer + 8);
  910. dref_table_init(entry);
  911. entry->flags_ = flags;
  912. if(flags != 0x000001)
  913. {
  914. switch(type)
  915. {
  916. case FOURCC('u', 'r', 'n', ' '):
  917. // read name and location (optional) as UTF8 zero-terminated string
  918. break;
  919. case FOURCC('u', 'r', 'l', ' '):
  920. // read location as UTF8 zero-terminated string
  921. break;
  922. }
  923. }
  924. buffer += entry_size;
  925. }
  926. return atom;
  927. }
  928. static void* dinf_read(mp4_context_t const* mp4_context,
  929. void* UNUSED(parent),
  930. unsigned char* buffer, uint64_t size)
  931. {
  932. dinf_t* atom = dinf_init();
  933. atom_read_list_t atom_read_list[] = {
  934. { FOURCC('d', 'r', 'e', 'f'), &dinf_add_dref, &dref_read },
  935. };
  936. int result = atom_reader(mp4_context,
  937. atom_read_list,
  938. sizeof(atom_read_list) / sizeof(atom_read_list[0]),
  939. atom,
  940. buffer, size);
  941. // check for mandatory atoms
  942. if(!atom->dref_)
  943. {
  944. MP4_ERROR("%s", "dinf: missing dref\n");
  945. result = 0;
  946. }
  947. if(!result)
  948. {
  949. dinf_exit(atom);
  950. return 0;
  951. }
  952. return atom;
  953. }
  954. static int minf_add_vmhd(mp4_context_t const* UNUSED(mp4_context),
  955. void* parent, void* child)
  956. {
  957. minf_t* minf = (minf_t*)parent;
  958. minf->vmhd_ = (vmhd_t*)child;
  959. return 1;
  960. }
  961. static int minf_add_smhd(mp4_context_t const* UNUSED(mp4_context),
  962. void* parent, void* child)
  963. {
  964. minf_t* minf = (minf_t*)parent;
  965. minf->smhd_ = (smhd_t*)child;
  966. return 1;
  967. }
  968. static int minf_add_dinf(mp4_context_t const* UNUSED(mp4_context),
  969. void* parent, void* child)
  970. {
  971. minf_t* minf = (minf_t*)parent;
  972. minf->dinf_ = (dinf_t*)child;
  973. return 1;
  974. }
  975. static int minf_add_stbl(mp4_context_t const* UNUSED(mp4_context),
  976. void* parent, void* child)
  977. {
  978. minf_t* minf = (minf_t*)parent;
  979. minf->stbl_ = (stbl_t*)child;
  980. return 1;
  981. }
  982. static void* minf_read(mp4_context_t const* mp4_context,
  983. void* UNUSED(parent),
  984. unsigned char* buffer, uint64_t size)
  985. {
  986. minf_t* atom = minf_init();
  987. atom_read_list_t atom_read_list[] = {
  988. { FOURCC('v', 'm', 'h', 'd'), &minf_add_vmhd, &vmhd_read },
  989. { FOURCC('s', 'm', 'h', 'd'), &minf_add_smhd, &smhd_read },
  990. { FOURCC('d', 'i', 'n', 'f'), &minf_add_dinf, &dinf_read },
  991. { FOURCC('s', 't', 'b', 'l'), &minf_add_stbl, &stbl_read }
  992. };
  993. int result = atom_reader(mp4_context,
  994. atom_read_list,
  995. sizeof(atom_read_list) / sizeof(atom_read_list[0]),
  996. atom,
  997. buffer, size);
  998. // check for mandatory atoms
  999. if(!atom->stbl_)
  1000. {
  1001. MP4_ERROR("%s", "minf: missing stbl\n");
  1002. result = 0;
  1003. }
  1004. if(!result)
  1005. {
  1006. minf_exit(atom);
  1007. return 0;
  1008. }
  1009. return atom;
  1010. }
  1011. static void* mdhd_read(mp4_context_t const* UNUSED(mp4_context),
  1012. void* UNUSED(parent),
  1013. unsigned char* buffer, uint64_t UNUSED(size))
  1014. {
  1015. uint16_t language;
  1016. unsigned int i;
  1017. mdhd_t* mdhd = mdhd_init();
  1018. mdhd->version_ = read_8(buffer + 0);
  1019. mdhd->flags_ = read_24(buffer + 1);
  1020. if(mdhd->version_ == 0)
  1021. {
  1022. mdhd->creation_time_ = read_32(buffer + 4);
  1023. mdhd->modification_time_ = read_32(buffer + 8);
  1024. mdhd->timescale_ = read_32(buffer + 12);
  1025. mdhd->duration_ = read_32(buffer + 16);
  1026. buffer += 20;
  1027. }
  1028. else
  1029. {
  1030. mdhd->creation_time_ = read_64(buffer + 4);
  1031. mdhd->modification_time_ = read_64(buffer + 12);
  1032. mdhd->timescale_ = read_32(buffer + 20);
  1033. mdhd->duration_ = read_64(buffer + 24);
  1034. buffer += 32;
  1035. }
  1036. language = read_16(buffer + 0);
  1037. for(i = 0; i != 3; ++i)
  1038. {
  1039. mdhd->language_[i] = ((language >> ((2 - i) * 5)) & 0x1f) + 0x60;
  1040. }
  1041. mdhd->predefined_ = read_16(buffer + 2);
  1042. return mdhd;
  1043. }
  1044. static void* tkhd_read(mp4_context_t const* UNUSED(mp4_context),
  1045. void* UNUSED(parent),
  1046. unsigned char* buffer, uint64_t size)
  1047. {
  1048. unsigned int i;
  1049. tkhd_t* tkhd = tkhd_init();
  1050. tkhd->version_ = read_8(buffer + 0);
  1051. tkhd->flags_ = read_24(buffer + 1);
  1052. if(tkhd->version_ == 0)
  1053. {
  1054. if(size < 92-8)
  1055. return 0;
  1056. tkhd->creation_time_ = read_32(buffer + 4);
  1057. tkhd->modification_time_ = read_32(buffer + 8);
  1058. tkhd->track_id_ = read_32(buffer + 12);
  1059. tkhd->reserved_ = read_32(buffer + 16);
  1060. tkhd->duration_ = read_32(buffer + 20);
  1061. buffer += 24;
  1062. }
  1063. else
  1064. {
  1065. if(size < 104-8)
  1066. return 0;
  1067. tkhd->creation_time_ = read_64(buffer + 4);
  1068. tkhd->modification_time_ = read_64(buffer + 12);
  1069. tkhd->track_id_ = read_32(buffer + 20);
  1070. tkhd->reserved_ = read_32(buffer + 24);
  1071. tkhd->duration_ = read_64(buffer + 28);
  1072. buffer += 36;
  1073. }
  1074. tkhd->reserved2_[0] = read_32(buffer + 0);
  1075. tkhd->reserved2_[1] = read_32(buffer + 4);
  1076. tkhd->layer_ = read_16(buffer + 8);
  1077. tkhd->predefined_ = read_16(buffer + 10);
  1078. tkhd->volume_ = read_16(buffer + 12);
  1079. tkhd->reserved3_ = read_16(buffer + 14);
  1080. buffer += 16;
  1081. for(i = 0; i != 9; ++i)
  1082. {
  1083. tkhd->matrix_[i] = read_32(buffer);
  1084. buffer += 4;
  1085. }
  1086. tkhd->width_ = read_32(buffer + 0);
  1087. tkhd->height_ = read_32(buffer + 4);
  1088. return tkhd;
  1089. }
  1090. static int mdia_add_mdhd(mp4_context_t const* UNUSED(mp4_context),
  1091. void* parent, void* child)
  1092. {
  1093. mdia_t* mdia = (mdia_t*)parent;
  1094. mdia->mdhd_ = (mdhd_t*)child;
  1095. return 1;
  1096. }
  1097. static int mdia_add_hdlr(mp4_context_t const* UNUSED(mp4_context),
  1098. void* parent, void* child)
  1099. {
  1100. mdia_t* mdia = (mdia_t*)parent;
  1101. mdia->hdlr_ = (hdlr_t*)child;
  1102. return 1;
  1103. }
  1104. static int mdia_add_minf(mp4_context_t const* UNUSED(mp4_context),
  1105. void* parent, void* child)
  1106. {
  1107. mdia_t* mdia = (mdia_t*)parent;
  1108. mdia->minf_ = (minf_t*)child;
  1109. return 1;
  1110. }
  1111. static void* mdia_read(mp4_context_t const* mp4_context,
  1112. void* UNUSED(parent),
  1113. unsigned char* buffer, uint64_t size)
  1114. {
  1115. mdia_t* atom = mdia_init();
  1116. atom_read_list_t atom_read_list[] = {
  1117. { FOURCC('m', 'd', 'h', 'd'), &mdia_add_mdhd, &mdhd_read },
  1118. { FOURCC('h', 'd', 'l', 'r'), &mdia_add_hdlr, &hdlr_read },
  1119. { FOURCC('m', 'i', 'n', 'f'), &mdia_add_minf, &minf_read }
  1120. };
  1121. int result = atom_reader(mp4_context,
  1122. atom_read_list,
  1123. sizeof(atom_read_list) / sizeof(atom_read_list[0]),
  1124. atom,
  1125. buffer, size);
  1126. // check for mandatory atoms
  1127. if(!atom->mdhd_)
  1128. {
  1129. MP4_ERROR("%s", "mdia: missing mdhd\n");
  1130. result = 0;
  1131. }
  1132. if(!atom->hdlr_)
  1133. {
  1134. MP4_ERROR("%s", "mdia: missing hdlr\n");
  1135. result = 0;
  1136. }
  1137. if(!atom->minf_)
  1138. {
  1139. MP4_ERROR("%s", "mdia: missing minf\n");
  1140. result = 0;
  1141. }
  1142. if(!result)
  1143. {
  1144. mdia_exit(atom);
  1145. return 0;
  1146. }
  1147. return atom;
  1148. }
  1149. static void* elst_read(mp4_context_t const* UNUSED(mp4_context),
  1150. void* UNUSED(parent),
  1151. unsigned char* buffer, uint64_t size)
  1152. {
  1153. unsigned int i;
  1154. elst_t* atom;
  1155. if(size < 8)
  1156. return 0;
  1157. atom = elst_init();
  1158. atom->version_ = read_8(buffer + 0);
  1159. atom->flags_ = read_24(buffer + 1);
  1160. atom->entry_count_ = read_32(buffer + 4);
  1161. buffer += 8;
  1162. atom->table_ = (elst_table_t*)(malloc(atom->entry_count_ * sizeof(elst_table_t)));
  1163. for(i = 0; i != atom->entry_count_; ++i)
  1164. {
  1165. elst_table_t* elst_table = &atom->table_[i];
  1166. if(atom->version_ == 0)
  1167. {
  1168. elst_table->segment_duration_ = read_32(buffer);
  1169. elst_table->media_time_ = (int32_t)read_32(buffer + 4);
  1170. buffer += 8;
  1171. }
  1172. else
  1173. {
  1174. elst_table->segment_duration_ = read_64(buffer);
  1175. elst_table->media_time_ = read_64(buffer + 8);
  1176. buffer += 16;
  1177. }
  1178. elst_table->media_rate_integer_ = read_16(buffer);
  1179. elst_table->media_rate_fraction_ = read_16(buffer + 2);
  1180. buffer += 4;
  1181. }
  1182. return atom;
  1183. }
  1184. static int edts_add_elst(mp4_context_t const* UNUSED(mp4_context),
  1185. void* parent, void* elst)
  1186. {
  1187. edts_t* edts = (edts_t*)parent;
  1188. edts->elst_ = (elst_t*)elst;
  1189. return 1;
  1190. }
  1191. static void* edts_read(mp4_context_t const* mp4_context,
  1192. void* UNUSED(parent),
  1193. unsigned char* buffer, uint64_t size)
  1194. {
  1195. edts_t* atom = edts_init();
  1196. atom_read_list_t atom_read_list[] = {
  1197. { FOURCC('e', 'l', 's', 't'), &edts_add_elst, &elst_read }
  1198. };
  1199. int result = atom_reader(mp4_context,
  1200. atom_read_list,
  1201. sizeof(atom_read_list) / sizeof(atom_read_list[0]),
  1202. atom,
  1203. buffer, size);
  1204. if(!result)
  1205. {
  1206. edts_exit(atom);
  1207. return 0;
  1208. }
  1209. return atom;
  1210. }
  1211. static int trak_add_tkhd(mp4_context_t const* UNUSED(mp4_context),
  1212. void* parent, void* tkhd)
  1213. {
  1214. trak_t* trak = (trak_t*)parent;
  1215. trak->tkhd_ = (tkhd_t*)tkhd;
  1216. return 1;
  1217. }
  1218. static int trak_add_mdia(mp4_context_t const* UNUSED(mp4_context),
  1219. void* parent, void* mdia)
  1220. {
  1221. trak_t* trak = (trak_t*)parent;
  1222. trak->mdia_ = (mdia_t*)mdia;
  1223. return 1;
  1224. }
  1225. static int trak_add_edts(mp4_context_t const* UNUSED(mp4_context),
  1226. void* parent, void* edts)
  1227. {
  1228. trak_t* trak = (trak_t*)parent;
  1229. trak->edts_ = (edts_t*)edts;
  1230. return 1;
  1231. }
  1232. static void* trak_read(mp4_context_t const* mp4_context,
  1233. void* UNUSED(parent),
  1234. unsigned char* buffer, uint64_t size)
  1235. {
  1236. trak_t* atom = trak_init();
  1237. atom_read_list_t atom_read_list[] = {
  1238. { FOURCC('t', 'k', 'h', 'd'), &trak_add_tkhd, &tkhd_read },
  1239. { FOURCC('m', 'd', 'i', 'a'), &trak_add_mdia, &mdia_read },
  1240. { FOURCC('e', 'd', 't', 's'), &trak_add_edts, &edts_read }
  1241. };
  1242. int result = atom_reader(mp4_context,
  1243. atom_read_list,
  1244. sizeof(atom_read_list) / sizeof(atom_read_list[0]),
  1245. atom,
  1246. buffer, size);
  1247. // check for mandatory atoms
  1248. if(!atom->tkhd_)
  1249. {
  1250. MP4_ERROR("%s", "trak: missing tkhd\n");
  1251. result = 0;
  1252. }
  1253. if(!atom->mdia_)
  1254. {
  1255. MP4_ERROR("%s", "trak: missing mdia\n");
  1256. result = 0;
  1257. }
  1258. if(result && !stsd_parse(mp4_context, atom, atom->mdia_->minf_->stbl_->stsd_))
  1259. {
  1260. MP4_ERROR("%s", "trak: error parsing stsd\n");
  1261. result = 0;
  1262. }
  1263. if(!result)
  1264. {
  1265. trak_exit(atom);
  1266. return 0;
  1267. }
  1268. return atom;
  1269. }
  1270. static void* mvhd_read(mp4_context_t const* UNUSED(mp4_context),
  1271. void* UNUSED(parent),
  1272. unsigned char* buffer, uint64_t size)
  1273. {
  1274. unsigned int i;
  1275. mvhd_t* atom = mvhd_init();
  1276. atom->version_ = read_8(buffer + 0);
  1277. atom->flags_ = read_24(buffer + 1);
  1278. if(atom->version_ == 0)
  1279. {
  1280. if(size < 108-8)
  1281. return 0;
  1282. atom->creation_time_ = read_32(buffer + 4);
  1283. atom->modification_time_ = read_32(buffer + 8);
  1284. atom->timescale_ = read_32(buffer + 12);
  1285. atom->duration_ = read_32(buffer + 16);
  1286. buffer += 20;
  1287. }
  1288. else
  1289. {
  1290. if(size < 120-8)
  1291. return 0;
  1292. atom->creation_time_ = read_64(buffer + 4);
  1293. atom->modification_time_ = read_64(buffer + 12);
  1294. atom->timescale_ = read_32(buffer + 20);
  1295. atom->duration_ = read_64(buffer + 24);
  1296. buffer += 32;
  1297. }
  1298. atom->rate_ = read_32(buffer + 0);
  1299. atom->volume_ = read_16(buffer + 4);
  1300. atom->reserved1_ = read_16(buffer + 6);
  1301. atom->reserved2_[0] = read_32(buffer + 8);
  1302. atom->reserved2_[1] = read_32(buffer + 12);
  1303. buffer += 16;
  1304. for(i = 0; i != 9; ++i)
  1305. {
  1306. atom->matrix_[i] = read_32(buffer);
  1307. buffer += 4;
  1308. }
  1309. for(i = 0; i != 6; ++i)
  1310. {
  1311. atom->predefined_[i] = read_32(buffer);
  1312. buffer += 4;
  1313. }
  1314. atom->next_track_id_ = read_32(buffer + 0);
  1315. return atom;
  1316. }
  1317. static int moov_add_mvhd(mp4_context_t const* UNUSED(mp4_context),
  1318. void* parent, void* mvhd)
  1319. {
  1320. moov_t* moov = (moov_t*)parent;
  1321. moov->mvhd_ = (mvhd_t*)mvhd;
  1322. return 1;
  1323. }
  1324. static int moov_add_trak(mp4_context_t const* mp4_context,
  1325. void* parent, void* child)
  1326. {
  1327. moov_t* moov = (moov_t*)parent;
  1328. trak_t* trak = (trak_t*)child;
  1329. if(moov->tracks_ == MAX_TRACKS)
  1330. {
  1331. trak_exit(trak);
  1332. return 0;
  1333. }
  1334. if(trak->mdia_->hdlr_->handler_type_ != FOURCC('v', 'i', 'd', 'e') &&
  1335. trak->mdia_->hdlr_->handler_type_ != FOURCC('s', 'o', 'u', 'n'))
  1336. {
  1337. MP4_INFO("Trak ignored (handler_type=%c%c%c%c, name=%s)\n",
  1338. trak->mdia_->hdlr_->handler_type_ >> 24,
  1339. trak->mdia_->hdlr_->handler_type_ >> 16,
  1340. trak->mdia_->hdlr_->handler_type_ >> 8,
  1341. trak->mdia_->hdlr_->handler_type_,
  1342. trak->mdia_->hdlr_->name_);
  1343. trak_exit(trak);
  1344. return 1; // continue
  1345. }
  1346. // check for tracks that have a duration, but no samples. This happens with
  1347. // Expression Encoder fragmented movie files.
  1348. if(trak->mdia_->minf_->stbl_->stco_ == 0 ||
  1349. (trak->mdia_->minf_->stbl_->stco_->entries_ == 0 &&
  1350. trak->mdia_->mdhd_->duration_))
  1351. {
  1352. trak->mdia_->mdhd_->duration_ = 0;
  1353. }
  1354. #if 0 // we can't ignore empty tracks, as the fragments may come later
  1355. // ignore empty track (unless LIVE)
  1356. if(trak->mdia_->mdhd_->duration_ == 0 && !moov->mvex_)
  1357. {
  1358. trak_exit(trak);
  1359. return 1; // continue
  1360. }
  1361. #endif
  1362. moov->traks_[moov->tracks_] = trak;
  1363. ++moov->tracks_;
  1364. return 1;
  1365. }
  1366. static void* trex_read(mp4_context_t const* UNUSED(mp4_context),
  1367. void* UNUSED(parent),
  1368. unsigned char* buffer, uint64_t size)
  1369. {
  1370. trex_t* atom = trex_init();
  1371. if(size < 24)
  1372. return 0;
  1373. atom->version_ = read_8(buffer + 0);
  1374. atom->flags_ = read_24(buffer + 1);
  1375. atom->track_id_ = read_32(buffer + 4);
  1376. atom->default_sample_description_index_ = read_32(buffer + 8);
  1377. atom->default_sample_duration_ = read_32(buffer + 12);
  1378. atom->default_sample_size_ = read_32(buffer + 16);
  1379. atom->default_sample_flags_ = read_32(buffer + 20);
  1380. return atom;
  1381. }
  1382. static int moov_add_mvex(mp4_context_t const* UNUSED(mp4_context),
  1383. void* parent, void* mvex)
  1384. {
  1385. moov_t* moov = (moov_t*)parent;
  1386. moov->mvex_ = (mvex_t*)mvex;
  1387. return 1;
  1388. }
  1389. static int mvex_add_trex(mp4_context_t const* UNUSED(mp4_context),
  1390. void* parent, void* child)
  1391. {
  1392. mvex_t* mvex = (mvex_t*)parent;
  1393. trex_t* trex = (trex_t*)child;
  1394. if(mvex->tracks_ == MAX_TRACKS)
  1395. {
  1396. trex_exit(trex);
  1397. return 0;
  1398. }
  1399. mvex->trexs_[mvex->tracks_] = trex;
  1400. ++mvex->tracks_;
  1401. return 1;
  1402. }
  1403. static void* mvex_read(mp4_context_t const* mp4_context,
  1404. void* UNUSED(parent),
  1405. unsigned char* buffer, uint64_t size)
  1406. {
  1407. mvex_t* atom = mvex_init();
  1408. atom_read_list_t atom_read_list[] = {
  1409. { FOURCC('t', 'r', 'e', 'x'), &mvex_add_trex, &trex_read }
  1410. };
  1411. int result = atom_reader(mp4_context,
  1412. atom_read_list,
  1413. sizeof(atom_read_list) / sizeof(atom_read_list[0]),
  1414. atom,
  1415. buffer, size);
  1416. // check for mandatory atoms
  1417. if(!atom->tracks_)
  1418. {
  1419. MP4_ERROR("%s", "mvex: missing trex\n");
  1420. result = 0;
  1421. }
  1422. if(!result)
  1423. {
  1424. mvex_exit(atom);
  1425. return 0;
  1426. }
  1427. return atom;
  1428. }
  1429. extern void* moov_read(mp4_context_t const* mp4_context,
  1430. void* UNUSED(parent),
  1431. unsigned char* buffer, uint64_t size)
  1432. {
  1433. moov_t* atom = moov_init();
  1434. atom_read_list_t atom_read_list[] = {
  1435. { FOURCC('m', 'v', 'h', 'd'), &moov_add_mvhd, &mvhd_read },
  1436. { FOURCC('t', 'r', 'a', 'k'), &moov_add_trak, &trak_read },
  1437. { FOURCC('m', 'v', 'e', 'x'), &moov_add_mvex, &mvex_read }
  1438. };
  1439. int result = atom_reader(mp4_context,
  1440. atom_read_list,
  1441. sizeof(atom_read_list) / sizeof(atom_read_list[0]),
  1442. atom,
  1443. buffer, size);
  1444. // check for mandatory atoms
  1445. if(!atom->mvhd_)
  1446. {
  1447. MP4_ERROR("%s", "moov: missing mvhd\n");
  1448. result = 0;
  1449. }
  1450. if(!atom->tracks_)
  1451. {
  1452. MP4_ERROR("%s", "moov: missing trak\n");
  1453. result = 0;
  1454. }
  1455. if(!result)
  1456. {
  1457. moov_exit(atom);
  1458. return 0;
  1459. }
  1460. return atom;
  1461. }
  1462. static int add_fragmented_samples(mp4_context_t const* mp4_context,
  1463. traf_t const* traf)
  1464. {
  1465. unsigned int track;
  1466. moov_t* moov = mp4_context->moov;
  1467. trak_t* trak = NULL;
  1468. trun_t* trun;
  1469. for(track = 0; track != moov->tracks_; ++track)
  1470. {
  1471. trak = moov->traks_[track];
  1472. if(trak->tkhd_->track_id_ == traf->tfhd_->track_id_)
  1473. break;
  1474. }
  1475. if(track == moov->tracks_)
  1476. {
  1477. MP4_ERROR("%s", "add_fragmented_samples: trak not found\n");
  1478. return 0;
  1479. }
  1480. trun = traf->trun_;
  1481. for(; trun != NULL; trun = trun->next_)
  1482. {
  1483. tfhd_t const* tfhd = traf->tfhd_;
  1484. // trun_t const* trun = traf->trun_;
  1485. unsigned int i;
  1486. unsigned int s = trak->samples_size_;
  1487. uint64_t data_offset = tfhd->base_data_offset_ + trun->data_offset_;
  1488. // int64_t pts = trak->fragment_pts_; // trak->mdia_->mdhd_->duration_;
  1489. int64_t pts = trak->mdia_->mdhd_->duration_;
  1490. unsigned int cto = 0;
  1491. //
  1492. if(pts == 0 && trak->edts_)
  1493. {
  1494. elst_t const* elst = trak->edts_->elst_;
  1495. if(elst && elst->entry_count_ > 0)
  1496. {
  1497. elst_table_t* elst_table = &elst->table_[0];
  1498. if(elst_table->media_time_ >= -1)
  1499. {
  1500. pts = elst_table->media_time_ != -1 ? elst_table->media_time_
  1501. : (int64_t)elst_table->segment_duration_;
  1502. trak->mdia_->mdhd_->duration_ = pts;
  1503. // trak->fragment_pts_ = pts;
  1504. }
  1505. }
  1506. }
  1507. trak->samples_size_ += trun->sample_count_;
  1508. // reserve one extra for the end information (like pts and cto).
  1509. trak->samples_ = (samples_t*)realloc(trak->samples_,
  1510. (trak->samples_size_ + 1) * sizeof(samples_t));
  1511. for(i = 0; i != trun->sample_count_; ++i)
  1512. {
  1513. samples_t* sample = &trak->samples_[s + i];
  1514. trun_table_t const* trun_table = &trun->table_[i];
  1515. unsigned int is_difference_sample = (trun_table->sample_flags_ >> 16) & 1;
  1516. cto = trun_table->sample_composition_time_offset_;
  1517. sample->pts_ = pts;
  1518. sample->size_ = trun_table->sample_size_;
  1519. sample->pos_ = data_offset;
  1520. sample->cto_ = trun_table->sample_composition_time_offset_;
  1521. // sample->is_ss_ = i == 0 ? 1 : 0;
  1522. sample->is_smooth_ss_ = i == 0 ? 1 : 0;
  1523. sample->is_ss_ = is_difference_sample ? 0 : 1;
  1524. #if 0
  1525. {
  1526. unsigned int depends_on = (trun_table->sample_flags_ >> 24) & 3;
  1527. unsigned int is_depended_on = (trun_table->sample_flags_ >> 22) & 3;
  1528. unsigned int has_redundancy = (trun_table->sample_flags_ >> 20) & 3;
  1529. unsigned int padding_value = (trun_table->sample_flags_ >> 17) & 7;
  1530. unsigned int is_difference_sample = (trun_table->sample_flags_ >> 16) & 1;
  1531. unsigned int degradation_priority = (trun_table->sample_flags_ >> 0) & 0xffff;
  1532. MP4_INFO("sample_flags: depends_on=%u is_depended_on=%u has_redundancy=%u padding=%u is_difference_sample=%u degradation_priority=%u\n",
  1533. depends_on, is_depended_on, has_redundancy, padding_value, is_difference_sample, degradation_priority);
  1534. }
  1535. #endif
  1536. pts += trun_table->sample_duration_;
  1537. data_offset += sample->size_;
  1538. // add fragment duration to track duration
  1539. trak->mdia_->mdhd_->duration_ += trun_table->sample_duration_;
  1540. // trak->fragment_pts_ += trun_table->sample_duration_;
  1541. }
  1542. // write end pts
  1543. trak->samples_[s + i].pts_ = pts;
  1544. trak->samples_[s + i].size_ = 0;
  1545. trak->samples_[s + i].pos_ = data_offset;
  1546. trak->samples_[s + i].cto_ = cto;
  1547. trak->samples_[s + i].is_ss_ = 1;
  1548. trak->samples_[s + i].is_smooth_ss_ = 1;
  1549. }
  1550. return 1;
  1551. }
  1552. static void* trun_read(mp4_context_t const* UNUSED(mp4_context),
  1553. void* parent,
  1554. unsigned char* buffer, uint64_t size)
  1555. {
  1556. unsigned int i;
  1557. trun_t* atom = trun_init();
  1558. traf_t* traf = (traf_t*)parent;
  1559. tfhd_t* tfhd = traf->tfhd_;
  1560. if(size < 8)
  1561. return 0;
  1562. atom->version_ = read_8(buffer + 0);
  1563. atom->flags_ = read_24(buffer + 1);
  1564. atom->sample_count_ = read_32(buffer + 4);
  1565. buffer += 8;
  1566. if(atom->flags_ & 0x0001)
  1567. {
  1568. atom->data_offset_ = read_32(buffer);
  1569. buffer += 4;
  1570. }
  1571. if(atom->flags_ & 0x0004)
  1572. {
  1573. atom->first_sample_flags_ = read_32(buffer);
  1574. buffer += 4;
  1575. }
  1576. atom->table_ = (trun_table_t*)malloc(atom->sample_count_ * sizeof(trun_table_t));
  1577. for(i = 0; i != atom->sample_count_; ++i)
  1578. {
  1579. uint32_t sample_duration = tfhd->default_sample_duration_;
  1580. uint32_t sample_size = tfhd->default_sample_size_;
  1581. uint32_t sample_flags = tfhd->default_sample_flags_;
  1582. uint32_t sample_composition_time_offset = 0;
  1583. if(atom->flags_ & 0x0100)
  1584. {
  1585. sample_duration = read_32(buffer);
  1586. buffer += 4;
  1587. }
  1588. if(atom->flags_ & 0x0200)
  1589. {
  1590. sample_size = read_32(buffer);
  1591. buffer += 4;
  1592. }
  1593. if(atom->flags_ & 0x0400)
  1594. {
  1595. sample_flags = read_32(buffer);
  1596. buffer += 4;
  1597. }
  1598. else
  1599. if(i == 0 && (atom->flags_ & 0x0004))
  1600. {
  1601. sample_flags = atom->first_sample_flags_;
  1602. }
  1603. if(atom->flags_ & 0x0800)
  1604. {
  1605. sample_composition_time_offset = read_32(buffer);
  1606. buffer += 4;
  1607. }
  1608. atom->table_[i].sample_duration_ = sample_duration;
  1609. atom->table_[i].sample_size_ = sample_size;
  1610. atom->table_[i].sample_flags_ = sample_flags;
  1611. atom->table_[i].sample_composition_time_offset_ =
  1612. sample_composition_time_offset;
  1613. }
  1614. return atom;
  1615. }
  1616. static void* tfhd_read(mp4_context_t const* mp4_context,
  1617. void* UNUSED(parent),
  1618. unsigned char* buffer, uint64_t size)
  1619. {
  1620. unsigned int i;
  1621. tfhd_t* atom = tfhd_init();
  1622. moov_t const* moov = mp4_context->moov;
  1623. mvex_t const* mvex = moov->mvex_;
  1624. trex_t* trex = NULL;
  1625. if(size < 8)
  1626. return 0;
  1627. if(mvex == NULL)
  1628. {
  1629. MP4_ERROR("%s", "tfhd: mvex not found\n");
  1630. return 0;
  1631. }
  1632. atom->version_ = read_8(buffer + 0);
  1633. atom->flags_ = read_24(buffer + 1);
  1634. atom->track_id_ = read_32(buffer + 4);
  1635. buffer += 8;
  1636. // trex_t trex_default;
  1637. // trex_default.default_sample_description_index_ = 1;
  1638. // trex_default.default_sample_duration_ = 0;
  1639. // trex_default.default_sample_size_ = 0;
  1640. // trex_default.default_sample_flags_ = 0;
  1641. // if(mvex == NULL)
  1642. // {
  1643. // trex = &trex_default;
  1644. //}
  1645. // else
  1646. // {
  1647. for(i = 0; i != mvex->tracks_; ++i)
  1648. {
  1649. if(mvex->trexs_[i]->track_id_ == atom->track_id_)
  1650. {
  1651. trex = mvex->trexs_[i];
  1652. break;
  1653. }
  1654. }
  1655. // }
  1656. if(trex == NULL)
  1657. {
  1658. MP4_ERROR("tfhd: trex not found (track_id=%u)\n", atom->track_id_);
  1659. return 0;
  1660. }
  1661. if(atom->flags_ & 0x000001)
  1662. {
  1663. atom->base_data_offset_ = read_64(buffer);
  1664. buffer += 8;
  1665. }
  1666. else
  1667. {
  1668. atom->base_data_offset_ = mp4_context->moof_offset_;
  1669. }
  1670. if(atom->flags_ & 0x000002)
  1671. {
  1672. atom->sample_description_index_ = read_32(buffer);
  1673. buffer += 4;
  1674. }
  1675. else
  1676. {
  1677. atom->sample_description_index_ = trex->default_sample_description_index_;
  1678. }
  1679. if(atom->flags_ & 0x000008)
  1680. {
  1681. atom->default_sample_duration_ = read_32(buffer);
  1682. buffer += 4;
  1683. }
  1684. else
  1685. {
  1686. atom->default_sample_duration_ = trex->default_sample_duration_;
  1687. }
  1688. if(atom->flags_ & 0x000010)
  1689. {
  1690. atom->default_sample_size_ = read_32(buffer);
  1691. buffer += 4;
  1692. }
  1693. else
  1694. {
  1695. atom->default_sample_size_ = trex->default_sample_size_;
  1696. }
  1697. if(atom->flags_ & 0x000020)
  1698. {
  1699. atom->default_sample_flags_ = read_32(buffer);
  1700. buffer += 4;
  1701. }
  1702. else
  1703. {
  1704. atom->default_sample_flags_ = trex->default_sample_flags_;
  1705. }
  1706. return atom;
  1707. }
  1708. static int traf_add_tfhd(mp4_context_t const* UNUSED(mp4_context),
  1709. void* parent, void* tfhd)
  1710. {
  1711. traf_t* traf = (traf_t*)parent;
  1712. traf->tfhd_ = (tfhd_t*)tfhd;
  1713. return 1;
  1714. }
  1715. static int traf_add_trun(mp4_context_t const* UNUSED(mp4_context),
  1716. void* parent, void* child)
  1717. {
  1718. traf_t* traf = (traf_t*)parent;
  1719. trun_t* trun = (trun_t*)child;
  1720. trun_t** adder = &traf->trun_;
  1721. while(*adder != NULL)
  1722. {
  1723. adder = &(*adder)->next_;
  1724. }
  1725. *adder = trun;
  1726. return 1;
  1727. }
  1728. static void* traf_read(mp4_context_t const* mp4_context,
  1729. void* UNUSED(parent),
  1730. unsigned char* buffer, uint64_t size)
  1731. {
  1732. traf_t* atom = traf_init();
  1733. atom_read_list_t atom_read_list[] = {
  1734. { FOURCC('t', 'f', 'h', 'd'), &traf_add_tfhd, &tfhd_read },
  1735. { FOURCC('t', 'r', 'u', 'n'), &traf_add_trun, &trun_read }
  1736. };
  1737. int result = atom_reader(mp4_context,
  1738. atom_read_list,
  1739. sizeof(atom_read_list) / sizeof(atom_read_list[0]),
  1740. atom,
  1741. buffer, size);
  1742. // check for mandatory atoms
  1743. if(!atom->tfhd_)
  1744. {
  1745. MP4_ERROR("%s", "traf: missing tfhd\n");
  1746. traf_exit(atom);
  1747. return 0;
  1748. }
  1749. if(!result)
  1750. {
  1751. traf_exit(atom);
  1752. return 0;
  1753. }
  1754. if(!add_fragmented_samples(mp4_context, atom))
  1755. {
  1756. traf_exit(atom);
  1757. return 0;
  1758. }
  1759. return atom;
  1760. }
  1761. static void* mfhd_read(mp4_context_t const* UNUSED(mp4_context),
  1762. void* UNUSED(parent),
  1763. unsigned char* buffer, uint64_t size)
  1764. {
  1765. mfhd_t* atom = mfhd_init();
  1766. if(size < 8)
  1767. return 0;
  1768. atom->version_ = read_8(buffer + 0);
  1769. atom->flags_ = read_24(buffer + 1);
  1770. atom->sequence_number_ = read_32(buffer + 4);
  1771. return atom;
  1772. }
  1773. static int moof_add_mfhd(mp4_context_t const* UNUSED(mp4_context),
  1774. void* parent, void* mfhd)
  1775. {
  1776. moof_t* moof = (moof_t*)parent;
  1777. moof->mfhd_ = (mfhd_t*)mfhd;
  1778. return 1;
  1779. }
  1780. static int moof_add_traf(mp4_context_t const* UNUSED(mp4_context),
  1781. void* parent, void* child)
  1782. {
  1783. moof_t* moof = (moof_t*)parent;
  1784. traf_t* traf = (traf_t*)child;
  1785. if(moof->tracks_ == MAX_TRACKS)
  1786. {
  1787. traf_exit(traf);
  1788. return 0;
  1789. }
  1790. moof->trafs_[moof->tracks_] = traf;
  1791. ++moof->tracks_;
  1792. return 1;
  1793. }
  1794. extern void* moof_read(struct mp4_context_t const* mp4_context,
  1795. void* UNUSED(parent),
  1796. unsigned char* buffer, uint64_t size)
  1797. {
  1798. moof_t* atom = moof_init();
  1799. atom_read_list_t atom_read_list[] = {
  1800. { FOURCC('m', 'f', 'h', 'd'), &moof_add_mfhd, &mfhd_read },
  1801. { FOURCC('t', 'r', 'a', 'f'), &moof_add_traf, &traf_read },
  1802. };
  1803. int result = atom_reader(mp4_context,
  1804. atom_read_list,
  1805. sizeof(atom_read_list) / sizeof(atom_read_list[0]),
  1806. atom,
  1807. buffer, size);
  1808. // check for mandatory atoms
  1809. if(!atom->mfhd_)
  1810. {
  1811. MP4_ERROR("%s", "moof: missing mfhd\n");
  1812. result = 0;
  1813. }
  1814. if(!atom->tracks_)
  1815. {
  1816. MP4_ERROR("%s", "moof: missing traf\n");
  1817. result = 0;
  1818. }
  1819. if(!result)
  1820. {
  1821. moof_exit(atom);
  1822. return 0;
  1823. }
  1824. return atom;
  1825. }
  1826. static int trak_build_index(mp4_context_t const* mp4_context, trak_t* trak)
  1827. {
  1828. stco_t const* stco = trak->mdia_->minf_->stbl_->stco_;
  1829. int have_samples = 1;
  1830. unsigned int stco_samples = 0;
  1831. if(stco == NULL || stco->entries_ == 0)
  1832. {
  1833. have_samples = 0;
  1834. }
  1835. if(have_samples)
  1836. {
  1837. trak->chunks_size_ = stco->entries_;
  1838. trak->chunks_ = (chunks_t*)malloc(trak->chunks_size_ * sizeof(chunks_t));
  1839. {
  1840. unsigned int i;
  1841. for(i = 0; i != trak->chunks_size_; ++i)
  1842. {
  1843. trak->chunks_[i].pos_ = stco->chunk_offsets_[i];
  1844. }
  1845. }
  1846. }
  1847. // process chunkmap:
  1848. if(have_samples)
  1849. {
  1850. stsc_t const* stsc = trak->mdia_->minf_->stbl_->stsc_;
  1851. unsigned int last = trak->chunks_size_;
  1852. unsigned int i = stsc->entries_;
  1853. while(i > 0)
  1854. {
  1855. unsigned int j;
  1856. --i;
  1857. for(j = stsc->table_[i].chunk_; j < last; j++)
  1858. {
  1859. trak->chunks_[j].id_ = stsc->table_[i].id_;
  1860. trak->chunks_[j].size_ = stsc->table_[i].samples_;
  1861. }
  1862. last = stsc->table_[i].chunk_;
  1863. }
  1864. }
  1865. // calc pts of chunks:
  1866. if(have_samples)
  1867. {
  1868. stsz_t const* stsz = trak->mdia_->minf_->stbl_->stsz_;
  1869. unsigned int sample_size = stsz->sample_size_;
  1870. unsigned int s = 0;
  1871. {
  1872. unsigned int j;
  1873. for(j = 0; j < trak->chunks_size_; j++)
  1874. {
  1875. trak->chunks_[j].sample_ = s;
  1876. s += trak->chunks_[j].size_;
  1877. }
  1878. }
  1879. stco_samples = s;
  1880. if(sample_size == 0)
  1881. {
  1882. trak->samples_size_ = stsz->entries_;
  1883. }
  1884. else
  1885. {
  1886. trak->samples_size_ = s;
  1887. }
  1888. // reserve one extra for the end information (like pts and cto).
  1889. trak->samples_ = (samples_t*)calloc(trak->samples_size_ + 1, sizeof(samples_t));
  1890. if(sample_size == 0)
  1891. {
  1892. unsigned int i;
  1893. for(i = 0; i != trak->samples_size_ ; ++i)
  1894. trak->samples_[i].size_ = stsz->sample_sizes_[i];
  1895. }
  1896. else
  1897. {
  1898. unsigned int i;
  1899. for(i = 0; i != trak->samples_size_ ; ++i)
  1900. trak->samples_[i].size_ = sample_size;
  1901. }
  1902. }
  1903. // calc pts:
  1904. if(have_samples)
  1905. {
  1906. stts_t const* stts = trak->mdia_->minf_->stbl_->stts_;
  1907. unsigned int s = 0;
  1908. uint64_t pts = 0;
  1909. unsigned int entries = stts->entries_;
  1910. unsigned int j;
  1911. for(j = 0; j < entries; j++)
  1912. {
  1913. unsigned int i;
  1914. unsigned int sample_count = stts->table_[j].sample_count_;
  1915. unsigned int sample_duration = stts->table_[j].sample_duration_;
  1916. for(i = 0; i < sample_count; i++)
  1917. {
  1918. trak->samples_[s].pts_ = pts;
  1919. ++s;
  1920. pts += sample_duration;
  1921. }
  1922. }
  1923. // write end pts
  1924. trak->samples_[s].pts_ = pts;
  1925. }
  1926. // calc composition times:
  1927. if(have_samples)
  1928. {
  1929. ctts_t const* ctts = trak->mdia_->minf_->stbl_->ctts_;
  1930. if(ctts)
  1931. {
  1932. unsigned int s = 0;
  1933. unsigned int entries = ctts->entries_;
  1934. unsigned int j;
  1935. unsigned int sample_offset = 0;
  1936. for(j = 0; j != entries; j++)
  1937. {
  1938. unsigned int i;
  1939. unsigned int sample_count = ctts->table_[j].sample_count_;
  1940. sample_offset = ctts->table_[j].sample_offset_;
  1941. for(i = 0; i < sample_count; i++)
  1942. {
  1943. if(s == trak->samples_size_)
  1944. {
  1945. MP4_WARNING("Warning: ctts_get_samples=%u, should be %u\n",
  1946. ctts_get_samples(ctts), trak->samples_size_);
  1947. break;
  1948. }
  1949. trak->samples_[s].cto_ = sample_offset;
  1950. ++s;
  1951. }
  1952. }
  1953. // write end cto
  1954. trak->samples_[s].cto_ = sample_offset;
  1955. }
  1956. }
  1957. // calc sample offsets
  1958. if(have_samples)
  1959. {
  1960. unsigned int s = 0;
  1961. unsigned int j;
  1962. for(j = 0; j != trak->chunks_size_; j++)
  1963. {
  1964. uint64_t pos = trak->chunks_[j].pos_;
  1965. unsigned int i;
  1966. for(i = 0; i != trak->chunks_[j].size_; i++)
  1967. {
  1968. if(s == trak->samples_size_)
  1969. {
  1970. MP4_WARNING("Warning: stco_get_samples=%u, should be %u\n",
  1971. stco_samples, trak->samples_size_);
  1972. break;
  1973. }
  1974. trak->samples_[s].pos_ = pos;
  1975. pos += trak->samples_[s].size_;
  1976. ++s;
  1977. }
  1978. }
  1979. }
  1980. if(have_samples)
  1981. {
  1982. stss_t const* stss = trak->mdia_->minf_->stbl_->stss_;
  1983. unsigned int i;
  1984. if(stss)
  1985. {
  1986. // TODO: The chunks for smooth streaming are now aligned to the keyframes.
  1987. // We may want to consider skipping some keyframes and use a
  1988. // minimal_increment_between_keyframes (say 2 seconds) as some chunks
  1989. // can be very small.
  1990. for(i = 0; i != stss->entries_; ++i)
  1991. {
  1992. uint32_t s = stss->sample_numbers_[i] - 1;
  1993. trak->samples_[s].is_ss_ = 1;
  1994. trak->samples_[s].is_smooth_ss_ = 1;
  1995. }
  1996. }
  1997. else
  1998. {
  1999. for(i = 0; i != trak->samples_size_; ++i)
  2000. {
  2001. trak->samples_[i].is_ss_ = 1;
  2002. }
  2003. }
  2004. // write end ss
  2005. trak->samples_[trak->samples_size_].is_ss_ = 1;
  2006. trak->samples_[trak->samples_size_].is_smooth_ss_ = 1;
  2007. }
  2008. return 1;
  2009. }
  2010. static void copy_sync_samples_to_audio_track(trak_t* video, trak_t* audio)
  2011. {
  2012. if(video)
  2013. {
  2014. samples_t* first = video->samples_;
  2015. samples_t* last = video->samples_ + video->samples_size_;
  2016. samples_t* audio_first = audio->samples_;
  2017. samples_t* audio_last = audio->samples_ + audio->samples_size_;
  2018. while(first != last)
  2019. {
  2020. if(first->is_smooth_ss_)
  2021. {
  2022. uint64_t pts = trak_time_to_moov_time(first->pts_,
  2023. audio->mdia_->mdhd_->timescale_, video->mdia_->mdhd_->timescale_);
  2024. while(audio_first != audio_last)
  2025. {
  2026. if(audio_first->pts_ >= pts)
  2027. {
  2028. audio_first->is_smooth_ss_ = 1;
  2029. break;
  2030. }
  2031. ++audio_first;
  2032. }
  2033. }
  2034. ++first;
  2035. }
  2036. }
  2037. else
  2038. {
  2039. // if there is no video track and we don't have sync samples, then insert
  2040. // smooth sync samples every 2 seconds
  2041. samples_t* f = audio->samples_;
  2042. samples_t* l = audio->samples_ + audio->samples_size_;
  2043. uint64_t pts = 0;
  2044. uint64_t increment = 2 * audio->mdia_->mdhd_->timescale_;
  2045. while(f != l)
  2046. {
  2047. if(f->pts_ >= pts)
  2048. {
  2049. f->is_smooth_ss_ = 1;
  2050. pts += increment;
  2051. }
  2052. ++f;
  2053. }
  2054. }
  2055. }
  2056. extern int moov_build_index(struct mp4_context_t const* mp4_context,
  2057. struct moov_t* moov)
  2058. {
  2059. // Build the track index
  2060. trak_t* audio_trak = NULL;
  2061. trak_t* video_trak = NULL;
  2062. unsigned int track;
  2063. // already indexed?
  2064. if(moov->is_indexed_)
  2065. {
  2066. return 1;
  2067. }
  2068. moov->is_indexed_ = 1;
  2069. for(track = 0; track != moov->tracks_; ++track)
  2070. {
  2071. trak_t* trak = moov->traks_[track];
  2072. switch(trak->mdia_->hdlr_->handler_type_)
  2073. {
  2074. case FOURCC('s', 'o', 'u', 'n'):
  2075. audio_trak = trak;
  2076. break;
  2077. case FOURCC('v', 'i', 'd', 'e'):
  2078. video_trak = trak;
  2079. break;
  2080. }
  2081. if(!trak_build_index(mp4_context, trak))
  2082. {
  2083. return 0;
  2084. }
  2085. }
  2086. // Copy the sync sample markers for smooth streaming from the video trak
  2087. // to the audio trak in case the audio track doesn't have an 'stss'.
  2088. if(!moov->mvex_ && audio_trak && !audio_trak->mdia_->minf_->stbl_->stss_)
  2089. {
  2090. copy_sync_samples_to_audio_track(video_trak, audio_trak);
  2091. }
  2092. return 1;
  2093. }
  2094. static void* tfra_read(struct mp4_context_t const* UNUSED(mp4_context),
  2095. void* UNUSED(parent),
  2096. unsigned char* buffer, uint64_t UNUSED(size))
  2097. {
  2098. unsigned int i;
  2099. unsigned int length_fields;
  2100. tfra_t* tfra = tfra_init();
  2101. tfra->version_ = read_8(buffer + 0);
  2102. tfra->flags_ = read_24(buffer + 1);
  2103. tfra->track_id_ = read_32(buffer + 4);
  2104. length_fields = read_32(buffer + 8);
  2105. tfra->length_size_of_traf_num_ = (((length_fields >> 4) & 3) + 1);
  2106. tfra->length_size_of_trun_num_ = (((length_fields >> 2) & 3) + 1);
  2107. tfra->length_size_of_sample_num_ = (((length_fields >> 0) & 3) + 1);
  2108. tfra->number_of_entry_ = read_32(buffer + 12);
  2109. tfra->table_ = (tfra_table_t*)malloc(tfra->number_of_entry_ * sizeof(tfra_table_t));
  2110. buffer += 16;
  2111. for(i = 0; i != tfra->number_of_entry_; ++i)
  2112. {
  2113. if(tfra->version_ == 0)
  2114. {
  2115. tfra->table_[i].time_ = read_32(buffer + 0);
  2116. tfra->table_[i].moof_offset_ = read_32(buffer + 4);
  2117. buffer += 8;
  2118. }
  2119. else
  2120. {
  2121. tfra->table_[i].time_ = read_64(buffer + 0);
  2122. tfra->table_[i].moof_offset_ = read_64(buffer + 8);
  2123. buffer += 16;
  2124. }
  2125. tfra->table_[i].traf_number_ =
  2126. read_n(buffer, tfra->length_size_of_traf_num_ * 8) - 1;
  2127. buffer += tfra->length_size_of_traf_num_;
  2128. tfra->table_[i].trun_number_ =
  2129. read_n(buffer, tfra->length_size_of_trun_num_ * 8) - 1;
  2130. buffer += tfra->length_size_of_trun_num_;
  2131. tfra->table_[i].sample_number_ =
  2132. read_n(buffer, tfra->length_size_of_sample_num_ * 8) - 1;
  2133. buffer += tfra->length_size_of_sample_num_ ;
  2134. }
  2135. return tfra;
  2136. }
  2137. static int mfra_add_tfra(struct mp4_context_t const* UNUSED(mp4_context),
  2138. void* parent, void* child)
  2139. {
  2140. mfra_t* mfra = (mfra_t*)parent;
  2141. tfra_t* tfra = (tfra_t*)child;
  2142. if(mfra->tracks_ == MAX_TRACKS)
  2143. {
  2144. mfra_exit(mfra);
  2145. return 0;
  2146. }
  2147. mfra->tfras_[mfra->tracks_] = tfra;
  2148. ++mfra->tracks_;
  2149. return 1;
  2150. }
  2151. extern void* mfra_read(struct mp4_context_t const* mp4_context,
  2152. void* UNUSED(parent),
  2153. unsigned char* buffer, uint64_t size)
  2154. {
  2155. mfra_t* atom = mfra_init();
  2156. struct atom_read_list_t atom_read_list[] = {
  2157. { FOURCC('t', 'f', 'r', 'a'), &mfra_add_tfra, &tfra_read },
  2158. };
  2159. int result = atom_reader(mp4_context,
  2160. atom_read_list,
  2161. sizeof(atom_read_list) / sizeof(atom_read_list[0]),
  2162. atom,
  2163. buffer, size);
  2164. if(!result)
  2165. {
  2166. mfra_exit(atom);
  2167. return 0;
  2168. }
  2169. return atom;
  2170. }
  2171. // End Of File