Advertisement
Guest User

Untitled

a guest
May 26th, 2017
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.38 KB | None | 0 0
  1. Index: Makefile
  2. ===================================================================
  3. --- Makefile_orig
  4. +++ Makefile
  5. @@ -43,6 +43,10 @@
  6. SRCCLI += output/mp4.c
  7. endif
  8.  
  9. +ifneq ($(findstring AVI_OUTPUT, $(CONFIG)),)
  10. +SRCCLI += output/avi.c
  11. +endif
  12. +
  13. # Visualization sources
  14. ifeq ($(VIS),yes)
  15. SRCS += common/visualize.c common/display-x11.c
  16. Index: configure
  17. ===================================================================
  18. --- configure_orig
  19. +++ configure
  20. @@ -11,6 +11,7 @@
  21. echo " --disable-lavf-input disables libavformat input"
  22. echo " --disable-ffms-input disables ffmpegsource input"
  23. echo " --disable-mp4-output disables mp4 output (using gpac)"
  24. +echo " --disable-avi-output disables avi output (using libavformat)"
  25. echo " --disable-pthread disables multithreaded encoding"
  26. echo " --disable-asm disables platform-specific assembly optimizations"
  27. echo " --enable-debug adds -g, doesn't strip"
  28. @@ -117,6 +118,7 @@
  29. lavf_input="auto"
  30. ffms_input="auto"
  31. mp4_output="auto"
  32. +avi_output="auto"
  33. pthread="auto"
  34. asm="auto"
  35. debug="no"
  36. @@ -169,6 +171,12 @@
  37. --disable-mp4-output)
  38. mp4_output="no"
  39. ;;
  40. + --enable-avi-output)
  41. + avi_output="auto"
  42. + ;;
  43. + --disable-avi-output)
  44. + avi_output="no"
  45. + ;;
  46. --extra-asflags=*)
  47. ASFLAGS="$ASFLAGS ${opt#--extra-asflags=}"
  48. ;;
  49. @@ -593,6 +601,32 @@
  50. LDFLAGSCLI="$LDFLAGSCLI $MP4_LDFLAGS"
  51. fi
  52.  
  53. +if [ "$avi_output" = "auto" ] ; then
  54. + avi_output="no"
  55. + if ${cross_prefix}pkg-config --exists libavformat 2>$DEVNULL; then
  56. + AVI_LIBS="$AVI_LIBS $(${cross_prefix}pkg-config --libs libavformat)"
  57. + AVI_CFLAGS="$AVI_CFLAGS $(${cross_prefix}pkg-config --cflags libavformat)"
  58. + fi
  59. + if [ -z "$AVI_LIBS" -a -z "$AVI_CFLAGS" ]; then
  60. + AVI_LIBS="-lavformat"
  61. + for lib in -lavcodec -lavutil -lm -lz -lbz2 $libpthread -lavifil32; do
  62. + cc_check "" $lib && AVI_LIBS="$AVI_LIBS $lib"
  63. + done
  64. + fi
  65. + AVI_LIBS="-L. $AVI_LIBS"
  66. + if cc_check libavformat/avformat.h "$AVI_CFLAGS $AVI_LIBS" ; then
  67. + if cc_check libavformat/avformat.h "$AVI_CFLAGS $AVI_LIBS" 'av_register_all(); av_guess_format( "avi", NULL, NULL );' ; then
  68. + avi_output="yes"
  69. + define AVI_OUTPUT
  70. + fi
  71. + fi
  72. +fi
  73. +
  74. +if [ "$avi_output" = "yes" ]; then
  75. + LDFLAGSCLI="$AVI_LIBS $LDFLAGSCLI"
  76. + [ -n "$AVI_CFLAGS" ] && CFLAGS="$CFLAGS $AVI_CFLAGS"
  77. +fi
  78. +
  79. if [ "$avs_input" = "auto" ] ; then
  80. avs_input=no
  81. if [ $SYS = MINGW ] && cc_check avisynth_c.h ; then
  82. @@ -709,6 +743,7 @@
  83. lavf input: $lavf_input
  84. ffms input: $ffms_input
  85. mp4 output: $mp4_output
  86. +avi output: $avi_output
  87. pthread: $pthread
  88. debug: $debug
  89. gprof: $gprof
  90. Index: output/avi.c
  91. ===================================================================
  92. --- /dev/null
  93. +++ output/avi.c
  94. @@ -0,0 +1,242 @@
  95. +/*****************************************************************************
  96. + * avi.c: x264 avi output module (using libavformat)
  97. + *****************************************************************************
  98. + * Copyright (C) 2010 x264 project
  99. + *
  100. + * Authors: Anton Mitrofanov <BugMaster@narod.ru>
  101. + *
  102. + * This program is free software; you can redistribute it and/or modify
  103. + * it under the terms of the GNU General Public License as published by
  104. + * the Free Software Foundation; either version 2 of the License, or
  105. + * (at your option) any later version.
  106. + *
  107. + * This program is distributed in the hope that it will be useful,
  108. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  109. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  110. + * GNU General Public License for more details.
  111. + *
  112. + * You should have received a copy of the GNU General Public License
  113. + * along with this program; if not, write to the Free Software
  114. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  115. + *****************************************************************************/
  116. +
  117. +#include "muxers.h"
  118. +#undef DECLARE_ALIGNED
  119. +#include <libavformat/avformat.h>
  120. +
  121. +typedef struct
  122. +{
  123. + AVFormatContext *mux_fc;
  124. + AVStream *video_stm;
  125. + uint8_t *data;
  126. + unsigned d_max;
  127. + unsigned d_cur;
  128. +} avi_hnd_t;
  129. +
  130. +static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest_pts )
  131. +{
  132. + avi_hnd_t *h = handle;
  133. +
  134. + if( !h )
  135. + return 0;
  136. +
  137. + if( h->data )
  138. + {
  139. + free( h->data );
  140. + h->data = NULL;
  141. + }
  142. +
  143. + if( h->mux_fc && h->video_stm )
  144. + {
  145. + av_write_trailer( h->mux_fc );
  146. + av_freep( &h->video_stm->codec->extradata );
  147. + av_freep( &h->video_stm->codec );
  148. + av_freep( &h->video_stm );
  149. + }
  150. +
  151. + if( h->mux_fc && h->mux_fc->pb )
  152. + {
  153. + url_fclose( h->mux_fc->pb );
  154. + h->mux_fc->pb = NULL;
  155. + }
  156. +
  157. + if( h->mux_fc )
  158. + av_freep( &h->mux_fc );
  159. +
  160. + free( h );
  161. +
  162. + return 0;
  163. +}
  164. +
  165. +static int open_file( char *psz_filename, hnd_t *p_handle )
  166. +{
  167. + avi_hnd_t *h;
  168. + AVOutputFormat *mux_fmt;
  169. +
  170. + *p_handle = NULL;
  171. +
  172. + FILE *fh = fopen( psz_filename, "w" );
  173. + if( !fh )
  174. + return -1;
  175. + else if( !x264_is_regular_file( fh ) )
  176. + {
  177. + fprintf( stderr, "avi [error]: AVI output is incompatible with non-regular file `%s'\n", psz_filename );
  178. + return -1;
  179. + }
  180. + fclose( fh );
  181. +
  182. + if( !(h = malloc( sizeof(avi_hnd_t) )) )
  183. + return -1;
  184. + memset( h, 0, sizeof(avi_hnd_t) );
  185. +
  186. + av_register_all();
  187. + mux_fmt = av_guess_format( "avi", NULL, NULL );
  188. + if( !mux_fmt )
  189. + {
  190. + close_file( h, 0, 0 );
  191. + return -1;
  192. + }
  193. +
  194. + h->mux_fc = avformat_alloc_context();
  195. + if( !h->mux_fc )
  196. + {
  197. + close_file( h, 0, 0 );
  198. + return -1;
  199. + }
  200. + h->mux_fc->oformat = mux_fmt;
  201. + snprintf( h->mux_fc->filename, sizeof(h->mux_fc->filename), "%s", psz_filename );
  202. +
  203. + if( url_fopen( &h->mux_fc->pb, psz_filename, URL_WRONLY ) < 0 )
  204. + {
  205. + close_file( h, 0, 0 );
  206. + return -1;
  207. + }
  208. +
  209. + *p_handle = h;
  210. +
  211. + return 0;
  212. +}
  213. +
  214. +static int set_param( hnd_t handle, x264_param_t *p_param )
  215. +{
  216. + avi_hnd_t *h = handle;
  217. + AVCodecContext *c;
  218. +
  219. + if( !h->mux_fc || h->video_stm )
  220. + return -1;
  221. +
  222. + h->video_stm = av_new_stream( h->mux_fc, 0 );
  223. + if( !h->video_stm )
  224. + return -1;
  225. +
  226. + c = h->video_stm->codec;
  227. + c->flags |= p_param->b_repeat_headers ? 0 : CODEC_FLAG_GLOBAL_HEADER;
  228. + c->time_base.num = p_param->i_timebase_num;
  229. + c->time_base.den = p_param->i_timebase_den;
  230. + c->width = p_param->i_width;
  231. + c->height = p_param->i_height;
  232. + c->pix_fmt = PIX_FMT_YUV420P;
  233. + c->codec_type = AVMEDIA_TYPE_VIDEO;
  234. + c->codec_id = CODEC_ID_H264;
  235. + c->codec_tag = MKTAG('H','2','6','4');
  236. +
  237. + if( av_set_parameters( h->mux_fc, NULL ) < 0 )
  238. + return -1;
  239. + if( !(c->flags & CODEC_FLAG_GLOBAL_HEADER) && av_write_header( h->mux_fc ) )
  240. + return -1;
  241. +
  242. + return 0;
  243. +}
  244. +
  245. +static int write_buffer( avi_hnd_t *h, uint8_t *p_nalu, int i_size )
  246. +{
  247. + unsigned ns = h->d_cur + i_size;
  248. +
  249. + if( !h->data || ns > h->d_max )
  250. + {
  251. + void *dp;
  252. + unsigned dn = 16;
  253. +
  254. + while( ns > dn )
  255. + dn <<= 1;
  256. +
  257. + dp = realloc( h->data, dn );
  258. + if( !dp )
  259. + return -1;
  260. +
  261. + h->data = dp;
  262. + h->d_max = dn;
  263. + }
  264. +
  265. + memcpy( h->data + h->d_cur, p_nalu, i_size );
  266. + h->d_cur = ns;
  267. +
  268. + return i_size;
  269. +}
  270. +
  271. +static int write_headers( hnd_t handle, x264_nal_t *p_nal )
  272. +{
  273. + avi_hnd_t *h = handle;
  274. + AVCodecContext *c;
  275. + int i_size = p_nal[0].i_payload + p_nal[1].i_payload + p_nal[2].i_payload;
  276. +
  277. + if( !h->mux_fc || !h->video_stm )
  278. + return -1;
  279. +
  280. + c = h->video_stm->codec;
  281. + if( c->flags & CODEC_FLAG_GLOBAL_HEADER )
  282. + {
  283. + c->extradata_size = i_size - p_nal[2].i_payload;
  284. + av_freep( &c->extradata );
  285. + c->extradata = av_malloc( c->extradata_size );
  286. + if( !c->extradata )
  287. + return -1;
  288. + /* Write the SPS/PPS to the extradata */
  289. + memcpy( c->extradata, p_nal[0].p_payload, c->extradata_size );
  290. + /* Write the SEI as part of the first frame */
  291. + if( write_buffer( h, p_nal[2].p_payload, p_nal[2].i_payload ) < 0 )
  292. + return -1;
  293. + if( av_write_header( h->mux_fc ) )
  294. + return -1;
  295. + }
  296. + else
  297. + if( write_buffer( h, p_nal[0].p_payload, i_size ) < 0 )
  298. + return -1;
  299. +
  300. + return i_size;
  301. +}
  302. +
  303. +static int write_frame( hnd_t handle, uint8_t *p_nalu, int i_size, x264_picture_t *p_picture )
  304. +{
  305. + avi_hnd_t *h = handle;
  306. + AVPacket pkt;
  307. +
  308. + if( !h->mux_fc || !h->video_stm )
  309. + return -1;
  310. +
  311. + av_init_packet(&pkt);
  312. + pkt.stream_index = h->video_stm->index;
  313. + pkt.flags |= p_picture->b_keyframe ? PKT_FLAG_KEY : 0;
  314. + if( h->d_cur )
  315. + {
  316. + if( write_buffer( h, p_nalu, i_size ) < 0 )
  317. + return -1;
  318. + pkt.data = h->data;
  319. + pkt.size = h->d_cur;
  320. + }
  321. + else
  322. + {
  323. + pkt.data = p_nalu;
  324. + pkt.size = i_size;
  325. + }
  326. + pkt.pts = AV_NOPTS_VALUE; //av_rescale_q( p_picture->i_pts, h->video_stm->codec->time_base, h->video_stm->time_base );
  327. + pkt.dts = AV_NOPTS_VALUE; //av_rescale_q( p_picture->i_dts, h->video_stm->codec->time_base, h->video_stm->time_base );
  328. + if( av_interleaved_write_frame( h->mux_fc, &pkt ) )
  329. + return -1;
  330. +
  331. + h->d_cur = 0;
  332. +
  333. + return i_size;
  334. +}
  335. +
  336. +const cli_output_t avi_output = { open_file, set_param, write_headers, write_frame, close_file };
  337. Index: output/output.h
  338. ===================================================================
  339. --- output/output_orig.h
  340. +++ output/output.h
  341. @@ -37,5 +37,6 @@
  342. extern const cli_output_t mkv_output;
  343. extern const cli_output_t mp4_output;
  344. extern const cli_output_t flv_output;
  345. +extern const cli_output_t avi_output;
  346.  
  347. #endif
  348. Index: x264.c
  349. ===================================================================
  350. --- x264_orig.c
  351. +++ x264.c
  352. @@ -92,6 +92,9 @@
  353. #ifdef MP4_OUTPUT
  354. "mp4",
  355. #endif
  356. +#ifdef AVI_OUTPUT
  357. + "avi",
  358. +#endif
  359. 0
  360. };
  361.  
  362. @@ -217,6 +220,7 @@
  363. " .mkv -> Matroska\n"
  364. " .flv -> Flash Video\n"
  365. " .mp4 -> MP4 if compiled with GPAC support (%s)\n"
  366. + " .avi -> AVI if compiled with support (%s)\n"
  367. "\n"
  368. "Options:\n"
  369. "\n"
  370. @@ -241,6 +245,11 @@
  371. "no",
  372. #endif
  373. #ifdef MP4_OUTPUT
  374. + "yes",
  375. +#else
  376. + "no",
  377. +#endif
  378. +#ifdef AVI_OUTPUT
  379. "yes"
  380. #else
  381. "no"
  382. @@ -795,6 +804,23 @@
  383. param->b_annexb = 0;
  384. param->b_dts_compress = 1;
  385. param->b_repeat_headers = 0;
  386. + }
  387. + else if( !strcasecmp( ext, "avi" ) )
  388. + {
  389. +#ifdef AVI_OUTPUT
  390. + output = avi_output;
  391. + param->b_annexb = 1;
  392. + param->b_dts_compress = 0;
  393. + param->b_repeat_headers = 1;
  394. + if( param->b_vfr_input )
  395. + {
  396. + fprintf( stderr, "x264 [warning]: VFR is not compatible with AVI output\n" );
  397. + param->b_vfr_input = 0;
  398. + }
  399. +#else
  400. + fprintf( stderr, "x264 [error]: not compiled with AVI output support\n" );
  401. + return -1;
  402. +#endif
  403. }
  404. else
  405. output = raw_output;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement