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.

840 lines
28KB

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