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 library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file xvidmpeg4.c
  21. * Interface to xvidcore for MPEG-4 compliant encoding.
  22. * @author Adam Thayer (krevnik@comcast.net)
  23. */
  24. #include <xvid.h>
  25. #include <unistd.h>
  26. #include "common.h"
  27. #include "avcodec.h"
  28. #include "internal.h"
  29. /**
  30. * Buffer management macros.
  31. */
  32. #define BUFFER_SIZE 1024
  33. #define BUFFER_REMAINING(x) (BUFFER_SIZE - strlen(x))
  34. #define BUFFER_CAT(x) (&((x)[strlen(x)]))
  35. /* For PPC Use */
  36. #if HAVE_ALTIVEC==1
  37. extern int has_altivec(void);
  38. #endif
  39. /**
  40. * Structure for the private XviD context.
  41. * This stores all the private context for the codec.
  42. */
  43. typedef struct xvid_context {
  44. void *encoder_handle; /** Handle for XviD Encoder */
  45. int xsize, ysize; /** Frame size */
  46. int vop_flags; /** VOP flags for XviD Encoder */
  47. int vol_flags; /** VOL flags for XviD Encoder */
  48. int me_flags; /** Motion Estimation flags */
  49. int qscale; /** Do we use constant scale? */
  50. int quicktime_format; /** Are we in a QT-based format? */
  51. AVFrame encoded_picture; /** Encoded frame information */
  52. char *twopassbuffer; /** Character buffer for two-pass */
  53. char *old_twopassbuffer; /** Old character buffer (two-pass) */
  54. char *twopassfile; /** second pass temp file name */
  55. unsigned char *intra_matrix; /** P-Frame Quant Matrix */
  56. unsigned char *inter_matrix; /** I-Frame Quant Matrix */
  57. } xvid_context_t;
  58. /**
  59. * Structure for the private first-pass plugin.
  60. */
  61. typedef struct xvid_ff_pass1 {
  62. int version; /** XviD version */
  63. xvid_context_t *context; /** Pointer to private context */
  64. } xvid_ff_pass1_t;
  65. /* Prototypes - See function implementation for details */
  66. int xvid_strip_vol_header(AVCodecContext *avctx, unsigned char *frame, unsigned int header_len, unsigned int frame_len);
  67. int xvid_ff_2pass(void *ref, int opt, void *p1, void *p2);
  68. void xvid_correct_framerate(AVCodecContext *avctx);
  69. /**
  70. * Creates the private context for the encoder.
  71. * All buffers are allocated, settings are loaded from the user,
  72. * and the encoder context created.
  73. *
  74. * @param avctx AVCodecContext pointer to context
  75. * @return Returns 0 on success, -1 on failure
  76. */
  77. int ff_xvid_encode_init(AVCodecContext *avctx) {
  78. int xerr, i;
  79. int xvid_flags = avctx->flags;
  80. xvid_context_t *x = avctx->priv_data;
  81. uint16_t *intra, *inter;
  82. int fd;
  83. xvid_plugin_single_t single;
  84. xvid_ff_pass1_t rc2pass1;
  85. xvid_plugin_2pass2_t rc2pass2;
  86. xvid_gbl_init_t xvid_gbl_init;
  87. xvid_enc_create_t xvid_enc_create;
  88. xvid_enc_plugin_t plugins[7];
  89. /* Bring in VOP flags from ffmpeg command-line */
  90. x->vop_flags = XVID_VOP_HALFPEL; /* Bare minimum quality */
  91. if( xvid_flags & CODEC_FLAG_4MV )
  92. x->vop_flags |= XVID_VOP_INTER4V; /* Level 3 */
  93. if( xvid_flags & CODEC_FLAG_TRELLIS_QUANT)
  94. x->vop_flags |= XVID_VOP_TRELLISQUANT; /* Level 5 */
  95. if( xvid_flags & CODEC_FLAG_AC_PRED )
  96. x->vop_flags |= XVID_VOP_HQACPRED; /* Level 6 */
  97. if( xvid_flags & CODEC_FLAG_GRAY )
  98. x->vop_flags |= XVID_VOP_GREYSCALE;
  99. /* Decide which ME quality setting to use */
  100. x->me_flags = 0;
  101. switch( avctx->me_method ) {
  102. case ME_FULL: /* Quality 6 */
  103. x->me_flags |= XVID_ME_EXTSEARCH16
  104. | XVID_ME_EXTSEARCH8;
  105. case ME_EPZS: /* Quality 4 */
  106. x->me_flags |= XVID_ME_ADVANCEDDIAMOND8
  107. | XVID_ME_HALFPELREFINE8
  108. | XVID_ME_CHROMA_PVOP
  109. | XVID_ME_CHROMA_BVOP;
  110. case ME_LOG: /* Quality 2 */
  111. case ME_PHODS:
  112. case ME_X1:
  113. x->me_flags |= XVID_ME_ADVANCEDDIAMOND16
  114. | XVID_ME_HALFPELREFINE16;
  115. case ME_ZERO: /* Quality 0 */
  116. default:
  117. break;
  118. }
  119. /* Decide how we should decide blocks */
  120. switch( avctx->mb_decision ) {
  121. case 2:
  122. x->vop_flags |= XVID_VOP_MODEDECISION_RD;
  123. x->me_flags |= XVID_ME_HALFPELREFINE8_RD
  124. | XVID_ME_QUARTERPELREFINE8_RD
  125. | XVID_ME_EXTSEARCH_RD
  126. | XVID_ME_CHECKPREDICTION_RD;
  127. case 1:
  128. if( !(x->vop_flags & XVID_VOP_MODEDECISION_RD) )
  129. x->vop_flags |= XVID_VOP_FAST_MODEDECISION_RD;
  130. x->me_flags |= XVID_ME_HALFPELREFINE16_RD
  131. | XVID_ME_QUARTERPELREFINE16_RD;
  132. default:
  133. break;
  134. }
  135. /* Bring in VOL flags from ffmpeg command-line */
  136. x->vol_flags = 0;
  137. if( xvid_flags & CODEC_FLAG_GMC ) {
  138. x->vol_flags |= XVID_VOL_GMC;
  139. x->me_flags |= XVID_ME_GME_REFINE;
  140. }
  141. if( xvid_flags & CODEC_FLAG_QPEL ) {
  142. x->vol_flags |= XVID_VOL_QUARTERPEL;
  143. x->me_flags |= XVID_ME_QUARTERPELREFINE16;
  144. if( x->vop_flags & XVID_VOP_INTER4V )
  145. x->me_flags |= XVID_ME_QUARTERPELREFINE8;
  146. }
  147. memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));
  148. xvid_gbl_init.version = XVID_VERSION;
  149. xvid_gbl_init.debug = 0;
  150. #ifdef ARCH_POWERPC
  151. /* XviD's PPC support is borked, use libavcodec to detect */
  152. #if HAVE_ALTIVEC==1
  153. if( has_altivec() ) {
  154. xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ALTIVEC;
  155. } else
  156. #endif
  157. xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
  158. #else
  159. /* XviD can detect on x86 */
  160. xvid_gbl_init.cpu_flags = 0;
  161. #endif
  162. /* Initialize */
  163. xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
  164. /* Create the encoder reference */
  165. memset(&xvid_enc_create, 0, sizeof(xvid_enc_create));
  166. xvid_enc_create.version = XVID_VERSION;
  167. /* Store the desired frame size */
  168. xvid_enc_create.width = x->xsize = avctx->width;
  169. xvid_enc_create.height = x->ysize = avctx->height;
  170. /* XviD can determine the proper profile to use */
  171. /* xvid_enc_create.profile = XVID_PROFILE_S_L3; */
  172. /* We don't use zones or threads */
  173. xvid_enc_create.zones = NULL;
  174. xvid_enc_create.num_zones = 0;
  175. xvid_enc_create.num_threads = 0;
  176. xvid_enc_create.plugins = plugins;
  177. xvid_enc_create.num_plugins = 0;
  178. /* Initialize Buffers */
  179. x->twopassbuffer = NULL;
  180. x->old_twopassbuffer = NULL;
  181. x->twopassfile = NULL;
  182. if( xvid_flags & CODEC_FLAG_PASS1 ) {
  183. memset(&rc2pass1, 0, sizeof(xvid_ff_pass1_t));
  184. rc2pass1.version = XVID_VERSION;
  185. rc2pass1.context = x;
  186. x->twopassbuffer = av_malloc(BUFFER_SIZE);
  187. x->old_twopassbuffer = av_malloc(BUFFER_SIZE);
  188. if( x->twopassbuffer == NULL || x->old_twopassbuffer == NULL ) {
  189. av_log(avctx, AV_LOG_ERROR,
  190. "XviD: Cannot allocate 2-pass log buffers\n");
  191. return -1;
  192. }
  193. x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0;
  194. plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass;
  195. plugins[xvid_enc_create.num_plugins].param = &rc2pass1;
  196. xvid_enc_create.num_plugins++;
  197. } else if( xvid_flags & CODEC_FLAG_PASS2 ) {
  198. memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t));
  199. rc2pass2.version = XVID_VERSION;
  200. rc2pass2.bitrate = avctx->bit_rate;
  201. fd = av_tempfile("xvidff.", &(x->twopassfile));
  202. if( fd == -1 ) {
  203. av_log(avctx, AV_LOG_ERROR,
  204. "XviD: Cannot write 2-pass pipe\n");
  205. return -1;
  206. }
  207. if( avctx->stats_in == NULL ) {
  208. av_log(avctx, AV_LOG_ERROR,
  209. "XviD: No 2-pass information loaded for second pass\n");
  210. return -1;
  211. }
  212. if( strlen(avctx->stats_in) >
  213. write(fd, avctx->stats_in, strlen(avctx->stats_in)) ) {
  214. close(fd);
  215. av_log(avctx, AV_LOG_ERROR,
  216. "XviD: Cannot write to 2-pass pipe\n");
  217. return -1;
  218. }
  219. close(fd);
  220. rc2pass2.filename = x->twopassfile;
  221. plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
  222. plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
  223. xvid_enc_create.num_plugins++;
  224. } else if( !(xvid_flags & CODEC_FLAG_QSCALE) ) {
  225. /* Single Pass Bitrate Control! */
  226. memset(&single, 0, sizeof(xvid_plugin_single_t));
  227. single.version = XVID_VERSION;
  228. single.bitrate = avctx->bit_rate;
  229. plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single;
  230. plugins[xvid_enc_create.num_plugins].param = &single;
  231. xvid_enc_create.num_plugins++;
  232. }
  233. /* Luminance Masking */
  234. if( 0.0 != avctx->lumi_masking ) {
  235. plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
  236. plugins[xvid_enc_create.num_plugins].param = NULL;
  237. xvid_enc_create.num_plugins++;
  238. }
  239. /* Frame Rate and Key Frames */
  240. xvid_correct_framerate(avctx);
  241. xvid_enc_create.fincr = avctx->time_base.num;
  242. xvid_enc_create.fbase = avctx->time_base.den;
  243. if( avctx->gop_size > 0 )
  244. xvid_enc_create.max_key_interval = avctx->gop_size;
  245. else
  246. xvid_enc_create.max_key_interval = 240; /* XviD's best default */
  247. /* Quants */
  248. if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1;
  249. else x->qscale = 0;
  250. xvid_enc_create.min_quant[0] = avctx->qmin;
  251. xvid_enc_create.min_quant[1] = avctx->qmin;
  252. xvid_enc_create.min_quant[2] = avctx->qmin;
  253. xvid_enc_create.max_quant[0] = avctx->qmax;
  254. xvid_enc_create.max_quant[1] = avctx->qmax;
  255. xvid_enc_create.max_quant[2] = avctx->qmax;
  256. /* Quant Matrices */
  257. x->intra_matrix = x->inter_matrix = NULL;
  258. if( avctx->mpeg_quant )
  259. x->vol_flags |= XVID_VOL_MPEGQUANT;
  260. if( (avctx->intra_matrix || avctx->inter_matrix) ) {
  261. x->vol_flags |= XVID_VOL_MPEGQUANT;
  262. if( avctx->intra_matrix ) {
  263. intra = avctx->intra_matrix;
  264. x->intra_matrix = av_malloc(sizeof(unsigned char) * 64);
  265. } else
  266. intra = NULL;
  267. if( avctx->inter_matrix ) {
  268. inter = avctx->inter_matrix;
  269. x->inter_matrix = av_malloc(sizeof(unsigned char) * 64);
  270. } else
  271. inter = NULL;
  272. for( i = 0; i < 64; i++ ) {
  273. if( intra )
  274. x->intra_matrix[i] = (unsigned char)intra[i];
  275. if( inter )
  276. x->inter_matrix[i] = (unsigned char)inter[i];
  277. }
  278. }
  279. /* Misc Settings */
  280. xvid_enc_create.frame_drop_ratio = 0;
  281. xvid_enc_create.global = 0;
  282. if( xvid_flags & CODEC_FLAG_CLOSED_GOP )
  283. xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
  284. /* Determines which codec mode we are operating in */
  285. avctx->extradata = NULL;
  286. avctx->extradata_size = 0;
  287. if( xvid_flags & CODEC_FLAG_GLOBAL_HEADER ) {
  288. /* In this case, we are claiming to be MPEG4 */
  289. x->quicktime_format = 1;
  290. avctx->codec_id = CODEC_ID_MPEG4;
  291. } else {
  292. /* We are claiming to be XviD */
  293. x->quicktime_format = 0;
  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 xvid_encoder = {
  658. "xvid",
  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. };