mp4.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*****************************************************************************
  2. * mp4.c: mp4 muxer
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 x264 project
  5. *
  6. * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  7. * Loren Merritt <lorenm@u.washington.edu>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  22. *
  23. * This program is also available under a commercial proprietary license.
  24. * For more information, contact us at licensing@x264.com.
  25. *****************************************************************************/
  26. #include "output.h"
  27. #include <gpac/isomedia.h>
  28. #ifdef _WIN32
  29. #include <windows.h>
  30. #endif
  31. typedef struct
  32. {
  33. GF_ISOFile *p_file;
  34. GF_AVCConfig *p_config;
  35. GF_ISOSample *p_sample;
  36. int i_track;
  37. uint32_t i_descidx;
  38. uint64_t i_time_res;
  39. int64_t i_time_inc;
  40. int64_t i_delay_time;
  41. int64_t i_init_delta;
  42. int i_numframe;
  43. int i_delay_frames;
  44. int b_dts_compress;
  45. int i_dts_compress_multiplier;
  46. int i_data_size;
  47. } mp4_hnd_t;
  48. static void recompute_bitrate_mp4( GF_ISOFile *p_file, int i_track )
  49. {
  50. u32 count, di, timescale, time_wnd, rate;
  51. u64 offset;
  52. Double br;
  53. GF_ESD *esd;
  54. esd = gf_isom_get_esd( p_file, i_track, 1 );
  55. if( !esd )
  56. return;
  57. esd->decoderConfig->avgBitrate = 0;
  58. esd->decoderConfig->maxBitrate = 0;
  59. rate = time_wnd = 0;
  60. timescale = gf_isom_get_media_timescale( p_file, i_track );
  61. count = gf_isom_get_sample_count( p_file, i_track );
  62. for( u32 i = 0; i < count; i++ )
  63. {
  64. GF_ISOSample *samp = gf_isom_get_sample_info( p_file, i_track, i+1, &di, &offset );
  65. if( !samp )
  66. {
  67. x264_cli_log( "mp4", X264_LOG_ERROR, "failure reading back frame %u\n", i );
  68. break;
  69. }
  70. if( esd->decoderConfig->bufferSizeDB < samp->dataLength )
  71. esd->decoderConfig->bufferSizeDB = samp->dataLength;
  72. esd->decoderConfig->avgBitrate += samp->dataLength;
  73. rate += samp->dataLength;
  74. if( samp->DTS > time_wnd + timescale )
  75. {
  76. if( rate > esd->decoderConfig->maxBitrate )
  77. esd->decoderConfig->maxBitrate = rate;
  78. time_wnd = samp->DTS;
  79. rate = 0;
  80. }
  81. gf_isom_sample_del( &samp );
  82. }
  83. br = (Double)(s64)gf_isom_get_media_duration( p_file, i_track );
  84. br /= timescale;
  85. esd->decoderConfig->avgBitrate = (u32)(esd->decoderConfig->avgBitrate / br);
  86. /*move to bps*/
  87. esd->decoderConfig->avgBitrate *= 8;
  88. esd->decoderConfig->maxBitrate *= 8;
  89. gf_isom_change_mpeg4_description( p_file, i_track, 1, esd );
  90. gf_odf_desc_del( (GF_Descriptor*)esd );
  91. }
  92. static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest_pts )
  93. {
  94. mp4_hnd_t *p_mp4 = handle;
  95. if( !p_mp4 )
  96. return 0;
  97. if( p_mp4->p_config )
  98. gf_odf_avc_cfg_del( p_mp4->p_config );
  99. if( p_mp4->p_sample )
  100. {
  101. if( p_mp4->p_sample->data )
  102. free( p_mp4->p_sample->data );
  103. p_mp4->p_sample->dataLength = 0;
  104. gf_isom_sample_del( &p_mp4->p_sample );
  105. }
  106. if( p_mp4->p_file )
  107. {
  108. if( p_mp4->i_track )
  109. {
  110. /* The mdhd duration is defined as CTS[final] - CTS[0] + duration of last frame.
  111. * The mdhd duration (in seconds) should be able to be longer than the tkhd duration since the track is managed by edts.
  112. * So, if mdhd duration is equal to the last DTS or less, we give the last composition time delta to the last sample duration.
  113. * And then, the mdhd duration is updated, but it time-wise doesn't give the actual duration.
  114. * The tkhd duration is the actual track duration. */
  115. uint64_t mdhd_duration = (2 * largest_pts - second_largest_pts) * p_mp4->i_time_inc;
  116. if( mdhd_duration != gf_isom_get_media_duration( p_mp4->p_file, p_mp4->i_track ) )
  117. {
  118. uint64_t last_dts = gf_isom_get_sample_dts( p_mp4->p_file, p_mp4->i_track, p_mp4->i_numframe );
  119. uint32_t last_duration = (uint32_t)( mdhd_duration > last_dts ? mdhd_duration - last_dts : (largest_pts - second_largest_pts) * p_mp4->i_time_inc );
  120. gf_isom_set_last_sample_duration( p_mp4->p_file, p_mp4->i_track, last_duration );
  121. }
  122. /* Write an Edit Box if the first CTS offset is positive.
  123. * A media_time is given by not the mvhd timescale but rather the mdhd timescale.
  124. * The reason is that an Edit Box maps the presentation time-line to the media time-line.
  125. * Any demuxers should follow the Edit Box if it exists. */
  126. GF_ISOSample *sample = gf_isom_get_sample_info( p_mp4->p_file, p_mp4->i_track, 1, NULL, NULL );
  127. if( sample && sample->CTS_Offset > 0 )
  128. {
  129. uint32_t mvhd_timescale = gf_isom_get_timescale( p_mp4->p_file );
  130. uint64_t tkhd_duration = (uint64_t)( mdhd_duration * ( (double)mvhd_timescale / p_mp4->i_time_res ) );
  131. gf_isom_append_edit_segment( p_mp4->p_file, p_mp4->i_track, tkhd_duration, sample->CTS_Offset, GF_ISOM_EDIT_NORMAL );
  132. }
  133. gf_isom_sample_del( &sample );
  134. recompute_bitrate_mp4( p_mp4->p_file, p_mp4->i_track );
  135. }
  136. gf_isom_set_pl_indication( p_mp4->p_file, GF_ISOM_PL_VISUAL, 0x15 );
  137. gf_isom_set_storage_mode( p_mp4->p_file, GF_ISOM_STORE_FLAT );
  138. gf_isom_close( p_mp4->p_file );
  139. }
  140. free( p_mp4 );
  141. return 0;
  142. }
  143. static int open_file( char *psz_filename, hnd_t *p_handle, cli_output_opt_t *opt )
  144. {
  145. *p_handle = NULL;
  146. FILE *fh = x264_fopen( psz_filename, "w" );
  147. if( !fh )
  148. return -1;
  149. int b_regular = x264_is_regular_file( fh );
  150. fclose( fh );
  151. FAIL_IF_ERR( !b_regular, "mp4", "MP4 output is incompatible with non-regular file `%s'\n", psz_filename );
  152. mp4_hnd_t *p_mp4 = calloc( 1, sizeof(mp4_hnd_t) );
  153. if( !p_mp4 )
  154. return -1;
  155. #ifdef _WIN32
  156. /* GPAC doesn't support Unicode filenames. */
  157. char ansi_filename[MAX_PATH];
  158. FAIL_IF_ERR( !x264_ansi_filename( psz_filename, ansi_filename, MAX_PATH, 1 ), "mp4", "invalid ansi filename\n" );
  159. p_mp4->p_file = gf_isom_open( ansi_filename, GF_ISOM_OPEN_WRITE, NULL );
  160. #else
  161. p_mp4->p_file = gf_isom_open( psz_filename, GF_ISOM_OPEN_WRITE, NULL );
  162. #endif
  163. p_mp4->b_dts_compress = opt->use_dts_compress;
  164. if( !(p_mp4->p_sample = gf_isom_sample_new()) )
  165. {
  166. close_file( p_mp4, 0, 0 );
  167. return -1;
  168. }
  169. gf_isom_set_brand_info( p_mp4->p_file, GF_ISOM_BRAND_AVC1, 0 );
  170. *p_handle = p_mp4;
  171. return 0;
  172. }
  173. static int set_param( hnd_t handle, x264_param_t *p_param )
  174. {
  175. mp4_hnd_t *p_mp4 = handle;
  176. p_mp4->i_delay_frames = p_param->i_bframe ? (p_param->i_bframe_pyramid ? 2 : 1) : 0;
  177. p_mp4->i_dts_compress_multiplier = p_mp4->b_dts_compress * p_mp4->i_delay_frames + 1;
  178. p_mp4->i_time_res = (uint64_t)p_param->i_timebase_den * p_mp4->i_dts_compress_multiplier;
  179. p_mp4->i_time_inc = (uint64_t)p_param->i_timebase_num * p_mp4->i_dts_compress_multiplier;
  180. FAIL_IF_ERR( p_mp4->i_time_res > UINT32_MAX, "mp4", "MP4 media timescale %"PRIu64" exceeds maximum\n", p_mp4->i_time_res );
  181. p_mp4->i_track = gf_isom_new_track( p_mp4->p_file, 0, GF_ISOM_MEDIA_VISUAL,
  182. p_mp4->i_time_res );
  183. p_mp4->p_config = gf_odf_avc_cfg_new();
  184. gf_isom_avc_config_new( p_mp4->p_file, p_mp4->i_track, p_mp4->p_config,
  185. NULL, NULL, &p_mp4->i_descidx );
  186. gf_isom_set_track_enabled( p_mp4->p_file, p_mp4->i_track, 1 );
  187. gf_isom_set_visual_info( p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx,
  188. p_param->i_width, p_param->i_height );
  189. if( p_param->vui.i_sar_width && p_param->vui.i_sar_height )
  190. {
  191. uint64_t dw = p_param->i_width << 16;
  192. uint64_t dh = p_param->i_height << 16;
  193. double sar = (double)p_param->vui.i_sar_width / p_param->vui.i_sar_height;
  194. if( sar > 1.0 )
  195. dw *= sar;
  196. else
  197. dh /= sar;
  198. gf_isom_set_pixel_aspect_ratio( p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx, p_param->vui.i_sar_width, p_param->vui.i_sar_height );
  199. gf_isom_set_track_layout_info( p_mp4->p_file, p_mp4->i_track, dw, dh, 0, 0, 0 );
  200. }
  201. p_mp4->i_data_size = p_param->i_width * p_param->i_height * 3 / 2;
  202. p_mp4->p_sample->data = malloc( p_mp4->i_data_size );
  203. if( !p_mp4->p_sample->data )
  204. {
  205. p_mp4->i_data_size = 0;
  206. return -1;
  207. }
  208. return 0;
  209. }
  210. static int check_buffer( mp4_hnd_t *p_mp4, int needed_size )
  211. {
  212. if( needed_size > p_mp4->i_data_size )
  213. {
  214. void *ptr = realloc( p_mp4->p_sample->data, needed_size );
  215. if( !ptr )
  216. return -1;
  217. p_mp4->p_sample->data = ptr;
  218. p_mp4->i_data_size = needed_size;
  219. }
  220. return 0;
  221. }
  222. static int write_headers( hnd_t handle, x264_nal_t *p_nal )
  223. {
  224. mp4_hnd_t *p_mp4 = handle;
  225. GF_AVCConfigSlot *p_slot;
  226. int sps_size = p_nal[0].i_payload - 4;
  227. int pps_size = p_nal[1].i_payload - 4;
  228. int sei_size = p_nal[2].i_payload;
  229. uint8_t *sps = p_nal[0].p_payload + 4;
  230. uint8_t *pps = p_nal[1].p_payload + 4;
  231. uint8_t *sei = p_nal[2].p_payload;
  232. // SPS
  233. p_mp4->p_config->configurationVersion = 1;
  234. p_mp4->p_config->AVCProfileIndication = sps[1];
  235. p_mp4->p_config->profile_compatibility = sps[2];
  236. p_mp4->p_config->AVCLevelIndication = sps[3];
  237. p_slot = malloc( sizeof(GF_AVCConfigSlot) );
  238. if( !p_slot )
  239. return -1;
  240. p_slot->size = sps_size;
  241. p_slot->data = malloc( p_slot->size );
  242. if( !p_slot->data )
  243. return -1;
  244. memcpy( p_slot->data, sps, sps_size );
  245. gf_list_add( p_mp4->p_config->sequenceParameterSets, p_slot );
  246. // PPS
  247. p_slot = malloc( sizeof(GF_AVCConfigSlot) );
  248. if( !p_slot )
  249. return -1;
  250. p_slot->size = pps_size;
  251. p_slot->data = malloc( p_slot->size );
  252. if( !p_slot->data )
  253. return -1;
  254. memcpy( p_slot->data, pps, pps_size );
  255. gf_list_add( p_mp4->p_config->pictureParameterSets, p_slot );
  256. gf_isom_avc_config_update( p_mp4->p_file, p_mp4->i_track, 1, p_mp4->p_config );
  257. // SEI
  258. if( check_buffer( p_mp4, p_mp4->p_sample->dataLength + sei_size ) )
  259. return -1;
  260. memcpy( p_mp4->p_sample->data + p_mp4->p_sample->dataLength, sei, sei_size );
  261. p_mp4->p_sample->dataLength += sei_size;
  262. return sei_size + sps_size + pps_size;
  263. }
  264. static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
  265. {
  266. mp4_hnd_t *p_mp4 = handle;
  267. int64_t dts;
  268. int64_t cts;
  269. if( check_buffer( p_mp4, p_mp4->p_sample->dataLength + i_size ) )
  270. return -1;
  271. memcpy( p_mp4->p_sample->data + p_mp4->p_sample->dataLength, p_nalu, i_size );
  272. p_mp4->p_sample->dataLength += i_size;
  273. if( !p_mp4->i_numframe )
  274. p_mp4->i_delay_time = p_picture->i_dts * -1;
  275. if( p_mp4->b_dts_compress )
  276. {
  277. if( p_mp4->i_numframe == 1 )
  278. p_mp4->i_init_delta = (p_picture->i_dts + p_mp4->i_delay_time) * p_mp4->i_time_inc;
  279. dts = p_mp4->i_numframe > p_mp4->i_delay_frames
  280. ? p_picture->i_dts * p_mp4->i_time_inc
  281. : p_mp4->i_numframe * (p_mp4->i_init_delta / p_mp4->i_dts_compress_multiplier);
  282. cts = p_picture->i_pts * p_mp4->i_time_inc;
  283. }
  284. else
  285. {
  286. dts = (p_picture->i_dts + p_mp4->i_delay_time) * p_mp4->i_time_inc;
  287. cts = (p_picture->i_pts + p_mp4->i_delay_time) * p_mp4->i_time_inc;
  288. }
  289. p_mp4->p_sample->IsRAP = p_picture->b_keyframe;
  290. p_mp4->p_sample->DTS = dts;
  291. p_mp4->p_sample->CTS_Offset = (uint32_t)(cts - dts);
  292. gf_isom_add_sample( p_mp4->p_file, p_mp4->i_track, p_mp4->i_descidx, p_mp4->p_sample );
  293. p_mp4->p_sample->dataLength = 0;
  294. p_mp4->i_numframe++;
  295. return i_size;
  296. }
  297. const cli_output_t mp4_output = { open_file, set_param, write_headers, write_frame, close_file };