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.

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