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.

620 lines
18 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*******************************************************************************
  2. output_mp4.c - A library for writing MPEG4.
  3. Copyright (C) 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 "output_mp4.h"
  15. #include "mp4_io.h"
  16. #include "mp4_writer.h"
  17. #include "moov.h"
  18. #include "output_bucket.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/types.h> // FreeBSD doesn't define off_t in stdio.h
  22. #if defined HAVE_ZLIB_H && defined HAVE_LIBZ
  23. // Compress the MOOV atom. Turn this off for Flash as it doesn't support it.
  24. // # define COMPRESS_MOOV_ATOM
  25. # include <zlib.h>
  26. #endif
  27. // traffic shaping: create offsets for each second
  28. static void create_traffic_shaping(moov_t* moov,
  29. unsigned int const* trak_sample_start,
  30. unsigned int const* trak_sample_end,
  31. int64_t offset,
  32. mp4_split_options_t* options)
  33. {
  34. uint64_t moov_duration = moov->mvhd_->duration_;
  35. long moov_time_scale = moov->mvhd_->timescale_;
  36. unsigned int track_index;
  37. int second;
  38. options->seconds =
  39. 1 + (int)((moov_duration + moov_time_scale - 1) / moov_time_scale);
  40. options->byte_offsets = (uint64_t*)
  41. calloc(options->seconds, sizeof(options->byte_offsets[0]));
  42. for(track_index = 0; track_index != moov->tracks_; ++track_index)
  43. {
  44. struct trak_t* trak = moov->traks_[track_index];
  45. long trak_time_scale = trak->mdia_->mdhd_->timescale_;
  46. struct samples_t* samples = trak->samples_;
  47. unsigned int sample = trak_sample_start[track_index];
  48. unsigned int end_sample = trak_sample_end[track_index];
  49. uint64_t pts = samples[sample].pts_;
  50. second = 0;
  51. while(sample != end_sample)
  52. {
  53. uint64_t trak_end_offset = 0;
  54. while(sample != end_sample && samples[sample].pts_ <= pts)
  55. {
  56. trak_end_offset = samples[sample].pos_;
  57. trak_end_offset += samples[sample].size_;
  58. trak_end_offset += offset;
  59. ++sample;
  60. }
  61. #if 0
  62. if(second < 20)
  63. {
  64. MP4_INFO("moov[%d]: offset=%"PRIu64"\n", second, trak_end_offset);
  65. }
  66. #endif
  67. if(options->byte_offsets[second] < trak_end_offset)
  68. options->byte_offsets[second] = trak_end_offset;
  69. pts += trak_time_scale; // next second
  70. ++second;
  71. }
  72. }
  73. }
  74. static void stco_shift_offsets_inplace(unsigned char* stco, int offset)
  75. {
  76. unsigned int entries = read_32(stco + 4);
  77. unsigned int* table = (unsigned int*)(stco + 8);
  78. unsigned int i;
  79. for(i = 0; i != entries; ++i)
  80. write_32((unsigned char*)&table[i], (read_32((unsigned char*)&table[i]) + offset));
  81. }
  82. static void trak_shift_offsets_inplace(struct trak_t* trak, int64_t offset)
  83. {
  84. // void* stco = trak->mdia_->minf_->stbl_->stco_inplace_;
  85. void* stco = trak->mdia_->minf_->stbl_->stco_->stco_inplace_;
  86. stco_shift_offsets_inplace((unsigned char*)stco, (int32_t)offset);
  87. }
  88. static void moov_shift_offsets_inplace(struct moov_t* moov, int64_t offset)
  89. {
  90. unsigned int i;
  91. for(i = 0; i != moov->tracks_; ++i)
  92. {
  93. trak_shift_offsets_inplace(moov->traks_[i], offset);
  94. }
  95. }
  96. #ifdef COMPRESS_MOOV_ATOM
  97. static void compress_moov(struct mp4_context_t* mp4_context,
  98. struct moov_t* moov,
  99. unsigned char* moov_data,
  100. uint64_t* moov_size)
  101. {
  102. uLong sourceLen = (uLong)(*moov_size - ATOM_PREAMBLE_SIZE);
  103. uLong destLen = compressBound(sourceLen);
  104. unsigned char* cmov = (unsigned char*)malloc(destLen);
  105. int zstatus = compress(cmov, &destLen, moov_data, sourceLen);
  106. if(zstatus == Z_OK)
  107. {
  108. MP4_INFO("cmov size = %lu (%ld%%)\n", destLen, 100 * destLen / sourceLen);
  109. }
  110. {
  111. const int extra_space = 4096;
  112. if(destLen + extra_space < sourceLen)
  113. {
  114. const int bytes_saved = sourceLen - destLen;
  115. uLong destLen2;
  116. int extra = 0;
  117. MP4_INFO("shifting offsets by %d\n", -bytes_saved);
  118. moov_shift_offsets_inplace(moov, -bytes_saved);
  119. extra += ATOM_PREAMBLE_SIZE + 4; // dcom
  120. extra += ATOM_PREAMBLE_SIZE + 4; // cmvd
  121. extra += ATOM_PREAMBLE_SIZE; // cmov
  122. extra += ATOM_PREAMBLE_SIZE + extra_space; // free
  123. MP4_INFO("shifting offsets by %d\n", extra);
  124. moov_shift_offsets_inplace(moov, extra);
  125. // recompress
  126. destLen2 = compressBound(sourceLen);
  127. zstatus = compress(cmov, &destLen2, moov_data, sourceLen);
  128. if(zstatus == Z_OK)
  129. {
  130. MP4_INFO("cmov size = %lu (%ld%%)\n", destLen2, 100 * destLen2 / sourceLen);
  131. if(destLen2 < destLen + extra_space)
  132. {
  133. // copy compressed movie atom
  134. unsigned char* outbuffer = moov_data;
  135. uint32_t dcom_size = ATOM_PREAMBLE_SIZE + 4;
  136. uint32_t cmvd_size = ATOM_PREAMBLE_SIZE + 4 + destLen2;
  137. uint32_t cmov_size = ATOM_PREAMBLE_SIZE + dcom_size + cmvd_size;
  138. uint32_t free_size = ATOM_PREAMBLE_SIZE + extra_space + destLen - destLen2;
  139. *moov_size = ATOM_PREAMBLE_SIZE + cmov_size + free_size;
  140. outbuffer = write_32(outbuffer, (uint32_t)*moov_size);
  141. // skip 'moov'
  142. outbuffer += 4;
  143. outbuffer = write_32(outbuffer, cmov_size);
  144. {
  145. outbuffer = write_32(outbuffer, FOURCC('c', 'm', 'o', 'v'));
  146. outbuffer = write_32(outbuffer, dcom_size);
  147. outbuffer = write_32(outbuffer, FOURCC('d', 'c', 'o', 'm'));
  148. outbuffer = write_32(outbuffer, FOURCC('z', 'l', 'i', 'b'));
  149. outbuffer = write_32(outbuffer, cmvd_size);
  150. {
  151. outbuffer = write_32(outbuffer, FOURCC('c', 'm', 'v', 'd'));
  152. outbuffer = write_32(outbuffer, sourceLen);
  153. memcpy(outbuffer, cmov, destLen2);
  154. outbuffer += destLen2;
  155. }
  156. }
  157. // add final padding
  158. outbuffer = write_32(outbuffer, free_size);
  159. outbuffer = write_32(outbuffer, FOURCC('f', 'r', 'e', 'e'));
  160. {
  161. const char free_bytes[8] =
  162. {
  163. 'C', 'o', 'd', 'e','S','h', 'o', 'p'
  164. };
  165. uint32_t padding_index;
  166. for(padding_index = ATOM_PREAMBLE_SIZE; padding_index != free_size; ++padding_index)
  167. {
  168. outbuffer[padding_index] = free_bytes[padding_index % 8];
  169. }
  170. }
  171. }
  172. else
  173. {
  174. MP4_ERROR("%s", "2nd pass compress overflow\n");
  175. }
  176. }
  177. }
  178. }
  179. free(cmov);
  180. }
  181. #endif
  182. static void trak_update_index(struct mp4_context_t const* mp4_context,
  183. struct trak_t* trak,
  184. unsigned int start, unsigned int end)
  185. {
  186. // write samples [start,end>
  187. // stts = [entries * [sample_count, sample_duration]
  188. {
  189. struct stts_t* stts = trak->mdia_->minf_->stbl_->stts_;
  190. unsigned int entries = 0;
  191. unsigned int s = start;
  192. while(s != end)
  193. {
  194. unsigned int sample_count = 1;
  195. unsigned int sample_duration =
  196. (unsigned int)(trak->samples_[s + 1].pts_ - trak->samples_[s].pts_);
  197. while(++s != end)
  198. {
  199. if((trak->samples_[s + 1].pts_ - trak->samples_[s].pts_) != sample_duration)
  200. break;
  201. ++sample_count;
  202. }
  203. // TODO: entries may be empty when we read a fragmented movie file. use
  204. // output_mov() instead.
  205. // if(entries + 1 > stts->entries_)
  206. // {
  207. // stts->table_ = (stts_table_t*)
  208. // realloc(stts->table_, (entries + 1) * sizeof(stts_table_t));
  209. // }
  210. stts->table_[entries].sample_count_ = sample_count;
  211. stts->table_[entries].sample_duration_ = sample_duration;
  212. ++entries;
  213. }
  214. stts->entries_ = entries;
  215. if(stts_get_samples(stts) != end - start)
  216. {
  217. MP4_WARNING("ERROR: stts_get_samples=%d, should be %d\n",
  218. stts_get_samples(stts), end - start);
  219. }
  220. }
  221. // ctts = [entries * [sample_count, sample_offset]
  222. {
  223. struct ctts_t* ctts = trak->mdia_->minf_->stbl_->ctts_;
  224. if(ctts)
  225. {
  226. unsigned int entries = 0;
  227. unsigned int s = start;
  228. while(s != end)
  229. {
  230. unsigned int sample_count = 1;
  231. unsigned int sample_offset = trak->samples_[s].cto_;
  232. while(++s != end)
  233. {
  234. if(trak->samples_[s].cto_ != sample_offset)
  235. break;
  236. ++sample_count;
  237. }
  238. // write entry
  239. ctts->table_[entries].sample_count_ = sample_count;
  240. ctts->table_[entries].sample_offset_ = sample_offset;
  241. ++entries;
  242. }
  243. ctts->entries_ = entries;
  244. if(ctts_get_samples(ctts) != end - start)
  245. {
  246. MP4_WARNING("ERROR: ctts_get_samples=%d, should be %d\n",
  247. ctts_get_samples(ctts), end - start);
  248. }
  249. }
  250. }
  251. // process chunkmap:
  252. {
  253. struct stsc_t* stsc = trak->mdia_->minf_->stbl_->stsc_;
  254. if(stsc != NULL)
  255. {
  256. unsigned int i;
  257. for(i = 0; i != trak->chunks_size_; ++i)
  258. {
  259. if(trak->chunks_[i].sample_ + trak->chunks_[i].size_ > start)
  260. break;
  261. }
  262. {
  263. unsigned int stsc_entries = 0;
  264. unsigned int chunk_start = i;
  265. unsigned int chunk_end;
  266. // problem.mp4: reported by Jin-seok Lee. Second track contains no samples
  267. if(trak->chunks_size_ != 0)
  268. {
  269. unsigned int samples =
  270. trak->chunks_[i].sample_ + trak->chunks_[i].size_ - start;
  271. unsigned int id = trak->chunks_[i].id_;
  272. // write entry [chunk,samples,id]
  273. stsc->table_[stsc_entries].chunk_ = 0;
  274. stsc->table_[stsc_entries].samples_ = samples;
  275. stsc->table_[stsc_entries].id_ = id;
  276. ++stsc_entries;
  277. if(i != trak->chunks_size_)
  278. {
  279. for(i += 1; i != trak->chunks_size_; ++i)
  280. {
  281. unsigned int next_size = trak->chunks_[i].size_;
  282. if(trak->chunks_[i].sample_ + trak->chunks_[i].size_ > end)
  283. {
  284. next_size = end - trak->chunks_[i].sample_;
  285. }
  286. if(next_size != samples)
  287. {
  288. samples = next_size;
  289. id = trak->chunks_[i].id_;
  290. stsc->table_[stsc_entries].chunk_ = i - chunk_start;
  291. stsc->table_[stsc_entries].samples_ = samples;
  292. stsc->table_[stsc_entries].id_ = id;
  293. ++stsc_entries;
  294. }
  295. if(trak->chunks_[i].sample_ + next_size == end)
  296. {
  297. break;
  298. }
  299. }
  300. }
  301. }
  302. chunk_end = i + 1;
  303. stsc->entries_ = stsc_entries;
  304. {
  305. struct stco_t* stco = trak->mdia_->minf_->stbl_->stco_;
  306. unsigned int entries = 0;
  307. for(i = chunk_start; i != chunk_end; ++i)
  308. {
  309. stco->chunk_offsets_[entries] = stco->chunk_offsets_[i];
  310. ++entries;
  311. }
  312. stco->entries_ = entries;
  313. // patch first chunk with correct sample offset
  314. stco->chunk_offsets_[0] = (uint32_t)trak->samples_[start].pos_;
  315. }
  316. }
  317. }
  318. }
  319. // process sync samples:
  320. if(trak->mdia_->minf_->stbl_->stss_)
  321. {
  322. struct stss_t* stss = trak->mdia_->minf_->stbl_->stss_;
  323. unsigned int entries = 0;
  324. unsigned int stss_start;
  325. unsigned int i;
  326. for(i = 0; i != stss->entries_; ++i)
  327. {
  328. if(stss->sample_numbers_[i] >= start + 1)
  329. break;
  330. }
  331. stss_start = i;
  332. for(; i != stss->entries_; ++i)
  333. {
  334. unsigned int sync_sample = stss->sample_numbers_[i];
  335. if(sync_sample >= end + 1)
  336. break;
  337. stss->sample_numbers_[entries] = sync_sample - start;
  338. ++entries;
  339. }
  340. stss->entries_ = entries;
  341. }
  342. // process sample sizes
  343. {
  344. struct stsz_t* stsz = trak->mdia_->minf_->stbl_->stsz_;
  345. if(stsz != NULL)
  346. {
  347. if(stsz->sample_size_ == 0)
  348. {
  349. unsigned int entries = 0;
  350. unsigned int i;
  351. for(i = start; i != end; ++i)
  352. {
  353. stsz->sample_sizes_[entries] = stsz->sample_sizes_[i];
  354. ++entries;
  355. }
  356. }
  357. stsz->entries_ = end - start;
  358. }
  359. }
  360. }
  361. extern int output_mp4(struct mp4_context_t* mp4_context,
  362. unsigned int const* trak_sample_start,
  363. unsigned int const* trak_sample_end,
  364. struct bucket_t** buckets,
  365. struct mp4_split_options_t* options)
  366. {
  367. unsigned int i;
  368. uint64_t mdat_start = mp4_context->mdat_atom.start_;
  369. uint64_t mdat_size = mp4_context->mdat_atom.size_;
  370. int64_t offset;
  371. struct moov_t* moov = mp4_context->moov;
  372. // unsigned char* moov_data = mp4_context->moov_data;
  373. unsigned char* moov_data = (unsigned char*)
  374. malloc((size_t)mp4_context->moov_atom.size_ + ATOM_PREAMBLE_SIZE + 1024);
  375. uint64_t moov_size;
  376. long moov_time_scale = moov->mvhd_->timescale_;
  377. uint64_t skip_from_start = UINT64_MAX;
  378. uint64_t end_offset = 0;
  379. uint64_t moov_duration = 0;
  380. #if 1
  381. uint64_t new_mdat_start = 0;
  382. {
  383. static char const free_data[] = {
  384. 0x0, 0x0, 0x0, 42, 'f', 'r', 'e', 'e',
  385. 'v', 'i', 'd', 'e', 'o', ' ', 's', 'e',
  386. 'r', 'v', 'e', 'd', ' ', 'b', 'y', ' ',
  387. 'm', 'o', 'd', '_', 'h', '2', '6', '4',
  388. '_', 's', 't', 'r', 'e', 'a', 'm', 'i',
  389. 'n', 'g'
  390. };
  391. uint32_t size_of_header = (uint32_t)mp4_context->ftyp_atom.size_ +
  392. sizeof(free_data);
  393. unsigned char* buffer = (unsigned char*)malloc(size_of_header);
  394. if(mp4_context->ftyp_atom.size_)
  395. {
  396. fseeko(mp4_context->infile, mp4_context->ftyp_atom.start_, SEEK_SET);
  397. if(fread(buffer, (off_t)mp4_context->ftyp_atom.size_, 1, mp4_context->infile) != 1)
  398. {
  399. MP4_ERROR("%s", "Error reading ftyp atom\n");
  400. free(buffer);
  401. return 0;
  402. }
  403. }
  404. // copy free data
  405. memcpy(buffer + mp4_context->ftyp_atom.size_, free_data,
  406. sizeof(free_data));
  407. if(options->output_format == OUTPUT_FORMAT_MP4)
  408. {
  409. bucket_t* bucket = bucket_init_memory(buffer, size_of_header);
  410. bucket_insert_tail(buckets, bucket);
  411. }
  412. free(buffer);
  413. new_mdat_start += size_of_header;
  414. }
  415. // new_mdat_start += mp4_context->moov_atom.size_;
  416. #endif
  417. offset = new_mdat_start - mp4_context->mdat_atom.start_;
  418. // subtract old moov size
  419. // offset -= mp4_context->moov_atom.size_;
  420. for(i = 0; i != moov->tracks_; ++i)
  421. {
  422. struct trak_t* trak = moov->traks_[i];
  423. struct stbl_t* stbl = trak->mdia_->minf_->stbl_;
  424. unsigned int start_sample = trak_sample_start[i];
  425. unsigned int end_sample = trak_sample_end[i];
  426. trak_update_index(mp4_context, trak, start_sample, end_sample);
  427. if(trak->samples_size_ == 0)
  428. {
  429. MP4_WARNING("Trak %u contains no samples. Maybe a fragmented file?", i);
  430. return 1;
  431. }
  432. {
  433. uint64_t skip =
  434. trak->samples_[start_sample].pos_ - trak->samples_[0].pos_;
  435. if(skip < skip_from_start)
  436. skip_from_start = skip;
  437. MP4_INFO("Trak can skip %" PRIu64 " bytes\n", skip);
  438. if(end_sample != trak->samples_size_)
  439. {
  440. uint64_t end_pos = trak->samples_[end_sample].pos_;
  441. if(end_pos > end_offset)
  442. end_offset = end_pos;
  443. MP4_INFO("New endpos=%" PRIu64 "\n", end_pos);
  444. MP4_INFO("Trak can skip %" PRIu64 " bytes at end\n",
  445. mdat_start + mdat_size - end_offset);
  446. }
  447. }
  448. {
  449. // fixup trak (duration)
  450. uint64_t trak_duration = stts_get_duration(stbl->stts_);
  451. long trak_time_scale = trak->mdia_->mdhd_->timescale_;
  452. {
  453. uint64_t duration = trak_time_to_moov_time(trak_duration,
  454. moov_time_scale, trak_time_scale);
  455. trak->mdia_->mdhd_->duration_= trak_duration;
  456. trak->tkhd_->duration_ = duration;
  457. MP4_INFO("trak: new_duration=%" PRIu64 "\n", duration);
  458. if(duration > moov_duration)
  459. moov_duration = duration;
  460. }
  461. }
  462. // MP4_INFO("stco.size=%d, ", read_int32(stbl->stco_ + 4));
  463. // MP4_INFO("stts.size=%d samples=%d\n", read_int32(stbl->stts_ + 4), stts_get_samples(stbl->stts_));
  464. // MP4_INFO("stsz.size=%d\n", read_int32(stbl->stsz_ + 8));
  465. // MP4_INFO("stsc.samples=%d\n", stsc_get_samples(stbl->stsc_));
  466. }
  467. moov->mvhd_->duration_ = moov_duration;
  468. MP4_INFO("moov: new_duration=%.2f seconds\n", moov_duration / (float)moov_time_scale);
  469. // subtract bytes we skip at the front of the mdat atom
  470. offset -= skip_from_start;
  471. MP4_INFO("%s", "moov: writing header\n");
  472. moov_write(moov, moov_data);
  473. moov_size = read_32(moov_data);
  474. // add new moov size
  475. offset += moov_size;
  476. MP4_INFO("shifting offsets by %" PRId64 "\n", offset);
  477. moov_shift_offsets_inplace(moov, offset);
  478. // traffic shaping: create offsets for each second
  479. create_traffic_shaping(moov,
  480. trak_sample_start,
  481. trak_sample_end,
  482. offset,
  483. options);
  484. #ifdef COMPRESS_MOOV_ATOM
  485. if(!options->client_is_flash)
  486. {
  487. compress_moov(mp4_context, moov, moov_data, &moov_size);
  488. }
  489. #endif
  490. if(end_offset != 0)
  491. {
  492. MP4_INFO("mdat_size=%" PRId64 " end_offset=%" PRId64 "\n",
  493. mdat_size, end_offset);
  494. mdat_size = end_offset - mdat_start;
  495. }
  496. mdat_start += skip_from_start;
  497. mdat_size -= skip_from_start;
  498. MP4_INFO("mdat_bucket(%" PRId64 ", %" PRId64 ")\n", mdat_start, mdat_size);
  499. bucket_insert_tail(buckets, bucket_init_memory(moov_data, moov_size));
  500. free(moov_data);
  501. {
  502. struct mp4_atom_t mdat_atom;
  503. mdat_atom.type_ = FOURCC('m', 'd', 'a', 't');
  504. mdat_atom.short_size_ = 0; // TODO: use original small/wide mdat box
  505. if(options->adaptive)
  506. {
  507. // empty mdat atom
  508. mdat_atom.size_ = ATOM_PREAMBLE_SIZE;
  509. }
  510. else
  511. {
  512. mdat_atom.size_ = mdat_size;
  513. }
  514. {
  515. unsigned char buffer[32];
  516. int mdat_header_size = mp4_atom_write_header(buffer, &mdat_atom);
  517. bucket_insert_tail(buckets,
  518. bucket_init_memory(buffer, mdat_header_size));
  519. if(mdat_atom.size_ - mdat_header_size)
  520. {
  521. bucket_insert_tail(buckets,
  522. bucket_init_file(mdat_start + mdat_header_size,
  523. mdat_atom.size_ - mdat_header_size));
  524. }
  525. }
  526. }
  527. return 1;
  528. }
  529. // End Of File