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.

766 lines
24KB

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