Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

8 месяцев назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "mov-writer.h"
  2. #include "mov-internal.h"
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <errno.h>
  7. #include <time.h>
  8. struct mov_writer_t
  9. {
  10. struct mov_t mov;
  11. uint64_t mdat_size;
  12. uint64_t mdat_offset;
  13. };
  14. static int mov_write_tail(struct mov_t* mov)
  15. {
  16. mov_buffer_w32(&mov->io, 8 + strlen(MOV_APP)); /* size */
  17. mov_buffer_write(&mov->io, "free", 4);
  18. mov_buffer_write(&mov->io, MOV_APP, strlen(MOV_APP));
  19. return 0;
  20. }
  21. static size_t mov_write_moov(struct mov_t* mov)
  22. {
  23. int i;
  24. size_t size;
  25. uint64_t offset;
  26. size = 8 /* Box */;
  27. offset = mov_buffer_tell(&mov->io);
  28. mov_buffer_w32(&mov->io, 0); /* size */
  29. mov_buffer_write(&mov->io, "moov", 4);
  30. size += mov_write_mvhd(mov);
  31. // size += mov_write_iods(mov);
  32. for(i = 0; i < mov->track_count; i++)
  33. {
  34. mov->track = mov->tracks + i;
  35. if (mov->track->sample_count < 1)
  36. continue;
  37. size += mov_write_trak(mov);
  38. }
  39. size += mov_write_udta(mov);
  40. mov_write_size(mov, offset, size); /* update size */
  41. return size;
  42. }
  43. void mov_write_size(const struct mov_t* mov, uint64_t offset, size_t size)
  44. {
  45. uint64_t offset2;
  46. assert(size < UINT32_MAX);
  47. offset2 = mov_buffer_tell(&mov->io);
  48. mov_buffer_seek(&mov->io, offset);
  49. mov_buffer_w32(&mov->io, (uint32_t)size);
  50. mov_buffer_seek(&mov->io, offset2);
  51. }
  52. static int mov_writer_init(struct mov_t* mov)
  53. {
  54. mov->ftyp.major_brand = MOV_BRAND_ISOM;
  55. mov->ftyp.minor_version = 0x200;
  56. mov->ftyp.brands_count = 4;
  57. mov->ftyp.compatible_brands[0] = MOV_BRAND_ISOM;
  58. mov->ftyp.compatible_brands[1] = MOV_BRAND_ISO2;
  59. mov->ftyp.compatible_brands[2] = MOV_BRAND_AVC1;
  60. mov->ftyp.compatible_brands[3] = MOV_BRAND_MP41;
  61. mov->header = 0;
  62. return 0;
  63. }
  64. struct mov_writer_t* mov_writer_create(const struct mov_buffer_t* buffer, void* param, int flags)
  65. {
  66. struct mov_t* mov;
  67. struct mov_writer_t* writer;
  68. writer = (struct mov_writer_t*)calloc(1, sizeof(struct mov_writer_t));
  69. if (NULL == writer)
  70. return NULL;
  71. mov = &writer->mov;
  72. mov->flags = flags;
  73. mov->io.param = param;
  74. memcpy(&mov->io.io, buffer, sizeof(mov->io.io));
  75. mov->mvhd.next_track_ID = 1;
  76. mov->mvhd.creation_time = time(NULL) + 0x7C25B080; // 1970 based -> 1904 based;
  77. mov->mvhd.modification_time = mov->mvhd.creation_time;
  78. mov->mvhd.timescale = 1000;
  79. mov->mvhd.duration = 0; // placeholder
  80. mov_writer_init(mov);
  81. mov_write_ftyp(mov);
  82. // free(reserved for 64bit mdat)
  83. mov_buffer_w32(&mov->io, 8); /* size */
  84. mov_buffer_write(&mov->io, "free", 4);
  85. // mdat
  86. writer->mdat_offset = mov_buffer_tell(&mov->io);
  87. mov_buffer_w32(&mov->io, 0); /* size */
  88. mov_buffer_write(&mov->io, "mdat", 4);
  89. return writer;
  90. }
  91. static int mov_writer_move(struct mov_t* mov, uint64_t to, uint64_t from, size_t bytes);
  92. void mov_writer_destroy(struct mov_writer_t* writer)
  93. {
  94. int i;
  95. uint64_t offset, offset2;
  96. struct mov_t* mov;
  97. struct mov_track_t* track;
  98. mov = &writer->mov;
  99. // finish mdat box
  100. if (writer->mdat_size + 8 <= UINT32_MAX)
  101. {
  102. mov_write_size(mov, writer->mdat_offset, (uint32_t)(writer->mdat_size + 8)); /* update size */
  103. }
  104. else
  105. {
  106. offset2 = mov_buffer_tell(&mov->io);
  107. writer->mdat_offset -= 8; // overwrite free box
  108. mov_buffer_seek(&mov->io, writer->mdat_offset);
  109. mov_buffer_w32(&mov->io, 1);
  110. mov_buffer_write(&mov->io, "mdat", 4);
  111. mov_buffer_w64(&mov->io, writer->mdat_size + 16);
  112. mov_buffer_seek(&mov->io, offset2);
  113. }
  114. // finish sample info
  115. for (i = 0; i < mov->track_count; i++)
  116. {
  117. track = &mov->tracks[i];
  118. if(track->sample_count < 1)
  119. continue;
  120. // pts in ms
  121. track->mdhd.duration = (track->samples[track->sample_count - 1].dts - track->samples[0].dts);
  122. if (track->sample_count > 1)
  123. {
  124. // duration += 3/4 * avg-duration + 1/4 * last-frame-duration
  125. track->mdhd.duration += track->mdhd.duration * 3 / (track->sample_count - 1) / 4 + (track->samples[track->sample_count - 1].dts - track->samples[track->sample_count - 2].dts) / 4;
  126. }
  127. //track->mdhd.duration = track->mdhd.duration * track->mdhd.timescale / 1000;
  128. track->tkhd.duration = track->mdhd.duration * mov->mvhd.timescale / track->mdhd.timescale;
  129. if (track->tkhd.duration > mov->mvhd.duration)
  130. mov->mvhd.duration = track->tkhd.duration; // maximum track duration
  131. }
  132. // write moov box
  133. offset = mov_buffer_tell(&mov->io);
  134. mov_write_moov(mov);
  135. offset2 = mov_buffer_tell(&mov->io);
  136. if (MOV_FLAG_FASTSTART & mov->flags)
  137. {
  138. // check stco -> co64
  139. uint64_t co64 = 0;
  140. for (i = 0; i < mov->track_count; i++)
  141. {
  142. co64 += mov_stco_size(&mov->tracks[i], offset2 - offset);
  143. }
  144. if (co64)
  145. {
  146. uint64_t sz;
  147. do
  148. {
  149. sz = co64;
  150. co64 = 0;
  151. for (i = 0; i < mov->track_count; i++)
  152. {
  153. co64 += mov_stco_size(&mov->tracks[i], offset2 - offset + sz);
  154. }
  155. } while (sz != co64);
  156. }
  157. // rewrite moov
  158. for (i = 0; i < mov->track_count; i++)
  159. mov->tracks[i].offset += (offset2 - offset) + co64;
  160. mov_buffer_seek(&mov->io, offset);
  161. mov_write_moov(mov);
  162. assert(mov_buffer_tell(&mov->io) == offset2 + co64);
  163. offset2 = mov_buffer_tell(&mov->io);
  164. mov_writer_move(mov, writer->mdat_offset, offset, (size_t)(offset2 - offset));
  165. }
  166. mov_write_tail(mov);
  167. for (i = 0; i < mov->track_count; i++)
  168. mov_free_track(mov->tracks + i);
  169. if (mov->tracks)
  170. free(mov->tracks);
  171. free(writer);
  172. }
  173. static int mov_writer_move(struct mov_t* mov, uint64_t to, uint64_t from, size_t bytes)
  174. {
  175. uint8_t* ptr;
  176. uint64_t i, j;
  177. void* buffer[2];
  178. assert(bytes < INT32_MAX);
  179. ptr = malloc((size_t)(bytes * 2));
  180. if (NULL == ptr)
  181. return -ENOMEM;
  182. buffer[0] = ptr;
  183. buffer[1] = ptr + bytes;
  184. mov_buffer_seek(&mov->io, from);
  185. mov_buffer_read(&mov->io, buffer[0], bytes);
  186. mov_buffer_seek(&mov->io, to);
  187. mov_buffer_read(&mov->io, buffer[1], bytes);
  188. j = 0;
  189. for (i = to; i < from; i += bytes)
  190. {
  191. mov_buffer_seek(&mov->io, i);
  192. mov_buffer_write(&mov->io, buffer[j], bytes);
  193. // MSDN: fopen https://msdn.microsoft.com/en-us/library/yeby3zcb.aspx
  194. // When the "r+", "w+", or "a+" access type is specified, both reading and
  195. // writing are enabled (the file is said to be open for "update").
  196. // However, when you switch from reading to writing, the input operation
  197. // must encounter an EOF marker. If there is no EOF, you must use an intervening
  198. // call to a file positioning function. The file positioning functions are
  199. // fsetpos, fseek, and rewind.
  200. // When you switch from writing to reading, you must use an intervening
  201. // call to either fflush or to a file positioning function.
  202. mov_buffer_seek(&mov->io, i+bytes);
  203. mov_buffer_read(&mov->io, buffer[j], bytes);
  204. j ^= 1;
  205. }
  206. mov_buffer_seek(&mov->io, i);
  207. mov_buffer_write(&mov->io, buffer[j], bytes - (size_t)(i - from));
  208. free(ptr);
  209. return mov_buffer_error(&mov->io);
  210. }
  211. int mov_writer_write(struct mov_writer_t* writer, int track, const void* data, size_t bytes, int64_t pts, int64_t dts, int flags)
  212. {
  213. struct mov_t* mov;
  214. struct mov_sample_t* sample;
  215. assert(bytes < UINT32_MAX);
  216. if (track < 0 || track >= (int)writer->mov.track_count)
  217. return -ENOENT;
  218. mov = &writer->mov;
  219. mov->track = &mov->tracks[track];
  220. if (mov->track->sample_count + 1 >= mov->track->sample_offset)
  221. {
  222. void* ptr = realloc(mov->track->samples, sizeof(struct mov_sample_t) * (mov->track->sample_offset + 1024));
  223. if (NULL == ptr) return -ENOMEM;
  224. mov->track->samples = ptr;
  225. mov->track->sample_offset += 1024;
  226. }
  227. pts = pts * mov->track->mdhd.timescale / 1000;
  228. dts = dts * mov->track->mdhd.timescale / 1000;
  229. sample = &mov->track->samples[mov->track->sample_count++];
  230. sample->sample_description_index = 1;
  231. sample->bytes = (uint32_t)bytes;
  232. sample->flags = flags;
  233. sample->data = NULL;
  234. sample->pts = pts;
  235. sample->dts = dts;
  236. sample->offset = mov_buffer_tell(&mov->io);
  237. mov_buffer_write(&mov->io, data, bytes);
  238. if (INT64_MIN == mov->track->start_dts)
  239. mov->track->start_dts = sample->dts;
  240. writer->mdat_size += bytes; // update media data size
  241. return mov_buffer_error(&mov->io);
  242. }
  243. int mov_writer_add_audio(struct mov_writer_t* writer, uint8_t object, int channel_count, int bits_per_sample, int sample_rate, const void* extra_data, size_t extra_data_size)
  244. {
  245. struct mov_t* mov;
  246. struct mov_track_t* track;
  247. mov = &writer->mov;
  248. track = mov_add_track(mov);
  249. if (NULL == track)
  250. return -ENOMEM;
  251. if (0 != mov_add_audio(track, &mov->mvhd, 1000, object, channel_count, bits_per_sample, sample_rate, extra_data, extra_data_size))
  252. return -ENOMEM;
  253. mov->mvhd.next_track_ID++;
  254. return mov->track_count++;
  255. }
  256. int mov_writer_add_video(struct mov_writer_t* writer, uint8_t object, int width, int height, const void* extra_data, size_t extra_data_size)
  257. {
  258. struct mov_t* mov;
  259. struct mov_track_t* track;
  260. mov = &writer->mov;
  261. track = mov_add_track(mov);
  262. if (NULL == track)
  263. return -ENOMEM;
  264. if (0 != mov_add_video(track, &mov->mvhd, 1000, object, width, height, extra_data, extra_data_size))
  265. return -ENOMEM;
  266. mov->mvhd.next_track_ID++;
  267. return mov->track_count++;
  268. }
  269. int mov_writer_add_subtitle(struct mov_writer_t* writer, uint8_t object, const void* extra_data, size_t extra_data_size)
  270. {
  271. struct mov_t* mov;
  272. struct mov_track_t* track;
  273. mov = &writer->mov;
  274. track = mov_add_track(mov);
  275. if (NULL == track)
  276. return -ENOMEM;
  277. if (0 != mov_add_subtitle(track, &mov->mvhd, 1000, object, extra_data, extra_data_size))
  278. return -ENOMEM;
  279. mov->mvhd.next_track_ID++;
  280. return mov->track_count++;
  281. }
  282. int mov_writer_add_udta(mov_writer_t* mov, const void* data, size_t size)
  283. {
  284. mov->mov.udta = data;
  285. mov->mov.udta_size = size;
  286. return 0;
  287. }