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.

929 lines
32KB

  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. }
  393. /* Decide how we should decide blocks */
  394. switch (avctx->mb_decision) {
  395. case 2:
  396. x->vop_flags |= XVID_VOP_MODEDECISION_RD;
  397. x->me_flags |= XVID_ME_HALFPELREFINE8_RD |
  398. XVID_ME_QUARTERPELREFINE8_RD |
  399. XVID_ME_EXTSEARCH_RD |
  400. XVID_ME_CHECKPREDICTION_RD;
  401. case 1:
  402. if (!(x->vop_flags & XVID_VOP_MODEDECISION_RD))
  403. x->vop_flags |= XVID_VOP_FAST_MODEDECISION_RD;
  404. x->me_flags |= XVID_ME_HALFPELREFINE16_RD |
  405. XVID_ME_QUARTERPELREFINE16_RD;
  406. default:
  407. break;
  408. }
  409. x->vol_flags = 0;
  410. if (x->gmc) {
  411. x->vol_flags |= XVID_VOL_GMC;
  412. x->me_flags |= XVID_ME_GME_REFINE;
  413. }
  414. if (xvid_flags & AV_CODEC_FLAG_QPEL) {
  415. x->vol_flags |= XVID_VOL_QUARTERPEL;
  416. x->me_flags |= XVID_ME_QUARTERPELREFINE16;
  417. if (x->vop_flags & XVID_VOP_INTER4V)
  418. x->me_flags |= XVID_ME_QUARTERPELREFINE8;
  419. }
  420. xvid_gbl_init.version = XVID_VERSION;
  421. xvid_gbl_init.debug = 0;
  422. xvid_gbl_init.cpu_flags = 0;
  423. /* Initialize */
  424. xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
  425. /* Create the encoder reference */
  426. xvid_enc_create.version = XVID_VERSION;
  427. /* Store the desired frame size */
  428. xvid_enc_create.width =
  429. x->xsize = avctx->width;
  430. xvid_enc_create.height =
  431. x->ysize = avctx->height;
  432. /* Xvid can determine the proper profile to use */
  433. /* xvid_enc_create.profile = XVID_PROFILE_S_L3; */
  434. /* We don't use zones */
  435. xvid_enc_create.zones = NULL;
  436. xvid_enc_create.num_zones = 0;
  437. xvid_enc_create.num_threads = avctx->thread_count;
  438. xvid_enc_create.plugins = plugins;
  439. xvid_enc_create.num_plugins = 0;
  440. /* Initialize Buffers */
  441. x->twopassbuffer = NULL;
  442. x->old_twopassbuffer = NULL;
  443. x->twopassfile = NULL;
  444. if (xvid_flags & AV_CODEC_FLAG_PASS1) {
  445. rc2pass1.version = XVID_VERSION;
  446. rc2pass1.context = x;
  447. x->twopassbuffer = av_malloc(BUFFER_SIZE);
  448. x->old_twopassbuffer = av_malloc(BUFFER_SIZE);
  449. if (!x->twopassbuffer || !x->old_twopassbuffer) {
  450. av_log(avctx, AV_LOG_ERROR,
  451. "Xvid: Cannot allocate 2-pass log buffers\n");
  452. return AVERROR(ENOMEM);
  453. }
  454. x->twopassbuffer[0] =
  455. x->old_twopassbuffer[0] = 0;
  456. plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass;
  457. plugins[xvid_enc_create.num_plugins].param = &rc2pass1;
  458. xvid_enc_create.num_plugins++;
  459. } else if (xvid_flags & AV_CODEC_FLAG_PASS2) {
  460. rc2pass2.version = XVID_VERSION;
  461. rc2pass2.bitrate = avctx->bit_rate;
  462. fd = xvid_tempfile(avctx, "xvidff.", &x->twopassfile);
  463. if (fd < 0) {
  464. av_log(avctx, AV_LOG_ERROR, "Xvid: Cannot write 2-pass pipe\n");
  465. return fd;
  466. }
  467. if (!avctx->stats_in) {
  468. av_log(avctx, AV_LOG_ERROR,
  469. "Xvid: No 2-pass information loaded for second pass\n");
  470. return AVERROR_INVALIDDATA;
  471. }
  472. if (strlen(avctx->stats_in) >
  473. write(fd, avctx->stats_in, strlen(avctx->stats_in))) {
  474. close(fd);
  475. av_log(avctx, AV_LOG_ERROR, "Xvid: Cannot write to 2-pass pipe\n");
  476. return AVERROR(EIO);
  477. }
  478. close(fd);
  479. rc2pass2.filename = x->twopassfile;
  480. plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
  481. plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
  482. xvid_enc_create.num_plugins++;
  483. } else if (!(xvid_flags & AV_CODEC_FLAG_QSCALE)) {
  484. /* Single Pass Bitrate Control! */
  485. single.version = XVID_VERSION;
  486. single.bitrate = avctx->bit_rate;
  487. plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single;
  488. plugins[xvid_enc_create.num_plugins].param = &single;
  489. xvid_enc_create.num_plugins++;
  490. }
  491. if (avctx->lumi_masking != 0.0)
  492. x->lumi_aq = 1;
  493. if (x->lumi_aq && x->variance_aq) {
  494. x->variance_aq = 0;
  495. av_log(avctx, AV_LOG_WARNING,
  496. "variance_aq is ignored when lumi_aq is set.\n");
  497. }
  498. /* Luminance Masking */
  499. if (x->lumi_aq) {
  500. masking_l.method = 0;
  501. plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
  502. /* The old behavior is that when avctx->lumi_masking is specified,
  503. * plugins[...].param = NULL. Trying to keep the old behavior here. */
  504. plugins[xvid_enc_create.num_plugins].param =
  505. avctx->lumi_masking ? NULL : &masking_l;
  506. xvid_enc_create.num_plugins++;
  507. }
  508. /* Variance AQ */
  509. if (x->variance_aq) {
  510. masking_v.method = 1;
  511. plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
  512. plugins[xvid_enc_create.num_plugins].param = &masking_v;
  513. xvid_enc_create.num_plugins++;
  514. }
  515. /* SSIM */
  516. if (x->ssim) {
  517. plugins[xvid_enc_create.num_plugins].func = xvid_plugin_ssim;
  518. ssim.b_printstat = x->ssim == 2;
  519. ssim.acc = x->ssim_acc;
  520. ssim.cpu_flags = xvid_gbl_init.cpu_flags;
  521. ssim.b_visualize = 0;
  522. plugins[xvid_enc_create.num_plugins].param = &ssim;
  523. xvid_enc_create.num_plugins++;
  524. }
  525. /* Frame Rate and Key Frames */
  526. xvid_correct_framerate(avctx);
  527. xvid_enc_create.fincr = avctx->time_base.num;
  528. xvid_enc_create.fbase = avctx->time_base.den;
  529. if (avctx->gop_size > 0)
  530. xvid_enc_create.max_key_interval = avctx->gop_size;
  531. else
  532. xvid_enc_create.max_key_interval = 240; /* Xvid's best default */
  533. /* Quants */
  534. if (xvid_flags & AV_CODEC_FLAG_QSCALE)
  535. x->qscale = 1;
  536. else
  537. x->qscale = 0;
  538. xvid_enc_create.min_quant[0] = avctx->qmin;
  539. xvid_enc_create.min_quant[1] = avctx->qmin;
  540. xvid_enc_create.min_quant[2] = avctx->qmin;
  541. xvid_enc_create.max_quant[0] = avctx->qmax;
  542. xvid_enc_create.max_quant[1] = avctx->qmax;
  543. xvid_enc_create.max_quant[2] = avctx->qmax;
  544. /* Quant Matrices */
  545. x->intra_matrix =
  546. x->inter_matrix = NULL;
  547. #if FF_API_PRIVATE_OPT
  548. FF_DISABLE_DEPRECATION_WARNINGS
  549. if (avctx->mpeg_quant)
  550. x->mpeg_quant = avctx->mpeg_quant;
  551. FF_ENABLE_DEPRECATION_WARNINGS
  552. #endif
  553. if (x->mpeg_quant)
  554. x->vol_flags |= XVID_VOL_MPEGQUANT;
  555. if ((avctx->intra_matrix || avctx->inter_matrix)) {
  556. x->vol_flags |= XVID_VOL_MPEGQUANT;
  557. if (avctx->intra_matrix) {
  558. intra = avctx->intra_matrix;
  559. x->intra_matrix = av_malloc(sizeof(unsigned char) * 64);
  560. if (!x->intra_matrix)
  561. return AVERROR(ENOMEM);
  562. } else
  563. intra = NULL;
  564. if (avctx->inter_matrix) {
  565. inter = avctx->inter_matrix;
  566. x->inter_matrix = av_malloc(sizeof(unsigned char) * 64);
  567. if (!x->inter_matrix)
  568. return AVERROR(ENOMEM);
  569. } else
  570. inter = NULL;
  571. for (i = 0; i < 64; i++) {
  572. if (intra)
  573. x->intra_matrix[i] = (unsigned char) intra[i];
  574. if (inter)
  575. x->inter_matrix[i] = (unsigned char) inter[i];
  576. }
  577. }
  578. /* Misc Settings */
  579. xvid_enc_create.frame_drop_ratio = 0;
  580. xvid_enc_create.global = 0;
  581. if (xvid_flags & AV_CODEC_FLAG_CLOSED_GOP)
  582. xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
  583. /* Determines which codec mode we are operating in */
  584. avctx->extradata = NULL;
  585. avctx->extradata_size = 0;
  586. if (xvid_flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  587. /* In this case, we are claiming to be MPEG-4 */
  588. x->quicktime_format = 1;
  589. avctx->codec_id = AV_CODEC_ID_MPEG4;
  590. } else {
  591. /* We are claiming to be Xvid */
  592. x->quicktime_format = 0;
  593. if (!avctx->codec_tag)
  594. avctx->codec_tag = AV_RL32("xvid");
  595. }
  596. /* Bframes */
  597. xvid_enc_create.max_bframes = avctx->max_b_frames;
  598. xvid_enc_create.bquant_offset = 100 * avctx->b_quant_offset;
  599. xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor;
  600. if (avctx->max_b_frames > 0 && !x->quicktime_format)
  601. xvid_enc_create.global |= XVID_GLOBAL_PACKED;
  602. /* Encode a dummy frame to get the extradata immediately */
  603. if (x->quicktime_format) {
  604. AVFrame *picture;
  605. AVPacket packet;
  606. int got_packet, ret;
  607. av_init_packet(&packet);
  608. picture = av_frame_alloc();
  609. if (!picture)
  610. return AVERROR(ENOMEM);
  611. xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
  612. if (xerr) {
  613. av_frame_free(&picture);
  614. av_log(avctx, AV_LOG_ERROR, "Xvid: Could not create encoder reference\n");
  615. return AVERROR_UNKNOWN;
  616. }
  617. x->encoder_handle = xvid_enc_create.handle;
  618. picture->width = avctx->width;
  619. picture->height = avctx->height;
  620. picture->format = avctx->pix_fmt;
  621. if ((ret = av_frame_get_buffer(picture, 32)) < 0) {
  622. xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
  623. av_frame_free(&picture);
  624. return ret;
  625. }
  626. ret = xvid_encode_frame(avctx, &packet, picture, &got_packet);
  627. if (!ret && got_packet)
  628. av_packet_unref(&packet);
  629. av_frame_free(&picture);
  630. xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
  631. }
  632. /* Create encoder context */
  633. xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
  634. if (xerr) {
  635. av_log(avctx, AV_LOG_ERROR, "Xvid: Could not create encoder reference\n");
  636. return -1;
  637. }
  638. x->encoder_handle = xvid_enc_create.handle;
  639. return 0;
  640. }
  641. static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  642. const AVFrame *picture, int *got_packet)
  643. {
  644. int xerr, i, ret, user_packet = !!pkt->data;
  645. struct xvid_context *x = avctx->priv_data;
  646. int mb_width = (avctx->width + 15) / 16;
  647. int mb_height = (avctx->height + 15) / 16;
  648. char *tmp;
  649. xvid_enc_frame_t xvid_enc_frame = { 0 };
  650. xvid_enc_stats_t xvid_enc_stats = { 0 };
  651. if (!user_packet &&
  652. (ret = av_new_packet(pkt, mb_width * mb_height * MAX_MB_BYTES + AV_INPUT_BUFFER_MIN_SIZE)) < 0) {
  653. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  654. return ret;
  655. }
  656. /* Start setting up the frame */
  657. xvid_enc_frame.version = XVID_VERSION;
  658. xvid_enc_stats.version = XVID_VERSION;
  659. /* Let Xvid know where to put the frame. */
  660. xvid_enc_frame.bitstream = pkt->data;
  661. xvid_enc_frame.length = pkt->size;
  662. /* Initialize input image fields */
  663. if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
  664. av_log(avctx, AV_LOG_ERROR,
  665. "Xvid: Color spaces other than 420P not supported\n");
  666. return -1;
  667. }
  668. xvid_enc_frame.input.csp = XVID_CSP_PLANAR; /* YUV420P */
  669. for (i = 0; i < 4; i++) {
  670. xvid_enc_frame.input.plane[i] = picture->data[i];
  671. xvid_enc_frame.input.stride[i] = picture->linesize[i];
  672. }
  673. /* Encoder Flags */
  674. xvid_enc_frame.vop_flags = x->vop_flags;
  675. xvid_enc_frame.vol_flags = x->vol_flags;
  676. xvid_enc_frame.motion = x->me_flags;
  677. xvid_enc_frame.type =
  678. picture->pict_type == AV_PICTURE_TYPE_I ? XVID_TYPE_IVOP :
  679. picture->pict_type == AV_PICTURE_TYPE_P ? XVID_TYPE_PVOP :
  680. picture->pict_type == AV_PICTURE_TYPE_B ? XVID_TYPE_BVOP :
  681. XVID_TYPE_AUTO;
  682. /* Pixel aspect ratio setting */
  683. if (avctx->sample_aspect_ratio.num < 1 || avctx->sample_aspect_ratio.num > 255 ||
  684. avctx->sample_aspect_ratio.den < 1 || avctx->sample_aspect_ratio.den > 255) {
  685. av_log(avctx, AV_LOG_ERROR, "Invalid pixel aspect ratio %i/%i\n",
  686. avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
  687. return -1;
  688. }
  689. xvid_enc_frame.par = XVID_PAR_EXT;
  690. xvid_enc_frame.par_width = avctx->sample_aspect_ratio.num;
  691. xvid_enc_frame.par_height = avctx->sample_aspect_ratio.den;
  692. /* Quant Setting */
  693. if (x->qscale)
  694. xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA;
  695. else
  696. xvid_enc_frame.quant = 0;
  697. /* Matrices */
  698. xvid_enc_frame.quant_intra_matrix = x->intra_matrix;
  699. xvid_enc_frame.quant_inter_matrix = x->inter_matrix;
  700. /* Encode */
  701. xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE,
  702. &xvid_enc_frame, &xvid_enc_stats);
  703. /* Two-pass log buffer swapping */
  704. avctx->stats_out = NULL;
  705. if (x->twopassbuffer) {
  706. tmp = x->old_twopassbuffer;
  707. x->old_twopassbuffer = x->twopassbuffer;
  708. x->twopassbuffer = tmp;
  709. x->twopassbuffer[0] = 0;
  710. if (x->old_twopassbuffer[0] != 0) {
  711. avctx->stats_out = x->old_twopassbuffer;
  712. }
  713. }
  714. if (xerr > 0) {
  715. uint8_t *sd = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_FACTOR,
  716. sizeof(int));
  717. if (!sd)
  718. return AVERROR(ENOMEM);
  719. *(int *)sd = xvid_enc_stats.quant * FF_QP2LAMBDA;
  720. *got_packet = 1;
  721. #if FF_API_CODED_FRAME
  722. FF_DISABLE_DEPRECATION_WARNINGS
  723. avctx->coded_frame->quality = xvid_enc_stats.quant * FF_QP2LAMBDA;
  724. if (xvid_enc_stats.type == XVID_TYPE_PVOP)
  725. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_P;
  726. else if (xvid_enc_stats.type == XVID_TYPE_BVOP)
  727. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_B;
  728. else if (xvid_enc_stats.type == XVID_TYPE_SVOP)
  729. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_S;
  730. else
  731. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  732. FF_ENABLE_DEPRECATION_WARNINGS
  733. #endif
  734. if (xvid_enc_frame.out_flags & XVID_KEYFRAME) {
  735. #if FF_API_CODED_FRAME
  736. FF_DISABLE_DEPRECATION_WARNINGS
  737. avctx->coded_frame->key_frame = 1;
  738. FF_ENABLE_DEPRECATION_WARNINGS
  739. #endif
  740. pkt->flags |= AV_PKT_FLAG_KEY;
  741. if (x->quicktime_format)
  742. return xvid_strip_vol_header(avctx, pkt,
  743. xvid_enc_stats.hlength, xerr);
  744. } else {
  745. #if FF_API_CODED_FRAME
  746. FF_DISABLE_DEPRECATION_WARNINGS
  747. avctx->coded_frame->key_frame = 0;
  748. FF_ENABLE_DEPRECATION_WARNINGS
  749. #endif
  750. }
  751. pkt->size = xerr;
  752. return 0;
  753. } else {
  754. if (!user_packet)
  755. av_packet_unref(pkt);
  756. if (!xerr)
  757. return 0;
  758. av_log(avctx, AV_LOG_ERROR,
  759. "Xvid: Encoding Error Occurred: %i\n", xerr);
  760. return xerr;
  761. }
  762. }
  763. static av_cold int xvid_encode_close(AVCodecContext *avctx)
  764. {
  765. struct xvid_context *x = avctx->priv_data;
  766. if (x->encoder_handle) {
  767. xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
  768. x->encoder_handle = NULL;
  769. }
  770. av_freep(&avctx->extradata);
  771. if (x->twopassbuffer) {
  772. av_free(x->twopassbuffer);
  773. av_free(x->old_twopassbuffer);
  774. }
  775. av_free(x->twopassfile);
  776. av_free(x->intra_matrix);
  777. av_free(x->inter_matrix);
  778. return 0;
  779. }
  780. #define OFFSET(x) offsetof(struct xvid_context, x)
  781. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  782. static const AVOption options[] = {
  783. { "lumi_aq", "Luminance masking AQ", OFFSET(lumi_aq), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  784. { "variance_aq", "Variance AQ", OFFSET(variance_aq), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  785. { "ssim", "Show SSIM information to stdout", OFFSET(ssim), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, VE, "ssim" },
  786. { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "ssim" },
  787. { "avg", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "ssim" },
  788. { "frame", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, "ssim" },
  789. { "ssim_acc", "SSIM accuracy", OFFSET(ssim_acc), AV_OPT_TYPE_INT, { .i64 = 2 }, 0, 4, VE },
  790. { "gmc", "use GMC", OFFSET(gmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  791. { "me_quality", "Motion estimation quality", OFFSET(me_quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 6, VE },
  792. { "mpeg_quant", "Use MPEG quantizers instead of H.263", OFFSET(mpeg_quant), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
  793. { NULL },
  794. };
  795. static const AVClass xvid_class = {
  796. .class_name = "libxvid",
  797. .item_name = av_default_item_name,
  798. .option = options,
  799. .version = LIBAVUTIL_VERSION_INT,
  800. };
  801. AVCodec ff_libxvid_encoder = {
  802. .name = "libxvid",
  803. .long_name = NULL_IF_CONFIG_SMALL("libxvidcore MPEG-4 part 2"),
  804. .type = AVMEDIA_TYPE_VIDEO,
  805. .id = AV_CODEC_ID_MPEG4,
  806. .priv_data_size = sizeof(struct xvid_context),
  807. .init = xvid_encode_init,
  808. .encode2 = xvid_encode_frame,
  809. .close = xvid_encode_close,
  810. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
  811. .priv_class = &xvid_class,
  812. .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE |
  813. FF_CODEC_CAP_INIT_CLEANUP,
  814. };