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.

768 lines
25KB

  1. /*
  2. * Interface to xvidcore for mpeg4 encoding
  3. * Copyright (c) 2004 Adam Thayer <krevnik@comcast.net>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file xvidmpeg4.c
  23. * Interface to xvidcore for MPEG-4 compliant encoding.
  24. * @author Adam Thayer (krevnik@comcast.net)
  25. */
  26. #include <xvid.h>
  27. #include <unistd.h>
  28. #include "avcodec.h"
  29. #include "libxvid_internal.h"
  30. /**
  31. * Buffer management macros.
  32. */
  33. #define BUFFER_SIZE 1024
  34. #define BUFFER_REMAINING(x) (BUFFER_SIZE - strlen(x))
  35. #define BUFFER_CAT(x) (&((x)[strlen(x)]))
  36. /* For PPC Use */
  37. #if HAVE_ALTIVEC==1
  38. extern int has_altivec(void);
  39. #endif
  40. /**
  41. * Structure for the private XviD context.
  42. * This stores all the private context for the codec.
  43. */
  44. typedef struct xvid_context {
  45. void *encoder_handle; /** Handle for XviD Encoder */
  46. int xsize, ysize; /** Frame size */
  47. int vop_flags; /** VOP flags for XviD Encoder */
  48. int vol_flags; /** VOL flags for XviD Encoder */
  49. int me_flags; /** Motion Estimation flags */
  50. int qscale; /** Do we use constant scale? */
  51. int quicktime_format; /** Are we in a QT-based format? */
  52. AVFrame encoded_picture; /** Encoded frame information */
  53. char *twopassbuffer; /** Character buffer for two-pass */
  54. char *old_twopassbuffer; /** Old character buffer (two-pass) */
  55. char *twopassfile; /** second pass temp file name */
  56. unsigned char *intra_matrix; /** P-Frame Quant Matrix */
  57. unsigned char *inter_matrix; /** I-Frame Quant Matrix */
  58. } xvid_context_t;
  59. /**
  60. * Structure for the private first-pass plugin.
  61. */
  62. typedef struct xvid_ff_pass1 {
  63. int version; /** XviD version */
  64. xvid_context_t *context; /** Pointer to private context */
  65. } xvid_ff_pass1_t;
  66. /* Prototypes - See function implementation for details */
  67. int xvid_strip_vol_header(AVCodecContext *avctx, unsigned char *frame, unsigned int header_len, unsigned int frame_len);
  68. int xvid_ff_2pass(void *ref, int opt, void *p1, void *p2);
  69. void xvid_correct_framerate(AVCodecContext *avctx);
  70. /**
  71. * Creates the private context for the encoder.
  72. * All buffers are allocated, settings are loaded from the user,
  73. * and the encoder context created.
  74. *
  75. * @param avctx AVCodecContext pointer to context
  76. * @return Returns 0 on success, -1 on failure
  77. */
  78. int ff_xvid_encode_init(AVCodecContext *avctx) {
  79. int xerr, i;
  80. int xvid_flags = avctx->flags;
  81. xvid_context_t *x = avctx->priv_data;
  82. uint16_t *intra, *inter;
  83. int fd;
  84. xvid_plugin_single_t single;
  85. xvid_ff_pass1_t rc2pass1;
  86. xvid_plugin_2pass2_t rc2pass2;
  87. xvid_gbl_init_t xvid_gbl_init;
  88. xvid_enc_create_t xvid_enc_create;
  89. xvid_enc_plugin_t plugins[7];
  90. /* Bring in VOP flags from ffmpeg command-line */
  91. x->vop_flags = XVID_VOP_HALFPEL; /* Bare minimum quality */
  92. if( xvid_flags & CODEC_FLAG_4MV )
  93. x->vop_flags |= XVID_VOP_INTER4V; /* Level 3 */
  94. if( xvid_flags & CODEC_FLAG_TRELLIS_QUANT)
  95. x->vop_flags |= XVID_VOP_TRELLISQUANT; /* Level 5 */
  96. if( xvid_flags & CODEC_FLAG_AC_PRED )
  97. x->vop_flags |= XVID_VOP_HQACPRED; /* Level 6 */
  98. if( xvid_flags & CODEC_FLAG_GRAY )
  99. x->vop_flags |= XVID_VOP_GREYSCALE;
  100. /* Decide which ME quality setting to use */
  101. x->me_flags = 0;
  102. switch( avctx->me_method ) {
  103. case ME_FULL: /* Quality 6 */
  104. x->me_flags |= XVID_ME_EXTSEARCH16
  105. | XVID_ME_EXTSEARCH8;
  106. case ME_EPZS: /* Quality 4 */
  107. x->me_flags |= XVID_ME_ADVANCEDDIAMOND8
  108. | XVID_ME_HALFPELREFINE8
  109. | XVID_ME_CHROMA_PVOP
  110. | XVID_ME_CHROMA_BVOP;
  111. case ME_LOG: /* Quality 2 */
  112. case ME_PHODS:
  113. case ME_X1:
  114. x->me_flags |= XVID_ME_ADVANCEDDIAMOND16
  115. | XVID_ME_HALFPELREFINE16;
  116. case ME_ZERO: /* Quality 0 */
  117. default:
  118. break;
  119. }
  120. /* Decide how we should decide blocks */
  121. switch( avctx->mb_decision ) {
  122. case 2:
  123. x->vop_flags |= XVID_VOP_MODEDECISION_RD;
  124. x->me_flags |= XVID_ME_HALFPELREFINE8_RD
  125. | XVID_ME_QUARTERPELREFINE8_RD
  126. | XVID_ME_EXTSEARCH_RD
  127. | XVID_ME_CHECKPREDICTION_RD;
  128. case 1:
  129. if( !(x->vop_flags & XVID_VOP_MODEDECISION_RD) )
  130. x->vop_flags |= XVID_VOP_FAST_MODEDECISION_RD;
  131. x->me_flags |= XVID_ME_HALFPELREFINE16_RD
  132. | XVID_ME_QUARTERPELREFINE16_RD;
  133. default:
  134. break;
  135. }
  136. /* Bring in VOL flags from ffmpeg command-line */
  137. x->vol_flags = 0;
  138. if( xvid_flags & CODEC_FLAG_GMC ) {
  139. x->vol_flags |= XVID_VOL_GMC;
  140. x->me_flags |= XVID_ME_GME_REFINE;
  141. }
  142. if( xvid_flags & CODEC_FLAG_QPEL ) {
  143. x->vol_flags |= XVID_VOL_QUARTERPEL;
  144. x->me_flags |= XVID_ME_QUARTERPELREFINE16;
  145. if( x->vop_flags & XVID_VOP_INTER4V )
  146. x->me_flags |= XVID_ME_QUARTERPELREFINE8;
  147. }
  148. memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));
  149. xvid_gbl_init.version = XVID_VERSION;
  150. xvid_gbl_init.debug = 0;
  151. #ifdef ARCH_POWERPC
  152. /* XviD's PPC support is borked, use libavcodec to detect */
  153. #if HAVE_ALTIVEC==1
  154. if( has_altivec() ) {
  155. xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ALTIVEC;
  156. } else
  157. #endif
  158. xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
  159. #else
  160. /* XviD can detect on x86 */
  161. xvid_gbl_init.cpu_flags = 0;
  162. #endif
  163. /* Initialize */
  164. xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
  165. /* Create the encoder reference */
  166. memset(&xvid_enc_create, 0, sizeof(xvid_enc_create));
  167. xvid_enc_create.version = XVID_VERSION;
  168. /* Store the desired frame size */
  169. xvid_enc_create.width = x->xsize = avctx->width;
  170. xvid_enc_create.height = x->ysize = avctx->height;
  171. /* XviD can determine the proper profile to use */
  172. /* xvid_enc_create.profile = XVID_PROFILE_S_L3; */
  173. /* We don't use zones or threads */
  174. xvid_enc_create.zones = NULL;
  175. xvid_enc_create.num_zones = 0;
  176. xvid_enc_create.num_threads = 0;
  177. xvid_enc_create.plugins = plugins;
  178. xvid_enc_create.num_plugins = 0;
  179. /* Initialize Buffers */
  180. x->twopassbuffer = NULL;
  181. x->old_twopassbuffer = NULL;
  182. x->twopassfile = NULL;
  183. if( xvid_flags & CODEC_FLAG_PASS1 ) {
  184. memset(&rc2pass1, 0, sizeof(xvid_ff_pass1_t));
  185. rc2pass1.version = XVID_VERSION;
  186. rc2pass1.context = x;
  187. x->twopassbuffer = av_malloc(BUFFER_SIZE);
  188. x->old_twopassbuffer = av_malloc(BUFFER_SIZE);
  189. if( x->twopassbuffer == NULL || x->old_twopassbuffer == NULL ) {
  190. av_log(avctx, AV_LOG_ERROR,
  191. "XviD: Cannot allocate 2-pass log buffers\n");
  192. return -1;
  193. }
  194. x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0;
  195. plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass;
  196. plugins[xvid_enc_create.num_plugins].param = &rc2pass1;
  197. xvid_enc_create.num_plugins++;
  198. } else if( xvid_flags & CODEC_FLAG_PASS2 ) {
  199. memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t));
  200. rc2pass2.version = XVID_VERSION;
  201. rc2pass2.bitrate = avctx->bit_rate;
  202. fd = av_tempfile("xvidff.", &(x->twopassfile));
  203. if( fd == -1 ) {
  204. av_log(avctx, AV_LOG_ERROR,
  205. "XviD: Cannot write 2-pass pipe\n");
  206. return -1;
  207. }
  208. if( avctx->stats_in == NULL ) {
  209. av_log(avctx, AV_LOG_ERROR,
  210. "XviD: No 2-pass information loaded for second pass\n");
  211. return -1;
  212. }
  213. if( strlen(avctx->stats_in) >
  214. write(fd, avctx->stats_in, strlen(avctx->stats_in)) ) {
  215. close(fd);
  216. av_log(avctx, AV_LOG_ERROR,
  217. "XviD: Cannot write to 2-pass pipe\n");
  218. return -1;
  219. }
  220. close(fd);
  221. rc2pass2.filename = x->twopassfile;
  222. plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
  223. plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
  224. xvid_enc_create.num_plugins++;
  225. } else if( !(xvid_flags & CODEC_FLAG_QSCALE) ) {
  226. /* Single Pass Bitrate Control! */
  227. memset(&single, 0, sizeof(xvid_plugin_single_t));
  228. single.version = XVID_VERSION;
  229. single.bitrate = avctx->bit_rate;
  230. plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single;
  231. plugins[xvid_enc_create.num_plugins].param = &single;
  232. xvid_enc_create.num_plugins++;
  233. }
  234. /* Luminance Masking */
  235. if( 0.0 != avctx->lumi_masking ) {
  236. plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
  237. plugins[xvid_enc_create.num_plugins].param = NULL;
  238. xvid_enc_create.num_plugins++;
  239. }
  240. /* Frame Rate and Key Frames */
  241. xvid_correct_framerate(avctx);
  242. xvid_enc_create.fincr = avctx->time_base.num;
  243. xvid_enc_create.fbase = avctx->time_base.den;
  244. if( avctx->gop_size > 0 )
  245. xvid_enc_create.max_key_interval = avctx->gop_size;
  246. else
  247. xvid_enc_create.max_key_interval = 240; /* XviD's best default */
  248. /* Quants */
  249. if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1;
  250. else x->qscale = 0;
  251. xvid_enc_create.min_quant[0] = avctx->qmin;
  252. xvid_enc_create.min_quant[1] = avctx->qmin;
  253. xvid_enc_create.min_quant[2] = avctx->qmin;
  254. xvid_enc_create.max_quant[0] = avctx->qmax;
  255. xvid_enc_create.max_quant[1] = avctx->qmax;
  256. xvid_enc_create.max_quant[2] = avctx->qmax;
  257. /* Quant Matrices */
  258. x->intra_matrix = x->inter_matrix = NULL;
  259. if( avctx->mpeg_quant )
  260. x->vol_flags |= XVID_VOL_MPEGQUANT;
  261. if( (avctx->intra_matrix || avctx->inter_matrix) ) {
  262. x->vol_flags |= XVID_VOL_MPEGQUANT;
  263. if( avctx->intra_matrix ) {
  264. intra = avctx->intra_matrix;
  265. x->intra_matrix = av_malloc(sizeof(unsigned char) * 64);
  266. } else
  267. intra = NULL;
  268. if( avctx->inter_matrix ) {
  269. inter = avctx->inter_matrix;
  270. x->inter_matrix = av_malloc(sizeof(unsigned char) * 64);
  271. } else
  272. inter = NULL;
  273. for( i = 0; i < 64; i++ ) {
  274. if( intra )
  275. x->intra_matrix[i] = (unsigned char)intra[i];
  276. if( inter )
  277. x->inter_matrix[i] = (unsigned char)inter[i];
  278. }
  279. }
  280. /* Misc Settings */
  281. xvid_enc_create.frame_drop_ratio = 0;
  282. xvid_enc_create.global = 0;
  283. if( xvid_flags & CODEC_FLAG_CLOSED_GOP )
  284. xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
  285. /* Determines which codec mode we are operating in */
  286. avctx->extradata = NULL;
  287. avctx->extradata_size = 0;
  288. if( xvid_flags & CODEC_FLAG_GLOBAL_HEADER ) {
  289. /* In this case, we are claiming to be MPEG4 */
  290. x->quicktime_format = 1;
  291. avctx->codec_id = CODEC_ID_MPEG4;
  292. } else {
  293. /* We are claiming to be XviD */
  294. x->quicktime_format = 0;
  295. if(!avctx->codec_tag)
  296. avctx->codec_tag = ff_get_fourcc("xvid");
  297. }
  298. /* Bframes */
  299. xvid_enc_create.max_bframes = avctx->max_b_frames;
  300. xvid_enc_create.bquant_offset = 100 * avctx->b_quant_offset;
  301. xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor;
  302. if( avctx->max_b_frames > 0 && !x->quicktime_format ) xvid_enc_create.global |= XVID_GLOBAL_PACKED;
  303. /* Create encoder context */
  304. xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
  305. if( xerr ) {
  306. av_log(avctx, AV_LOG_ERROR, "XviD: Could not create encoder reference\n");
  307. return -1;
  308. }
  309. x->encoder_handle = xvid_enc_create.handle;
  310. avctx->coded_frame = &x->encoded_picture;
  311. return 0;
  312. }
  313. /**
  314. * Encodes a single frame.
  315. *
  316. * @param avctx AVCodecContext pointer to context
  317. * @param frame Pointer to encoded frame buffer
  318. * @param buf_size Size of encoded frame buffer
  319. * @param data Pointer to AVFrame of unencoded frame
  320. * @return Returns 0 on success, -1 on failure
  321. */
  322. int ff_xvid_encode_frame(AVCodecContext *avctx,
  323. unsigned char *frame, int buf_size, void *data) {
  324. int xerr, i;
  325. char *tmp;
  326. xvid_context_t *x = avctx->priv_data;
  327. AVFrame *picture = data;
  328. AVFrame *p = &(x->encoded_picture);
  329. xvid_enc_frame_t xvid_enc_frame;
  330. xvid_enc_stats_t xvid_enc_stats;
  331. /* Start setting up the frame */
  332. memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame));
  333. xvid_enc_frame.version = XVID_VERSION;
  334. memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats));
  335. xvid_enc_stats.version = XVID_VERSION;
  336. *p = *picture;
  337. /* Let XviD know where to put the frame. */
  338. xvid_enc_frame.bitstream = frame;
  339. xvid_enc_frame.length = buf_size;
  340. /* Initialize input image fields */
  341. if( avctx->pix_fmt != PIX_FMT_YUV420P ) {
  342. av_log(avctx, AV_LOG_ERROR, "XviD: Color spaces other than 420p not supported\n");
  343. return -1;
  344. }
  345. xvid_enc_frame.input.csp = XVID_CSP_PLANAR; /* YUV420P */
  346. for( i = 0; i < 4; i++ ) {
  347. xvid_enc_frame.input.plane[i] = picture->data[i];
  348. xvid_enc_frame.input.stride[i] = picture->linesize[i];
  349. }
  350. /* Encoder Flags */
  351. xvid_enc_frame.vop_flags = x->vop_flags;
  352. xvid_enc_frame.vol_flags = x->vol_flags;
  353. xvid_enc_frame.motion = x->me_flags;
  354. xvid_enc_frame.type = XVID_TYPE_AUTO;
  355. /* Quant Setting */
  356. if( x->qscale ) xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA;
  357. else xvid_enc_frame.quant = 0;
  358. /* Matrices */
  359. xvid_enc_frame.quant_intra_matrix = x->intra_matrix;
  360. xvid_enc_frame.quant_inter_matrix = x->inter_matrix;
  361. /* Encode */
  362. xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE,
  363. &xvid_enc_frame, &xvid_enc_stats);
  364. /* Two-pass log buffer swapping */
  365. avctx->stats_out = NULL;
  366. if( x->twopassbuffer ) {
  367. tmp = x->old_twopassbuffer;
  368. x->old_twopassbuffer = x->twopassbuffer;
  369. x->twopassbuffer = tmp;
  370. x->twopassbuffer[0] = 0;
  371. if( x->old_twopassbuffer[0] != 0 ) {
  372. avctx->stats_out = x->old_twopassbuffer;
  373. }
  374. }
  375. if( 0 <= xerr ) {
  376. p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA;
  377. if( xvid_enc_stats.type == XVID_TYPE_PVOP )
  378. p->pict_type = FF_P_TYPE;
  379. else if( xvid_enc_stats.type == XVID_TYPE_BVOP )
  380. p->pict_type = FF_B_TYPE;
  381. else if( xvid_enc_stats.type == XVID_TYPE_SVOP )
  382. p->pict_type = FF_S_TYPE;
  383. else
  384. p->pict_type = FF_I_TYPE;
  385. if( xvid_enc_frame.out_flags & XVID_KEYFRAME ) {
  386. p->key_frame = 1;
  387. if( x->quicktime_format )
  388. return xvid_strip_vol_header(avctx, frame,
  389. xvid_enc_stats.hlength, xerr);
  390. } else
  391. p->key_frame = 0;
  392. return xerr;
  393. } else {
  394. av_log(avctx, AV_LOG_ERROR, "XviD: Encoding Error Occurred: %i\n", xerr);
  395. return -1;
  396. }
  397. }
  398. /**
  399. * Destroys the private context for the encoder.
  400. * All buffers are freed, and the XviD encoder context is destroyed.
  401. *
  402. * @param avctx AVCodecContext pointer to context
  403. * @return Returns 0, success guaranteed
  404. */
  405. int ff_xvid_encode_close(AVCodecContext *avctx) {
  406. xvid_context_t *x = avctx->priv_data;
  407. xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
  408. if( avctx->extradata != NULL )
  409. av_free(avctx->extradata);
  410. if( x->twopassbuffer != NULL ) {
  411. av_free(x->twopassbuffer);
  412. av_free(x->old_twopassbuffer);
  413. }
  414. if( x->twopassfile != NULL )
  415. av_free(x->twopassfile);
  416. if( x->intra_matrix != NULL )
  417. av_free(x->intra_matrix);
  418. if( x->inter_matrix != NULL )
  419. av_free(x->inter_matrix);
  420. return 0;
  421. }
  422. /**
  423. * Routine to create a global VO/VOL header for MP4 container.
  424. * What we do here is extract the header from the XviD bitstream
  425. * as it is encoded. We also strip the repeated headers from the
  426. * bitstream when a global header is requested for MPEG-4 ISO
  427. * compliance.
  428. *
  429. * @param avctx AVCodecContext pointer to context
  430. * @param frame Pointer to encoded frame data
  431. * @param header_len Length of header to search
  432. * @param frame_len Length of encoded frame data
  433. * @return Returns new length of frame data
  434. */
  435. int xvid_strip_vol_header(AVCodecContext *avctx,
  436. unsigned char *frame,
  437. unsigned int header_len,
  438. unsigned int frame_len) {
  439. int vo_len = 0, i;
  440. for( i = 0; i < header_len - 3; i++ ) {
  441. if( frame[i] == 0x00 &&
  442. frame[i+1] == 0x00 &&
  443. frame[i+2] == 0x01 &&
  444. frame[i+3] == 0xB6 ) {
  445. vo_len = i;
  446. break;
  447. }
  448. }
  449. if( vo_len > 0 ) {
  450. /* We need to store the header, so extract it */
  451. if( avctx->extradata == NULL ) {
  452. avctx->extradata = av_malloc(vo_len);
  453. memcpy(avctx->extradata, frame, vo_len);
  454. avctx->extradata_size = vo_len;
  455. }
  456. /* Less dangerous now, memmove properly copies the two
  457. chunks of overlapping data */
  458. memmove(frame, &(frame[vo_len]), frame_len - vo_len);
  459. return frame_len - vo_len;
  460. } else
  461. return frame_len;
  462. }
  463. /**
  464. * Routine to correct a possibly erroneous framerate being fed to us.
  465. * XviD currently chokes on framerates where the ticks per frame is
  466. * extremely large. This function works to correct problems in this area
  467. * by estimating a new framerate and taking the simpler fraction of
  468. * the two presented.
  469. *
  470. * @param avctx Context that contains the framerate to correct.
  471. */
  472. void xvid_correct_framerate(AVCodecContext *avctx) {
  473. int frate, fbase;
  474. int est_frate, est_fbase;
  475. int gcd;
  476. float est_fps, fps;
  477. frate = avctx->time_base.den;
  478. fbase = avctx->time_base.num;
  479. gcd = ff_gcd(frate, fbase);
  480. if( gcd > 1 ) {
  481. frate /= gcd;
  482. fbase /= gcd;
  483. }
  484. if( frate <= 65000 && fbase <= 65000 ) {
  485. avctx->time_base.den = frate;
  486. avctx->time_base.num = fbase;
  487. return;
  488. }
  489. fps = (float)frate / (float)fbase;
  490. est_fps = roundf(fps * 1000.0) / 1000.0;
  491. est_frate = (int)est_fps;
  492. if( est_fps > (int)est_fps ) {
  493. est_frate = (est_frate + 1) * 1000;
  494. est_fbase = (int)roundf((float)est_frate / est_fps);
  495. } else
  496. est_fbase = 1;
  497. gcd = ff_gcd(est_frate, est_fbase);
  498. if( gcd > 1 ) {
  499. est_frate /= gcd;
  500. est_fbase /= gcd;
  501. }
  502. if( fbase > est_fbase ) {
  503. avctx->time_base.den = est_frate;
  504. avctx->time_base.num = est_fbase;
  505. av_log(avctx, AV_LOG_DEBUG,
  506. "XviD: framerate re-estimated: %.2f, %.3f%% correction\n",
  507. est_fps, (((est_fps - fps)/fps) * 100.0));
  508. } else {
  509. avctx->time_base.den = frate;
  510. avctx->time_base.num = fbase;
  511. }
  512. }
  513. /*
  514. * XviD 2-Pass Kludge Section
  515. *
  516. * XviD's default 2-pass doesn't allow us to create data as we need to, so
  517. * this section spends time replacing the first pass plugin so we can write
  518. * statistic information as libavcodec requests in. We have another kludge
  519. * that allows us to pass data to the second pass in XviD without a custom
  520. * rate-control plugin.
  521. */
  522. /**
  523. * Initializes the two-pass plugin and context.
  524. *
  525. * @param param Input construction parameter structure
  526. * @param handle Private context handle
  527. * @return Returns XVID_ERR_xxxx on failure, or 0 on success.
  528. */
  529. static int xvid_ff_2pass_create(xvid_plg_create_t * param,
  530. void ** handle) {
  531. xvid_ff_pass1_t *x = (xvid_ff_pass1_t *)param->param;
  532. char *log = x->context->twopassbuffer;
  533. /* Do a quick bounds check */
  534. if( log == NULL )
  535. return XVID_ERR_FAIL;
  536. /* We use snprintf() */
  537. /* This is because we can safely prevent a buffer overflow */
  538. log[0] = 0;
  539. snprintf(log, BUFFER_REMAINING(log),
  540. "# ffmpeg 2-pass log file, using xvid codec\n");
  541. snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
  542. "# Do not modify. libxvidcore version: %d.%d.%d\n\n",
  543. XVID_VERSION_MAJOR(XVID_VERSION),
  544. XVID_VERSION_MINOR(XVID_VERSION),
  545. XVID_VERSION_PATCH(XVID_VERSION));
  546. *handle = x->context;
  547. return 0;
  548. }
  549. /**
  550. * Destroys the two-pass plugin context.
  551. *
  552. * @param ref Context pointer for the plugin
  553. * @param param Destrooy context
  554. * @return Returns 0, success guaranteed
  555. */
  556. static int xvid_ff_2pass_destroy(xvid_context_t *ref,
  557. xvid_plg_destroy_t *param) {
  558. /* Currently cannot think of anything to do on destruction */
  559. /* Still, the framework should be here for reference/use */
  560. if( ref->twopassbuffer != NULL )
  561. ref->twopassbuffer[0] = 0;
  562. return 0;
  563. }
  564. /**
  565. * Enables fast encode mode during the first pass.
  566. *
  567. * @param ref Context pointer for the plugin
  568. * @param param Frame data
  569. * @return Returns 0, success guaranteed
  570. */
  571. static int xvid_ff_2pass_before(xvid_context_t *ref,
  572. xvid_plg_data_t *param) {
  573. int motion_remove;
  574. int motion_replacements;
  575. int vop_remove;
  576. /* Nothing to do here, result is changed too much */
  577. if( param->zone && param->zone->mode == XVID_ZONE_QUANT )
  578. return 0;
  579. /* We can implement a 'turbo' first pass mode here */
  580. param->quant = 2;
  581. /* Init values */
  582. motion_remove = ~XVID_ME_CHROMA_PVOP &
  583. ~XVID_ME_CHROMA_BVOP &
  584. ~XVID_ME_EXTSEARCH16 &
  585. ~XVID_ME_ADVANCEDDIAMOND16;
  586. motion_replacements = XVID_ME_FAST_MODEINTERPOLATE |
  587. XVID_ME_SKIP_DELTASEARCH |
  588. XVID_ME_FASTREFINE16 |
  589. XVID_ME_BFRAME_EARLYSTOP;
  590. vop_remove = ~XVID_VOP_MODEDECISION_RD &
  591. ~XVID_VOP_FAST_MODEDECISION_RD &
  592. ~XVID_VOP_TRELLISQUANT &
  593. ~XVID_VOP_INTER4V &
  594. ~XVID_VOP_HQACPRED;
  595. param->vol_flags &= ~XVID_VOL_GMC;
  596. param->vop_flags &= vop_remove;
  597. param->motion_flags &= motion_remove;
  598. param->motion_flags |= motion_replacements;
  599. return 0;
  600. }
  601. /**
  602. * Captures statistic data and writes it during first pass.
  603. *
  604. * @param ref Context pointer for the plugin
  605. * @param param Statistic data
  606. * @return Returns XVID_ERR_xxxx on failure, or 0 on success
  607. */
  608. static int xvid_ff_2pass_after(xvid_context_t *ref,
  609. xvid_plg_data_t *param) {
  610. char *log = ref->twopassbuffer;
  611. char *frame_types = " ipbs";
  612. char frame_type;
  613. /* Quick bounds check */
  614. if( log == NULL )
  615. return XVID_ERR_FAIL;
  616. /* Convert the type given to us into a character */
  617. if( param->type < 5 && param->type > 0 ) {
  618. frame_type = frame_types[param->type];
  619. } else {
  620. return XVID_ERR_FAIL;
  621. }
  622. snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
  623. "%c %d %d %d %d %d %d\n",
  624. frame_type, param->stats.quant, param->stats.kblks, param->stats.mblks,
  625. param->stats.ublks, param->stats.length, param->stats.hlength);
  626. return 0;
  627. }
  628. /**
  629. * Dispatch function for our custom plugin.
  630. * This handles the dispatch for the XviD plugin. It passes data
  631. * on to other functions for actual processing.
  632. *
  633. * @param ref Context pointer for the plugin
  634. * @param cmd The task given for us to complete
  635. * @param p1 First parameter (varies)
  636. * @param p2 Second parameter (varies)
  637. * @return Returns XVID_ERR_xxxx on failure, or 0 on success
  638. */
  639. int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2) {
  640. switch( cmd ) {
  641. case XVID_PLG_INFO:
  642. case XVID_PLG_FRAME:
  643. return 0;
  644. case XVID_PLG_BEFORE:
  645. return xvid_ff_2pass_before(ref, p1);
  646. case XVID_PLG_CREATE:
  647. return xvid_ff_2pass_create(p1, p2);
  648. case XVID_PLG_AFTER:
  649. return xvid_ff_2pass_after(ref, p1);
  650. case XVID_PLG_DESTROY:
  651. return xvid_ff_2pass_destroy(ref, p1);
  652. default:
  653. return XVID_ERR_FAIL;
  654. }
  655. }
  656. /**
  657. * XviD codec definition for libavcodec.
  658. */
  659. AVCodec libxvid_encoder = {
  660. "libxvid",
  661. CODEC_TYPE_VIDEO,
  662. CODEC_ID_XVID,
  663. sizeof(xvid_context_t),
  664. ff_xvid_encode_init,
  665. ff_xvid_encode_frame,
  666. ff_xvid_encode_close,
  667. .pix_fmts= (enum PixelFormat[]){PIX_FMT_YUV420P, -1},
  668. };