You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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