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.

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