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.

959 lines
33KB

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