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.

771 lines
25KB

  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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. /**
  29. * Buffer management macros.
  30. */
  31. #define BUFFER_SIZE 1024
  32. #define BUFFER_REMAINING(x) (BUFFER_SIZE - strlen(x))
  33. #define BUFFER_CAT(x) (&((x)[strlen(x)]))
  34. /* For PPC Use */
  35. #if HAVE_ALTIVEC==1
  36. extern int has_altivec(void);
  37. #endif
  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. #if HAVE_ALTIVEC==1
  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. x->twopassfile = av_malloc(BUFFER_SIZE);
  201. if( x->twopassfile == NULL ) {
  202. av_log(avctx, AV_LOG_ERROR,
  203. "XviD: Cannot allocate 2-pass buffer\n");
  204. return -1;
  205. }
  206. strcpy(x->twopassfile, "/tmp/xvidff.XXXXXX");
  207. fd = mkstemp(x->twopassfile);
  208. if( fd == -1 ) {
  209. av_log(avctx, AV_LOG_ERROR,
  210. "XviD: Cannot write 2-pass pipe\n");
  211. return -1;
  212. }
  213. if( avctx->stats_in == NULL ) {
  214. av_log(avctx, AV_LOG_ERROR,
  215. "XviD: No 2-pass information loaded for second pass\n");
  216. return -1;
  217. }
  218. if( strlen(avctx->stats_in) >
  219. write(fd, avctx->stats_in, strlen(avctx->stats_in)) ) {
  220. close(fd);
  221. av_log(avctx, AV_LOG_ERROR,
  222. "XviD: Cannot write to 2-pass pipe\n");
  223. return -1;
  224. }
  225. close(fd);
  226. rc2pass2.filename = x->twopassfile;
  227. plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
  228. plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
  229. xvid_enc_create.num_plugins++;
  230. } else if( !(xvid_flags & CODEC_FLAG_QSCALE) ) {
  231. /* Single Pass Bitrate Control! */
  232. memset(&single, 0, sizeof(xvid_plugin_single_t));
  233. single.version = XVID_VERSION;
  234. single.bitrate = avctx->bit_rate;
  235. plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single;
  236. plugins[xvid_enc_create.num_plugins].param = &single;
  237. xvid_enc_create.num_plugins++;
  238. }
  239. /* Luminance Masking */
  240. if( 0.0 != avctx->lumi_masking ) {
  241. plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
  242. plugins[xvid_enc_create.num_plugins].param = NULL;
  243. xvid_enc_create.num_plugins++;
  244. }
  245. /* Frame Rate and Key Frames */
  246. xvid_correct_framerate(avctx);
  247. xvid_enc_create.fincr = avctx->frame_rate_base;
  248. xvid_enc_create.fbase = avctx->frame_rate;
  249. if( avctx->gop_size > 0 )
  250. xvid_enc_create.max_key_interval = avctx->gop_size;
  251. else
  252. xvid_enc_create.max_key_interval = 240; /* XviD's best default */
  253. /* Quants */
  254. if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1;
  255. else x->qscale = 0;
  256. xvid_enc_create.min_quant[0] = avctx->qmin;
  257. xvid_enc_create.min_quant[1] = avctx->qmin;
  258. xvid_enc_create.min_quant[2] = avctx->qmin;
  259. xvid_enc_create.max_quant[0] = avctx->qmax;
  260. xvid_enc_create.max_quant[1] = avctx->qmax;
  261. xvid_enc_create.max_quant[2] = avctx->qmax;
  262. /* Quant Matrices */
  263. x->intra_matrix = x->inter_matrix = NULL;
  264. if( avctx->mpeg_quant )
  265. x->vol_flags |= XVID_VOL_MPEGQUANT;
  266. if( (avctx->intra_matrix || avctx->inter_matrix) ) {
  267. x->vol_flags |= XVID_VOL_MPEGQUANT;
  268. if( avctx->intra_matrix ) {
  269. intra = avctx->intra_matrix;
  270. x->intra_matrix = av_malloc(sizeof(unsigned char) * 64);
  271. } else
  272. intra = NULL;
  273. if( avctx->inter_matrix ) {
  274. inter = avctx->inter_matrix;
  275. x->inter_matrix = av_malloc(sizeof(unsigned char) * 64);
  276. } else
  277. inter = NULL;
  278. for( i = 0; i < 64; i++ ) {
  279. if( intra )
  280. x->intra_matrix[i] = (unsigned char)intra[i];
  281. if( inter )
  282. x->inter_matrix[i] = (unsigned char)inter[i];
  283. }
  284. }
  285. /* Misc Settings */
  286. xvid_enc_create.frame_drop_ratio = 0;
  287. xvid_enc_create.global = 0;
  288. if( xvid_flags & CODEC_FLAG_CLOSED_GOP )
  289. xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
  290. /* Determines which codec mode we are operating in */
  291. avctx->extradata = NULL;
  292. avctx->extradata_size = 0;
  293. if( xvid_flags & CODEC_FLAG_GLOBAL_HEADER ) {
  294. /* In this case, we are claiming to be MPEG4 */
  295. x->quicktime_format = 1;
  296. avctx->codec_id = CODEC_ID_MPEG4;
  297. } else {
  298. /* We are claiming to be XviD */
  299. x->quicktime_format = 0;
  300. avctx->codec_tag = ff_get_fourcc("xvid");
  301. }
  302. /* Bframes */
  303. xvid_enc_create.max_bframes = avctx->max_b_frames;
  304. xvid_enc_create.bquant_offset = avctx->b_quant_offset;
  305. xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor;
  306. if( avctx->max_b_frames > 0 && !x->quicktime_format ) xvid_enc_create.global |= XVID_GLOBAL_PACKED;
  307. /* Create encoder context */
  308. xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
  309. if( xerr ) {
  310. av_log(avctx, AV_LOG_ERROR, "XviD: Could not create encoder reference\n");
  311. return -1;
  312. }
  313. x->encoder_handle = xvid_enc_create.handle;
  314. avctx->coded_frame = &x->encoded_picture;
  315. return 0;
  316. }
  317. /**
  318. * Encodes a single frame.
  319. *
  320. * @param avctx AVCodecContext pointer to context
  321. * @param frame Pointer to encoded frame buffer
  322. * @param buf_size Size of encoded frame buffer
  323. * @param data Pointer to AVFrame of unencoded frame
  324. * @return Returns 0 on success, -1 on failure
  325. */
  326. int ff_xvid_encode_frame(AVCodecContext *avctx,
  327. unsigned char *frame, int buf_size, void *data) {
  328. int xerr, i;
  329. char *tmp;
  330. xvid_context_t *x = avctx->priv_data;
  331. AVFrame *picture = data;
  332. AVFrame *p = &(x->encoded_picture);
  333. xvid_enc_frame_t xvid_enc_frame;
  334. xvid_enc_stats_t xvid_enc_stats;
  335. /* Start setting up the frame */
  336. memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame));
  337. xvid_enc_frame.version = XVID_VERSION;
  338. memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats));
  339. xvid_enc_stats.version = XVID_VERSION;
  340. *p = *picture;
  341. /* Let XviD know where to put the frame. */
  342. xvid_enc_frame.bitstream = frame;
  343. xvid_enc_frame.length = buf_size;
  344. /* Initialize input image fields */
  345. if( avctx->pix_fmt != PIX_FMT_YUV420P ) {
  346. av_log(avctx, AV_LOG_ERROR, "XviD: Color spaces other than 420p not supported\n");
  347. return -1;
  348. }
  349. xvid_enc_frame.input.csp = XVID_CSP_PLANAR; /* YUV420P */
  350. for( i = 0; i < 4; i++ ) {
  351. xvid_enc_frame.input.plane[i] = picture->data[i];
  352. xvid_enc_frame.input.stride[i] = picture->linesize[i];
  353. }
  354. /* Encoder Flags */
  355. xvid_enc_frame.vop_flags = x->vop_flags;
  356. xvid_enc_frame.vol_flags = x->vol_flags;
  357. xvid_enc_frame.motion = x->me_flags;
  358. xvid_enc_frame.type = XVID_TYPE_AUTO;
  359. /* Quant Setting */
  360. if( x->qscale ) xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA;
  361. else xvid_enc_frame.quant = 0;
  362. /* Matrices */
  363. xvid_enc_frame.quant_intra_matrix = x->intra_matrix;
  364. xvid_enc_frame.quant_inter_matrix = x->inter_matrix;
  365. /* Encode */
  366. xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE,
  367. &xvid_enc_frame, &xvid_enc_stats);
  368. /* Two-pass log buffer swapping */
  369. avctx->stats_out = NULL;
  370. if( x->twopassbuffer ) {
  371. tmp = x->old_twopassbuffer;
  372. x->old_twopassbuffer = x->twopassbuffer;
  373. x->twopassbuffer = tmp;
  374. x->twopassbuffer[0] = 0;
  375. if( x->old_twopassbuffer[0] != 0 ) {
  376. avctx->stats_out = x->old_twopassbuffer;
  377. }
  378. }
  379. if( 0 <= xerr ) {
  380. p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA;
  381. if( xvid_enc_stats.type == XVID_TYPE_PVOP )
  382. p->pict_type = FF_P_TYPE;
  383. else if( xvid_enc_stats.type == XVID_TYPE_BVOP )
  384. p->pict_type = FF_B_TYPE;
  385. else if( xvid_enc_stats.type == XVID_TYPE_SVOP )
  386. p->pict_type = FF_SP_TYPE;
  387. else
  388. p->pict_type = FF_I_TYPE;
  389. if( xvid_enc_frame.out_flags & XVID_KEYFRAME ) {
  390. p->key_frame = 1;
  391. if( x->quicktime_format )
  392. return xvid_strip_vol_header(avctx, frame,
  393. xvid_enc_stats.hlength, xerr);
  394. } else
  395. p->key_frame = 0;
  396. return xerr;
  397. } else {
  398. av_log(avctx, AV_LOG_ERROR, "XviD: Encoding Error Occurred: %i\n", xerr);
  399. return -1;
  400. }
  401. }
  402. /**
  403. * Destroys the private context for the encoder.
  404. * All buffers are freed, and the XviD encoder context is destroyed.
  405. *
  406. * @param avctx AVCodecContext pointer to context
  407. * @return Returns 0, success guaranteed
  408. */
  409. int ff_xvid_encode_close(AVCodecContext *avctx) {
  410. xvid_context_t *x = avctx->priv_data;
  411. xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
  412. if( avctx->extradata != NULL )
  413. av_free(avctx->extradata);
  414. if( x->twopassbuffer != NULL ) {
  415. av_free(x->twopassbuffer);
  416. av_free(x->old_twopassbuffer);
  417. }
  418. if( x->twopassfile != NULL )
  419. av_free(x->twopassfile);
  420. if( x->intra_matrix != NULL )
  421. av_free(x->intra_matrix);
  422. if( x->inter_matrix != NULL )
  423. av_free(x->inter_matrix);
  424. return 0;
  425. }
  426. /**
  427. * Routine to create a global VO/VOL header for MP4 container.
  428. * What we do here is extract the header from the XviD bitstream
  429. * as it is encoded. We also strip the repeated headers from the
  430. * bitstream when a global header is requested for MPEG-4 ISO
  431. * compliance.
  432. *
  433. * @param avctx AVCodecContext pointer to context
  434. * @param frame Pointer to encoded frame data
  435. * @param header_len Length of header to search
  436. * @param frame_len Length of encoded frame data
  437. * @return Returns new length of frame data
  438. */
  439. int xvid_strip_vol_header(AVCodecContext *avctx,
  440. unsigned char *frame,
  441. unsigned int header_len,
  442. unsigned int frame_len) {
  443. int vo_len = 0, i;
  444. for( i = 0; i < header_len - 3; i++ ) {
  445. if( frame[i] == 0x00 &&
  446. frame[i+1] == 0x00 &&
  447. frame[i+2] == 0x01 &&
  448. frame[i+3] == 0xB6 ) {
  449. vo_len = i;
  450. break;
  451. }
  452. }
  453. if( vo_len > 0 ) {
  454. /* We need to store the header, so extract it */
  455. if( avctx->extradata == NULL ) {
  456. avctx->extradata = av_malloc(vo_len);
  457. memcpy(avctx->extradata, frame, vo_len);
  458. avctx->extradata_size = vo_len;
  459. }
  460. /* Less dangerous now, memmove properly copies the two
  461. chunks of overlapping data */
  462. memmove(frame, &(frame[vo_len]), frame_len - vo_len);
  463. return frame_len - vo_len;
  464. } else
  465. return frame_len;
  466. }
  467. /**
  468. * Routine to correct a possibly erroneous framerate being fed to us.
  469. * XviD currently chokes on framerates where the ticks per frame is
  470. * extremely large. This function works to correct problems in this area
  471. * by estimating a new framerate and taking the simpler fraction of
  472. * the two presented.
  473. *
  474. * @param avctx Context that contains the framerate to correct.
  475. */
  476. void xvid_correct_framerate(AVCodecContext *avctx) {
  477. int frate, fbase;
  478. int est_frate, est_fbase;
  479. int gcd;
  480. float est_fps, fps;
  481. frate = avctx->frame_rate;
  482. fbase = avctx->frame_rate_base;
  483. gcd = ff_gcd(frate, fbase);
  484. if( gcd > 1 ) {
  485. frate /= gcd;
  486. fbase /= gcd;
  487. }
  488. if( frate <= 65000 && fbase <= 65000 ) {
  489. avctx->frame_rate = frate;
  490. avctx->frame_rate_base = fbase;
  491. return;
  492. }
  493. fps = (float)frate / (float)fbase;
  494. est_fps = roundf(fps * 1000.0) / 1000.0;
  495. est_frate = (int)est_fps;
  496. if( est_fps > (int)est_fps ) {
  497. est_frate = (est_frate + 1) * 1000;
  498. est_fbase = (int)roundf((float)est_frate / est_fps);
  499. } else
  500. est_fbase = 1;
  501. gcd = ff_gcd(est_frate, est_fbase);
  502. if( gcd > 1 ) {
  503. est_frate /= gcd;
  504. est_fbase /= gcd;
  505. }
  506. if( fbase > est_fbase ) {
  507. avctx->frame_rate = est_frate;
  508. avctx->frame_rate_base = est_fbase;
  509. av_log(avctx, AV_LOG_DEBUG,
  510. "XviD: framerate re-estimated: %.2f, %.3f%% correction\n",
  511. est_fps, (((est_fps - fps)/fps) * 100.0));
  512. } else {
  513. avctx->frame_rate = frate;
  514. avctx->frame_rate_base = fbase;
  515. }
  516. }
  517. /*
  518. * XviD 2-Pass Kludge Section
  519. *
  520. * XviD's default 2-pass doesn't allow us to create data as we need to, so
  521. * this section spends time replacing the first pass plugin so we can write
  522. * statistic information as libavcodec requests in. We have another kludge
  523. * that allows us to pass data to the second pass in XviD without a custom
  524. * rate-control plugin.
  525. */
  526. /**
  527. * Initializes the two-pass plugin and context.
  528. *
  529. * @param param Input construction parameter structure
  530. * @param handle Private context handle
  531. * @return Returns XVID_ERR_xxxx on failure, or 0 on success.
  532. */
  533. static int xvid_ff_2pass_create(xvid_plg_create_t * param,
  534. void ** handle) {
  535. xvid_ff_pass1_t *x = (xvid_ff_pass1_t *)param->param;
  536. char *log = x->context->twopassbuffer;
  537. /* Do a quick bounds check */
  538. if( log == NULL )
  539. return XVID_ERR_FAIL;
  540. /* We use snprintf() */
  541. /* This is because we can safely prevent a buffer overflow */
  542. log[0] = 0;
  543. snprintf(log, BUFFER_REMAINING(log),
  544. "# ffmpeg 2-pass log file, using xvid codec\n");
  545. snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
  546. "# Do not modify. libxvidcore version: %d.%d.%d\n\n",
  547. XVID_VERSION_MAJOR(XVID_VERSION),
  548. XVID_VERSION_MINOR(XVID_VERSION),
  549. XVID_VERSION_PATCH(XVID_VERSION));
  550. *handle = x->context;
  551. return 0;
  552. }
  553. /**
  554. * Destroys the two-pass plugin context.
  555. *
  556. * @param ref Context pointer for the plugin
  557. * @param param Destrooy context
  558. * @return Returns 0, success guaranteed
  559. */
  560. static int xvid_ff_2pass_destroy(xvid_context_t *ref,
  561. xvid_plg_destroy_t *param) {
  562. /* Currently cannot think of anything to do on destruction */
  563. /* Still, the framework should be here for reference/use */
  564. if( ref->twopassbuffer != NULL )
  565. ref->twopassbuffer[0] = 0;
  566. return 0;
  567. }
  568. /**
  569. * Enables fast encode mode during the first pass.
  570. *
  571. * @param ref Context pointer for the plugin
  572. * @param param Frame data
  573. * @return Returns 0, success guaranteed
  574. */
  575. static int xvid_ff_2pass_before(xvid_context_t *ref,
  576. xvid_plg_data_t *param) {
  577. int motion_remove;
  578. int motion_replacements;
  579. int vop_remove;
  580. /* Nothing to do here, result is changed too much */
  581. if( param->zone && param->zone->mode == XVID_ZONE_QUANT )
  582. return 0;
  583. /* We can implement a 'turbo' first pass mode here */
  584. param->quant = 2;
  585. /* Init values */
  586. motion_remove = ~XVID_ME_CHROMA_PVOP &
  587. ~XVID_ME_CHROMA_BVOP &
  588. ~XVID_ME_EXTSEARCH16 &
  589. ~XVID_ME_ADVANCEDDIAMOND16;
  590. motion_replacements = XVID_ME_FAST_MODEINTERPOLATE |
  591. XVID_ME_SKIP_DELTASEARCH |
  592. XVID_ME_FASTREFINE16 |
  593. XVID_ME_BFRAME_EARLYSTOP;
  594. vop_remove = ~XVID_VOP_MODEDECISION_RD &
  595. ~XVID_VOP_FAST_MODEDECISION_RD &
  596. ~XVID_VOP_TRELLISQUANT &
  597. ~XVID_VOP_INTER4V &
  598. ~XVID_VOP_HQACPRED;
  599. param->vol_flags &= ~XVID_VOL_GMC;
  600. param->vop_flags &= vop_remove;
  601. param->motion_flags &= motion_remove;
  602. param->motion_flags |= motion_replacements;
  603. return 0;
  604. }
  605. /**
  606. * Captures statistic data and writes it during first pass.
  607. *
  608. * @param ref Context pointer for the plugin
  609. * @param param Statistic data
  610. * @return Returns XVID_ERR_xxxx on failure, or 0 on success
  611. */
  612. static int xvid_ff_2pass_after(xvid_context_t *ref,
  613. xvid_plg_data_t *param) {
  614. char *log = ref->twopassbuffer;
  615. char *frame_types = " ipbs";
  616. char frame_type;
  617. /* Quick bounds check */
  618. if( log == NULL )
  619. return XVID_ERR_FAIL;
  620. /* Convert the type given to us into a character */
  621. if( param->type < 5 && param->type > 0 ) {
  622. frame_type = frame_types[param->type];
  623. } else {
  624. return XVID_ERR_FAIL;
  625. }
  626. snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
  627. "%c %d %d %d %d %d %d\n",
  628. frame_type, param->stats.quant, param->stats.kblks, param->stats.mblks,
  629. param->stats.ublks, param->stats.length, param->stats.hlength);
  630. return 0;
  631. }
  632. /**
  633. * Dispatch function for our custom plugin.
  634. * This handles the dispatch for the XviD plugin. It passes data
  635. * on to other functions for actual processing.
  636. *
  637. * @param ref Context pointer for the plugin
  638. * @param cmd The task given for us to complete
  639. * @param p1 First parameter (varies)
  640. * @param p2 Second parameter (varies)
  641. * @return Returns XVID_ERR_xxxx on failure, or 0 on success
  642. */
  643. int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2) {
  644. switch( cmd ) {
  645. case XVID_PLG_INFO:
  646. case XVID_PLG_FRAME:
  647. return 0;
  648. case XVID_PLG_BEFORE:
  649. return xvid_ff_2pass_before(ref, p1);
  650. case XVID_PLG_CREATE:
  651. return xvid_ff_2pass_create(p1, p2);
  652. case XVID_PLG_AFTER:
  653. return xvid_ff_2pass_after(ref, p1);
  654. case XVID_PLG_DESTROY:
  655. return xvid_ff_2pass_destroy(ref, p1);
  656. default:
  657. return XVID_ERR_FAIL;
  658. }
  659. }
  660. /**
  661. * XviD codec definition for libavcodec.
  662. */
  663. AVCodec xvid_encoder = {
  664. "xvid",
  665. CODEC_TYPE_VIDEO,
  666. CODEC_ID_XVID,
  667. sizeof(xvid_context_t),
  668. ff_xvid_encode_init,
  669. ff_xvid_encode_frame,
  670. ff_xvid_encode_close
  671. };