FFmpeg
libxvid.c
Go to the documentation of this file.
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 FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * Interface to xvidcore for MPEG-4 compliant encoding.
25  * @author Adam Thayer (krevnik@comcast.net)
26  */
27 
28 #include <stdio.h>
29 #include <string.h>
30 #include <xvid.h>
31 
32 #include "libavutil/avassert.h"
33 #include "libavutil/file_open.h"
34 #include "libavutil/internal.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/mem.h"
38 #include "libavutil/opt.h"
39 
40 #include "avcodec.h"
41 #include "codec_internal.h"
42 #include "encode.h"
43 #include "mpegutils.h"
44 
45 #if HAVE_UNISTD_H
46 #include <unistd.h>
47 #endif
48 
49 #if HAVE_IO_H
50 #include <io.h>
51 #endif
52 
53 /**
54  * Buffer management macros.
55  */
56 #define BUFFER_SIZE 1024
57 #define BUFFER_REMAINING(x) (BUFFER_SIZE - strlen(x))
58 #define BUFFER_CAT(x) (&((x)[strlen(x)]))
59 
60 /**
61  * Structure for the private Xvid context.
62  * This stores all the private context for the codec.
63  */
64 struct xvid_context {
65  AVClass *class;
66  void *encoder_handle; /**< Handle for Xvid encoder */
67  int xsize; /**< Frame x size */
68  int ysize; /**< Frame y size */
69  int vop_flags; /**< VOP flags for Xvid encoder */
70  int vol_flags; /**< VOL flags for Xvid encoder */
71  int me_flags; /**< Motion Estimation flags */
72  int qscale; /**< Do we use constant scale? */
73  int quicktime_format; /**< Are we in a QT-based format? */
74  char *twopassbuffer; /**< Character buffer for two-pass */
75  char *old_twopassbuffer; /**< Old character buffer (two-pass) */
76  char *twopassfile; /**< second pass temp file name */
77  int twopassfd;
78  unsigned char *intra_matrix; /**< P-Frame Quant Matrix */
79  unsigned char *inter_matrix; /**< I-Frame Quant Matrix */
80  int lumi_aq; /**< Lumi masking as an aq method */
81  int variance_aq; /**< Variance adaptive quantization */
82  int ssim; /**< SSIM information display mode */
83  int ssim_acc; /**< SSIM accuracy. 0: accurate. 4: fast. */
84  int gmc;
85  int me_quality; /**< Motion estimation quality. 0: fast 6: best. */
86  int mpeg_quant; /**< Quantization type. 0: H.263, 1: MPEG */
87 };
88 
89 /**
90  * Structure for the private first-pass plugin.
91  */
92 struct xvid_ff_pass1 {
93  int version; /**< Xvid version */
94  struct xvid_context *context; /**< Pointer to private context */
95 };
96 
97 static int xvid_encode_close(AVCodecContext *avctx);
98 static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
99  const AVFrame *picture, int *got_packet);
100 
101 
102 /*
103  * Xvid 2-Pass Kludge Section
104  *
105  * Xvid's default 2-pass doesn't allow us to create data as we need to, so
106  * this section spends time replacing the first pass plugin so we can write
107  * statistic information as libavcodec requests in. We have another kludge
108  * that allows us to pass data to the second pass in Xvid without a custom
109  * rate-control plugin.
110  */
111 
112 /**
113  * Initialize the two-pass plugin and context.
114  *
115  * @param param Input construction parameter structure
116  * @param handle Private context handle
117  * @return Returns XVID_ERR_xxxx on failure, or 0 on success.
118  */
119 static int xvid_ff_2pass_create(xvid_plg_create_t *param, void **handle)
120 {
121  struct xvid_ff_pass1 *x = (struct xvid_ff_pass1 *) param->param;
122  char *log = x->context->twopassbuffer;
123 
124  /* Do a quick bounds check */
125  if (!log)
126  return XVID_ERR_FAIL;
127 
128  /* We use snprintf() */
129  /* This is because we can safely prevent a buffer overflow */
130  log[0] = 0;
132  "# ffmpeg 2-pass log file, using xvid codec\n");
134  "# Do not modify. libxvidcore version: %d.%d.%d\n\n",
135  XVID_VERSION_MAJOR(XVID_VERSION),
136  XVID_VERSION_MINOR(XVID_VERSION),
137  XVID_VERSION_PATCH(XVID_VERSION));
138 
139  *handle = x->context;
140  return 0;
141 }
142 
143 /**
144  * Destroy the two-pass plugin context.
145  *
146  * @param ref Context pointer for the plugin
147  * @param param Destroy context
148  * @return Returns 0, success guaranteed
149  */
151  xvid_plg_destroy_t *param)
152 {
153  /* Currently cannot think of anything to do on destruction */
154  /* Still, the framework should be here for reference/use */
155  if (ref->twopassbuffer)
156  ref->twopassbuffer[0] = 0;
157  return 0;
158 }
159 
160 /**
161  * Enable fast encode mode during the first pass.
162  *
163  * @param ref Context pointer for the plugin
164  * @param param Frame data
165  * @return Returns 0, success guaranteed
166  */
168  xvid_plg_data_t *param)
169 {
170  int motion_remove;
171  int motion_replacements;
172  int vop_remove;
173 
174  /* Nothing to do here, result is changed too much */
175  if (param->zone && param->zone->mode == XVID_ZONE_QUANT)
176  return 0;
177 
178  /* We can implement a 'turbo' first pass mode here */
179  param->quant = 2;
180 
181  /* Init values */
182  motion_remove = ~XVID_ME_CHROMA_PVOP &
183  ~XVID_ME_CHROMA_BVOP &
184  ~XVID_ME_EXTSEARCH16 &
185  ~XVID_ME_ADVANCEDDIAMOND16;
186  motion_replacements = XVID_ME_FAST_MODEINTERPOLATE |
187  XVID_ME_SKIP_DELTASEARCH |
188  XVID_ME_FASTREFINE16 |
189  XVID_ME_BFRAME_EARLYSTOP;
190  vop_remove = ~XVID_VOP_MODEDECISION_RD &
191  ~XVID_VOP_FAST_MODEDECISION_RD &
192  ~XVID_VOP_TRELLISQUANT &
193  ~XVID_VOP_INTER4V &
194  ~XVID_VOP_HQACPRED;
195 
196  param->vol_flags &= ~XVID_VOL_GMC;
197  param->vop_flags &= vop_remove;
198  param->motion_flags &= motion_remove;
199  param->motion_flags |= motion_replacements;
200 
201  return 0;
202 }
203 
204 /**
205  * Capture statistic data and write it during first pass.
206  *
207  * @param ref Context pointer for the plugin
208  * @param param Statistic data
209  * @return Returns XVID_ERR_xxxx on failure, or 0 on success
210  */
212  xvid_plg_data_t *param)
213 {
214  char *log = ref->twopassbuffer;
215  const char *frame_types = " ipbs";
216  char frame_type;
217 
218  /* Quick bounds check */
219  if (!log)
220  return XVID_ERR_FAIL;
221 
222  /* Convert the type given to us into a character */
223  if (param->type < 5 && param->type > 0)
224  frame_type = frame_types[param->type];
225  else
226  return XVID_ERR_FAIL;
227 
229  "%c %d %d %d %d %d %d\n",
230  frame_type, param->stats.quant, param->stats.kblks,
231  param->stats.mblks, param->stats.ublks,
232  param->stats.length, param->stats.hlength);
233 
234  return 0;
235 }
236 
237 /**
238  * Dispatch function for our custom plugin.
239  * This handles the dispatch for the Xvid plugin. It passes data
240  * on to other functions for actual processing.
241  *
242  * @param ref Context pointer for the plugin
243  * @param cmd The task given for us to complete
244  * @param p1 First parameter (varies)
245  * @param p2 Second parameter (varies)
246  * @return Returns XVID_ERR_xxxx on failure, or 0 on success
247  */
248 static int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2)
249 {
250  switch (cmd) {
251  case XVID_PLG_INFO:
252  case XVID_PLG_FRAME:
253  return 0;
254  case XVID_PLG_BEFORE:
255  return xvid_ff_2pass_before(ref, p1);
256  case XVID_PLG_CREATE:
257  return xvid_ff_2pass_create(p1, p2);
258  case XVID_PLG_AFTER:
259  return xvid_ff_2pass_after(ref, p1);
260  case XVID_PLG_DESTROY:
261  return xvid_ff_2pass_destroy(ref, p1);
262  default:
263  return XVID_ERR_FAIL;
264  }
265 }
266 
267 /**
268  * Routine to create a global VO/VOL header for MP4 container.
269  * What we do here is extract the header from the Xvid bitstream
270  * as it is encoded. We also strip the repeated headers from the
271  * bitstream when a global header is requested for MPEG-4 ISO
272  * compliance.
273  *
274  * @param avctx AVCodecContext pointer to context
275  * @param frame Pointer to encoded frame data
276  * @param header_len Length of header to search
277  * @param frame_len Length of encoded frame data
278  * @return Returns new length of frame data
279  */
281  unsigned int header_len,
282  unsigned int frame_len)
283 {
284  int vo_len = 0, i;
285 
286  for (i = 0; i < header_len - 3; i++) {
287  if (pkt->data[i] == 0x00 &&
288  pkt->data[i + 1] == 0x00 &&
289  pkt->data[i + 2] == 0x01 &&
290  pkt->data[i + 3] == 0xB6) {
291  vo_len = i;
292  break;
293  }
294  }
295 
296  if (vo_len > 0) {
297  /* We need to store the header, so extract it */
298  if (!avctx->extradata) {
299  avctx->extradata = av_malloc(vo_len);
300  if (!avctx->extradata)
301  return AVERROR(ENOMEM);
302  memcpy(avctx->extradata, pkt->data, vo_len);
303  avctx->extradata_size = vo_len;
304  }
305  /* Less dangerous now, memmove properly copies the two
306  * chunks of overlapping data */
307  memmove(pkt->data, &pkt->data[vo_len], frame_len - vo_len);
308  pkt->size = frame_len - vo_len;
309  }
310  return 0;
311 }
312 
313 /**
314  * Routine to correct a possibly erroneous framerate being fed to us.
315  * Xvid currently chokes on framerates where the ticks per frame is
316  * extremely large. This function works to correct problems in this area
317  * by estimating a new framerate and taking the simpler fraction of
318  * the two presented.
319  *
320  * @param avctx Context that contains the framerate to correct.
321  */
323 {
324  int frate, fbase;
325  int est_frate, est_fbase;
326  int gcd;
327  float est_fps, fps;
328 
329  frate = avctx->time_base.den;
330  fbase = avctx->time_base.num;
331 
332  gcd = av_gcd(frate, fbase);
333  if (gcd > 1) {
334  frate /= gcd;
335  fbase /= gcd;
336  }
337 
338  if (frate <= 65000 && fbase <= 65000) {
339  avctx->time_base.den = frate;
340  avctx->time_base.num = fbase;
341  return;
342  }
343 
344  fps = (float) frate / (float) fbase;
345  est_fps = roundf(fps * 1000.0) / 1000.0;
346 
347  est_frate = (int) est_fps;
348  if (est_fps > (int) est_fps) {
349  est_frate = (est_frate + 1) * 1000;
350  est_fbase = (int) roundf((float) est_frate / est_fps);
351  } else
352  est_fbase = 1;
353 
354  gcd = av_gcd(est_frate, est_fbase);
355  if (gcd > 1) {
356  est_frate /= gcd;
357  est_fbase /= gcd;
358  }
359 
360  if (fbase > est_fbase) {
361  avctx->time_base.den = est_frate;
362  avctx->time_base.num = est_fbase;
363  av_log(avctx, AV_LOG_DEBUG,
364  "Xvid: framerate re-estimated: %.2f, %.3f%% correction\n",
365  est_fps, (((est_fps - fps) / fps) * 100.0));
366  } else {
367  avctx->time_base.den = frate;
368  avctx->time_base.num = fbase;
369  }
370 }
371 
373 {
374  int xerr, i, ret = -1;
375  int xvid_flags = avctx->flags;
376  struct xvid_context *x = avctx->priv_data;
377  uint16_t *intra, *inter;
378  int fd;
379 
380  xvid_plugin_single_t single = { 0 };
381  struct xvid_ff_pass1 rc2pass1 = { 0 };
382  xvid_plugin_2pass2_t rc2pass2 = { 0 };
383  xvid_plugin_lumimasking_t masking_l = { 0 }; /* For lumi masking */
384  xvid_plugin_lumimasking_t masking_v = { 0 }; /* For variance AQ */
385  xvid_plugin_ssim_t ssim = { 0 };
386  xvid_gbl_init_t xvid_gbl_init = { 0 };
387  xvid_enc_create_t xvid_enc_create = { 0 };
388  xvid_enc_plugin_t plugins[4];
389 
390  x->twopassfd = -1;
391 
392  /* Bring in VOP flags from ffmpeg command-line */
393  x->vop_flags = XVID_VOP_HALFPEL; /* Bare minimum quality */
394  if (xvid_flags & AV_CODEC_FLAG_4MV)
395  x->vop_flags |= XVID_VOP_INTER4V; /* Level 3 */
396  if (avctx->trellis)
397  x->vop_flags |= XVID_VOP_TRELLISQUANT; /* Level 5 */
398  if (xvid_flags & AV_CODEC_FLAG_AC_PRED)
399  x->vop_flags |= XVID_VOP_HQACPRED; /* Level 6 */
400  if (xvid_flags & AV_CODEC_FLAG_GRAY)
401  x->vop_flags |= XVID_VOP_GREYSCALE;
402 
403  /* Decide which ME quality setting to use */
404  x->me_flags = 0;
405  switch (x->me_quality) {
406  case 6:
407  case 5:
408  x->me_flags |= XVID_ME_EXTSEARCH16 |
409  XVID_ME_EXTSEARCH8;
410  case 4:
411  case 3:
412  x->me_flags |= XVID_ME_ADVANCEDDIAMOND8 |
413  XVID_ME_HALFPELREFINE8 |
414  XVID_ME_CHROMA_PVOP |
415  XVID_ME_CHROMA_BVOP;
416  case 2:
417  case 1:
418  x->me_flags |= XVID_ME_ADVANCEDDIAMOND16 |
419  XVID_ME_HALFPELREFINE16;
420  }
421 
422  /* Decide how we should decide blocks */
423  switch (avctx->mb_decision) {
424  case FF_MB_DECISION_RD:
425  x->vop_flags |= XVID_VOP_MODEDECISION_RD;
426  x->me_flags |= XVID_ME_HALFPELREFINE8_RD |
427  XVID_ME_QUARTERPELREFINE8_RD |
428  XVID_ME_EXTSEARCH_RD |
429  XVID_ME_CHECKPREDICTION_RD;
430  case FF_MB_DECISION_BITS:
431  if (!(x->vop_flags & XVID_VOP_MODEDECISION_RD))
432  x->vop_flags |= XVID_VOP_FAST_MODEDECISION_RD;
433  x->me_flags |= XVID_ME_HALFPELREFINE16_RD |
434  XVID_ME_QUARTERPELREFINE16_RD;
435  default:
436  break;
437  }
438 
439  /* Bring in VOL flags from ffmpeg command-line */
440  x->vol_flags = 0;
441  if (x->gmc) {
442  x->vol_flags |= XVID_VOL_GMC;
443  x->me_flags |= XVID_ME_GME_REFINE;
444  }
445  if (xvid_flags & AV_CODEC_FLAG_QPEL) {
446  x->vol_flags |= XVID_VOL_QUARTERPEL;
447  x->me_flags |= XVID_ME_QUARTERPELREFINE16;
448  if (x->vop_flags & XVID_VOP_INTER4V)
449  x->me_flags |= XVID_ME_QUARTERPELREFINE8;
450  }
451 
452  xvid_gbl_init.version = XVID_VERSION;
453  xvid_gbl_init.debug = 0;
454  xvid_gbl_init.cpu_flags = 0;
455 
456  /* Initialize */
457  xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
458 
459  /* Create the encoder reference */
460  xvid_enc_create.version = XVID_VERSION;
461 
462  /* Store the desired frame size */
463  xvid_enc_create.width =
464  x->xsize = avctx->width;
465  xvid_enc_create.height =
466  x->ysize = avctx->height;
467 
468  /* Xvid can determine the proper profile to use */
469  /* xvid_enc_create.profile = XVID_PROFILE_S_L3; */
470 
471  /* We don't use zones */
472  xvid_enc_create.zones = NULL;
473  xvid_enc_create.num_zones = 0;
474 
475  xvid_enc_create.num_threads = avctx->thread_count;
476 #if (XVID_VERSION <= 0x010303) && (XVID_VERSION >= 0x010300)
477  /* workaround for a bug in libxvidcore */
478  if (avctx->height <= 16) {
479  if (avctx->thread_count < 2) {
480  xvid_enc_create.num_threads = 0;
481  } else {
482  av_log(avctx, AV_LOG_ERROR,
483  "Too small height for threads > 1.");
484  return AVERROR(EINVAL);
485  }
486  }
487 #endif
488 
489  xvid_enc_create.plugins = plugins;
490  xvid_enc_create.num_plugins = 0;
491 
492  /* Initialize Buffers */
493  x->twopassbuffer = NULL;
494  x->old_twopassbuffer = NULL;
495  x->twopassfile = NULL;
496 
497  if (xvid_flags & AV_CODEC_FLAG_PASS1) {
498  rc2pass1.version = XVID_VERSION;
499  rc2pass1.context = x;
502  if (!x->twopassbuffer || !x->old_twopassbuffer) {
503  av_log(avctx, AV_LOG_ERROR,
504  "Xvid: Cannot allocate 2-pass log buffers\n");
505  return AVERROR(ENOMEM);
506  }
507  x->twopassbuffer[0] =
508  x->old_twopassbuffer[0] = 0;
509 
510  plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass;
511  plugins[xvid_enc_create.num_plugins].param = &rc2pass1;
512  xvid_enc_create.num_plugins++;
513  } else if (xvid_flags & AV_CODEC_FLAG_PASS2) {
514  rc2pass2.version = XVID_VERSION;
515  rc2pass2.bitrate = avctx->bit_rate;
516 
517  fd = avpriv_tempfile("xvidff.", &x->twopassfile, 0, avctx);
518  if (fd < 0) {
519  av_log(avctx, AV_LOG_ERROR, "Xvid: Cannot write 2-pass pipe\n");
520  return fd;
521  }
522  x->twopassfd = fd;
523 
524  if (!avctx->stats_in) {
525  av_log(avctx, AV_LOG_ERROR,
526  "Xvid: No 2-pass information loaded for second pass\n");
527  return AVERROR(EINVAL);
528  }
529 
530  ret = write(fd, avctx->stats_in, strlen(avctx->stats_in));
531  if (ret == -1)
532  ret = AVERROR(errno);
533  else if (strlen(avctx->stats_in) > ret) {
534  av_log(avctx, AV_LOG_ERROR, "Xvid: Cannot write to 2-pass pipe\n");
535  ret = AVERROR(EIO);
536  }
537  if (ret < 0)
538  return ret;
539 
540  rc2pass2.filename = x->twopassfile;
541  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
542  plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
543  xvid_enc_create.num_plugins++;
544  } else if (!(xvid_flags & AV_CODEC_FLAG_QSCALE)) {
545  /* Single Pass Bitrate Control! */
546  single.version = XVID_VERSION;
547  single.bitrate = avctx->bit_rate;
548 
549  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single;
550  plugins[xvid_enc_create.num_plugins].param = &single;
551  xvid_enc_create.num_plugins++;
552  }
553 
554  if (avctx->lumi_masking != 0.0)
555  x->lumi_aq = 1;
556 
557  /* Luminance Masking */
558  if (x->lumi_aq) {
559  masking_l.method = 0;
560  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
561 
562  /* The old behavior is that when avctx->lumi_masking is specified,
563  * plugins[...].param = NULL. Trying to keep the old behavior here. */
564  plugins[xvid_enc_create.num_plugins].param =
565  avctx->lumi_masking ? NULL : &masking_l;
566  xvid_enc_create.num_plugins++;
567  }
568 
569  /* Variance AQ */
570  if (x->variance_aq) {
571  masking_v.method = 1;
572  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
573  plugins[xvid_enc_create.num_plugins].param = &masking_v;
574  xvid_enc_create.num_plugins++;
575  }
576 
577  if (x->lumi_aq && x->variance_aq )
578  av_log(avctx, AV_LOG_INFO,
579  "Both lumi_aq and variance_aq are enabled. The resulting quality"
580  "will be the worse one of the two effects made by the AQ.\n");
581 
582  /* SSIM */
583  if (x->ssim) {
584  plugins[xvid_enc_create.num_plugins].func = xvid_plugin_ssim;
585  ssim.b_printstat = x->ssim == 2;
586  ssim.acc = x->ssim_acc;
587  ssim.cpu_flags = xvid_gbl_init.cpu_flags;
588  ssim.b_visualize = 0;
589  plugins[xvid_enc_create.num_plugins].param = &ssim;
590  xvid_enc_create.num_plugins++;
591  }
592 
593  /* Frame Rate and Key Frames */
594  xvid_correct_framerate(avctx);
595  xvid_enc_create.fincr = avctx->time_base.num;
596  xvid_enc_create.fbase = avctx->time_base.den;
597  if (avctx->gop_size > 0)
598  xvid_enc_create.max_key_interval = avctx->gop_size;
599  else
600  xvid_enc_create.max_key_interval = 240; /* Xvid's best default */
601 
602  /* Quants */
603  if (xvid_flags & AV_CODEC_FLAG_QSCALE)
604  x->qscale = 1;
605  else
606  x->qscale = 0;
607 
608  xvid_enc_create.min_quant[0] = avctx->qmin;
609  xvid_enc_create.min_quant[1] = avctx->qmin;
610  xvid_enc_create.min_quant[2] = avctx->qmin;
611  xvid_enc_create.max_quant[0] = avctx->qmax;
612  xvid_enc_create.max_quant[1] = avctx->qmax;
613  xvid_enc_create.max_quant[2] = avctx->qmax;
614 
615  /* Quant Matrices */
616  x->intra_matrix =
617  x->inter_matrix = NULL;
618 
620  if (ret < 0)
621  return ret;
622 
623  if (x->mpeg_quant)
624  x->vol_flags |= XVID_VOL_MPEGQUANT;
625  if ((avctx->intra_matrix || avctx->inter_matrix)) {
626  x->vol_flags |= XVID_VOL_MPEGQUANT;
627 
628  if (avctx->intra_matrix) {
629  intra = avctx->intra_matrix;
630  x->intra_matrix = av_malloc(sizeof(unsigned char) * 64);
631  if (!x->intra_matrix)
632  return AVERROR(ENOMEM);
633  } else
634  intra = NULL;
635  if (avctx->inter_matrix) {
636  inter = avctx->inter_matrix;
637  x->inter_matrix = av_malloc(sizeof(unsigned char) * 64);
638  if (!x->inter_matrix)
639  return AVERROR(ENOMEM);
640  } else
641  inter = NULL;
642 
643  for (i = 0; i < 64; i++) {
644  if (intra)
645  x->intra_matrix[i] = (unsigned char) intra[i];
646  if (inter)
647  x->inter_matrix[i] = (unsigned char) inter[i];
648  }
649  }
650 
651  /* Misc Settings */
652  xvid_enc_create.frame_drop_ratio = 0;
653  xvid_enc_create.global = 0;
654  if (xvid_flags & AV_CODEC_FLAG_CLOSED_GOP)
655  xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
656 
657  /* Determines which codec mode we are operating in */
658  avctx->extradata = NULL;
659  avctx->extradata_size = 0;
660  if (xvid_flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
661  /* In this case, we are claiming to be MPEG-4 */
662  x->quicktime_format = 1;
663  } else {
664  /* We are claiming to be Xvid */
665  x->quicktime_format = 0;
666  if (!avctx->codec_tag)
667  avctx->codec_tag = AV_RL32("xvid");
668  }
669 
670  /* Bframes */
671  xvid_enc_create.max_bframes = avctx->max_b_frames;
672  xvid_enc_create.bquant_offset = 100 * avctx->b_quant_offset;
673  xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor;
674  if (avctx->max_b_frames > 0 && !x->quicktime_format)
675  xvid_enc_create.global |= XVID_GLOBAL_PACKED;
676 
677  av_assert0(xvid_enc_create.num_plugins + (!!x->ssim) + (!!x->variance_aq) + (!!x->lumi_aq) <= FF_ARRAY_ELEMS(plugins));
678 
679  /* Encode a dummy frame to get the extradata immediately */
680  if (x->quicktime_format) {
681  AVFrame *picture;
682  AVPacket *packet;
683  int size, got_packet;
684 
685  packet = av_packet_alloc();
686  if (!packet)
687  return AVERROR(ENOMEM);
688 
689  picture = av_frame_alloc();
690  if (!picture) {
691  av_packet_free(&packet);
692  return AVERROR(ENOMEM);
693  }
694 
695  xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
696  if( xerr ) {
697  av_packet_free(&packet);
698  av_frame_free(&picture);
699  av_log(avctx, AV_LOG_ERROR, "Xvid: Could not create encoder reference\n");
700  return AVERROR_EXTERNAL;
701  }
702  x->encoder_handle = xvid_enc_create.handle;
703  size = ((avctx->width + 1) & ~1) * ((avctx->height + 1) & ~1);
704  picture->data[0] = av_malloc(size + size / 2);
705  if (!picture->data[0]) {
706  av_packet_free(&packet);
707  av_frame_free(&picture);
708  return AVERROR(ENOMEM);
709  }
710  picture->data[1] = picture->data[0] + size;
711  picture->data[2] = picture->data[1] + size / 4;
712  memset(picture->data[0], 0, size);
713  memset(picture->data[1], 128, size / 2);
714  xvid_encode_frame(avctx, packet, picture, &got_packet);
715  av_packet_free(&packet);
716  av_free(picture->data[0]);
717  av_frame_free(&picture);
718  xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
719  }
720 
721  /* Create encoder context */
722  xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
723  if (xerr) {
724  av_log(avctx, AV_LOG_ERROR, "Xvid: Could not create encoder reference\n");
725  return AVERROR_EXTERNAL;
726  }
727 
728  x->encoder_handle = xvid_enc_create.handle;
729 
730  return 0;
731 }
732 
734  const AVFrame *picture, int *got_packet)
735 {
736  int xerr, i, ret;
737  struct xvid_context *x = avctx->priv_data;
738  int mb_width = (avctx->width + 15) / 16;
739  int mb_height = (avctx->height + 15) / 16;
740  char *tmp;
741 
742  xvid_enc_frame_t xvid_enc_frame = { 0 };
743  xvid_enc_stats_t xvid_enc_stats = { 0 };
744 
745  if ((ret = ff_alloc_packet(avctx, pkt, mb_width*(int64_t)mb_height*MAX_MB_BYTES + FF_INPUT_BUFFER_MIN_SIZE)) < 0)
746  return ret;
747 
748  /* Start setting up the frame */
749  xvid_enc_frame.version = XVID_VERSION;
750  xvid_enc_stats.version = XVID_VERSION;
751 
752  /* Let Xvid know where to put the frame. */
753  xvid_enc_frame.bitstream = pkt->data;
754  xvid_enc_frame.length = pkt->size;
755 
756  /* Initialize input image fields */
757  if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {
758  av_log(avctx, AV_LOG_ERROR,
759  "Xvid: Color spaces other than 420P not supported\n");
760  return AVERROR(EINVAL);
761  }
762 
763  xvid_enc_frame.input.csp = XVID_CSP_PLANAR; /* YUV420P */
764 
765  for (i = 0; i < 4; i++) {
766  xvid_enc_frame.input.plane[i] = picture->data[i];
767  xvid_enc_frame.input.stride[i] = picture->linesize[i];
768  }
769 
770  /* Encoder Flags */
771  xvid_enc_frame.vop_flags = x->vop_flags;
772  xvid_enc_frame.vol_flags = x->vol_flags;
773  xvid_enc_frame.motion = x->me_flags;
774  xvid_enc_frame.type =
775  picture->pict_type == AV_PICTURE_TYPE_I ? XVID_TYPE_IVOP :
776  picture->pict_type == AV_PICTURE_TYPE_P ? XVID_TYPE_PVOP :
777  picture->pict_type == AV_PICTURE_TYPE_B ? XVID_TYPE_BVOP :
778  XVID_TYPE_AUTO;
779 
780  /* Pixel aspect ratio setting */
781  if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.num > 255 ||
782  avctx->sample_aspect_ratio.den < 0 || avctx->sample_aspect_ratio.den > 255) {
783  av_log(avctx, AV_LOG_WARNING,
784  "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",
787  avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den, 255);
788  }
789  xvid_enc_frame.par = XVID_PAR_EXT;
790  xvid_enc_frame.par_width = avctx->sample_aspect_ratio.num;
791  xvid_enc_frame.par_height = avctx->sample_aspect_ratio.den;
792 
793  /* Quant Setting */
794  if (x->qscale)
795  xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA;
796  else
797  xvid_enc_frame.quant = 0;
798 
799  /* Matrices */
800  xvid_enc_frame.quant_intra_matrix = x->intra_matrix;
801  xvid_enc_frame.quant_inter_matrix = x->inter_matrix;
802 
803  /* Encode */
804  xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE,
805  &xvid_enc_frame, &xvid_enc_stats);
806 
807  /* Two-pass log buffer swapping */
808  avctx->stats_out = NULL;
809  if (x->twopassbuffer) {
810  tmp = x->old_twopassbuffer;
812  x->twopassbuffer = tmp;
813  x->twopassbuffer[0] = 0;
814  if (x->old_twopassbuffer[0] != 0) {
815  avctx->stats_out = x->old_twopassbuffer;
816  }
817  }
818 
819  if (xerr > 0) {
820  enum AVPictureType pict_type;
821 
822  *got_packet = 1;
823 
824  if (xvid_enc_stats.type == XVID_TYPE_PVOP)
825  pict_type = AV_PICTURE_TYPE_P;
826  else if (xvid_enc_stats.type == XVID_TYPE_BVOP)
827  pict_type = AV_PICTURE_TYPE_B;
828  else if (xvid_enc_stats.type == XVID_TYPE_SVOP)
829  pict_type = AV_PICTURE_TYPE_S;
830  else
831  pict_type = AV_PICTURE_TYPE_I;
832 
833  ff_encode_add_stats_side_data(pkt, xvid_enc_stats.quant * FF_QP2LAMBDA, NULL, 0, pict_type);
834 
835  if (xvid_enc_frame.out_flags & XVID_KEYFRAME) {
837  if (x->quicktime_format)
838  return xvid_strip_vol_header(avctx, pkt,
839  xvid_enc_stats.hlength, xerr);
840  }
841 
842  pkt->size = xerr;
843 
844  return 0;
845  } else {
846  if (!xerr)
847  return 0;
848  av_log(avctx, AV_LOG_ERROR,
849  "Xvid: Encoding Error Occurred: %i\n", xerr);
850  return AVERROR_EXTERNAL;
851  }
852 }
853 
855 {
856  struct xvid_context *x = avctx->priv_data;
857 
858  if (x->encoder_handle) {
859  xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
860  x->encoder_handle = NULL;
861  }
862 
863  if (x->twopassbuffer) {
864  av_freep(&x->twopassbuffer);
866  avctx->stats_out = NULL;
867  }
868  if (x->twopassfd>=0) {
869  unlink(x->twopassfile);
870  close(x->twopassfd);
871  x->twopassfd = -1;
872  }
873  av_freep(&x->twopassfile);
874  av_freep(&x->intra_matrix);
875  av_freep(&x->inter_matrix);
876 
877  return 0;
878 }
879 
880 #define OFFSET(x) offsetof(struct xvid_context, x)
881 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
882 static const AVOption options[] = {
883  { "lumi_aq", "Luminance masking AQ", OFFSET(lumi_aq), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
884  { "variance_aq", "Variance AQ", OFFSET(variance_aq), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
885  { "ssim", "Show SSIM information to stdout", OFFSET(ssim), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, VE, .unit = "ssim" },
886  { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, .unit = "ssim" },
887  { "avg", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, .unit = "ssim" },
888  { "frame", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 2 }, INT_MIN, INT_MAX, VE, .unit = "ssim" },
889  { "ssim_acc", "SSIM accuracy", OFFSET(ssim_acc), AV_OPT_TYPE_INT, { .i64 = 2 }, 0, 4, VE },
890  { "gmc", "use GMC", OFFSET(gmc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
891  { "me_quality", "Motion estimation quality", OFFSET(me_quality), AV_OPT_TYPE_INT, { .i64 = 4 }, 0, 6, VE },
892  { "mpeg_quant", "Use MPEG quantizers instead of H.263", OFFSET(mpeg_quant), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
893  { NULL },
894 };
895 
896 static const AVClass xvid_class = {
897  .class_name = "libxvid",
898  .item_name = av_default_item_name,
899  .option = options,
900  .version = LIBAVUTIL_VERSION_INT,
901 };
902 
904  .p.name = "libxvid",
905  CODEC_LONG_NAME("libxvidcore MPEG-4 part 2"),
906  .p.type = AVMEDIA_TYPE_VIDEO,
907  .p.id = AV_CODEC_ID_MPEG4,
909  .priv_data_size = sizeof(struct xvid_context),
912  .close = xvid_encode_close,
914  .color_ranges = AVCOL_RANGE_MPEG,
915  .p.priv_class = &xvid_class,
916  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
917  .p.wrapper_name = "libxvid",
918 };
FF_MATRIX_TYPE_INTRA
#define FF_MATRIX_TYPE_INTRA
Check if the elements of codec context matrices (intra_matrix, inter_matrix or chroma_intra_matrix) a...
Definition: encode.h:106
CODEC_PIXFMTS
#define CODEC_PIXFMTS(...)
Definition: codec_internal.h:392
xvid_context::encoder_handle
void * encoder_handle
Handle for Xvid encoder.
Definition: libxvid.c:66
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
xvid_context::old_twopassbuffer
char * old_twopassbuffer
Old character buffer (two-pass)
Definition: libxvid.c:75
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
xvid_context::xsize
int xsize
Frame x size.
Definition: libxvid.c:67
AVPictureType
AVPictureType
Definition: avutil.h:276
AV_CODEC_FLAG_QSCALE
#define AV_CODEC_FLAG_QSCALE
Use fixed qscale.
Definition: avcodec.h:213
int64_t
long long int64_t
Definition: coverity.c:34
AVCodecContext::intra_matrix
uint16_t * intra_matrix
custom intra quantization matrix Must be allocated with the av_malloc() family of functions,...
Definition: avcodec.h:956
xvid_context::qscale
int qscale
Do we use constant scale?
Definition: libxvid.c:72
normalize.log
log
Definition: normalize.py:21
AVCodecContext::lumi_masking
float lumi_masking
luminance masking (0-> disabled)
Definition: avcodec.h:816
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
xvid_encode_frame
static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *picture, int *got_packet)
Definition: libxvid.c:733
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:427
AVPacket::data
uint8_t * data
Definition: packet.h:588
AVOption
AVOption.
Definition: opt.h:429
encode.h
AVCodecContext::b_quant_offset
float b_quant_offset
qscale offset between IP and B-frames
Definition: avcodec.h:793
FFCodec
Definition: codec_internal.h:127
BUFFER_REMAINING
#define BUFFER_REMAINING(x)
Definition: libxvid.c:57
xvid_strip_vol_header
static int xvid_strip_vol_header(AVCodecContext *avctx, AVPacket *pkt, unsigned int header_len, unsigned int frame_len)
Routine to create a global VO/VOL header for MP4 container.
Definition: libxvid.c:280
mathematics.h
xvid_context
Structure for the private Xvid context.
Definition: libxvid.c:64
AVCodecContext::mb_decision
int mb_decision
macroblock decision mode
Definition: avcodec.h:944
AVCodecContext::qmax
int qmax
maximum quantizer
Definition: avcodec.h:1249
mpegutils.h
AV_CODEC_FLAG_4MV
#define AV_CODEC_FLAG_4MV
4 MV per MB allowed / advanced prediction for H.263.
Definition: avcodec.h:217
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:643
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: packet.c:74
FF_INPUT_BUFFER_MIN_SIZE
#define FF_INPUT_BUFFER_MIN_SIZE
Used by some encoders as upper bound for the length of headers.
Definition: encode.h:33
roundf
static av_always_inline av_const float roundf(float x)
Definition: libm.h:453
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:448
AV_CODEC_FLAG_GLOBAL_HEADER
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:318
av_gcd
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
Definition: mathematics.c:37
close
static av_cold void close(AVCodecParserContext *s)
Definition: apv_parser.c:136
xvid_ff_pass1::context
struct xvid_context * context
Pointer to private context.
Definition: libxvid.c:94
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1569
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:496
xvid_context::intra_matrix
unsigned char * intra_matrix
P-Frame Quant Matrix.
Definition: libxvid.c:78
ff_encode_add_stats_side_data
int ff_encode_add_stats_side_data(AVPacket *pkt, int quality, const int64_t error[], int error_count, enum AVPictureType pict_type)
Definition: encode.c:919
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:359
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
AVRational::num
int num
Numerator.
Definition: rational.h:59
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:52
avassert.h
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:106
float
float
Definition: af_crystalizer.c:122
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:523
MAX_MB_BYTES
#define MAX_MB_BYTES
Definition: mpegutils.h:35
intreadwrite.h
xvid_context::twopassfile
char * twopassfile
second pass temp file name
Definition: libxvid.c:76
AVCodecContext::stats_in
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:1328
xvid_context::ssim_acc
int ssim_acc
SSIM accuracy.
Definition: libxvid.c:83
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:144
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:41
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:231
OFFSET
#define OFFSET(x)
Definition: libxvid.c:880
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
file_open.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:332
tmp
static uint8_t tmp[40]
Definition: aes_ctr.c:52
if
if(ret)
Definition: filter_design.txt:179
xvid_context::quicktime_format
int quicktime_format
Are we in a QT-based format?
Definition: libxvid.c:73
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
xvid_context::me_flags
int me_flags
Motion Estimation flags.
Definition: libxvid.c:71
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:76
NULL
#define NULL
Definition: coverity.c:32
xvid_context::me_quality
int me_quality
Motion estimation quality.
Definition: libxvid.c:85
xvid_context::vop_flags
int vop_flags
VOP flags for Xvid encoder.
Definition: libxvid.c:69
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:489
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:241
BUFFER_CAT
#define BUFFER_CAT(x)
Definition: libxvid.c:58
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:278
options
Definition: swscale.c:43
AV_CODEC_FLAG_AC_PRED
#define AV_CODEC_FLAG_AC_PRED
H.263 advanced intra coding / MPEG-4 AC prediction.
Definition: avcodec.h:327
AVCodecContext::trellis
int trellis
trellis RD quantization
Definition: avcodec.h:1313
xvid_ff_2pass
static int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2)
Dispatch function for our custom plugin.
Definition: libxvid.c:248
xvid_context::twopassfd
int twopassfd
Definition: libxvid.c:77
options
static const AVOption options[]
Definition: libxvid.c:882
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:543
AVCodecContext::stats_out
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:1320
AV_CODEC_FLAG_QPEL
#define AV_CODEC_FLAG_QPEL
Use qpel MC.
Definition: avcodec.h:225
avpriv_tempfile
int avpriv_tempfile(const char *prefix, char **filename, int log_offset, void *log_ctx)
Wrapper to work around the lack of mkstemp() on mingw.
Definition: file_open.c:111
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:519
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:550
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
xvid_class
static const AVClass xvid_class
Definition: libxvid.c:896
AV_CODEC_FLAG_GRAY
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:302
AVPacket::size
int size
Definition: packet.h:589
AVCodecContext::gop_size
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1013
codec_internal.h
AVFrame::quality
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:549
xvid_ff_pass1::version
int version
Xvid version.
Definition: libxvid.c:93
size
int size
Definition: twinvq_data.h:10344
xvid_encode_close
static int xvid_encode_close(AVCodecContext *avctx)
Definition: libxvid.c:854
AV_CODEC_FLAG_PASS2
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:294
xvid_context::variance_aq
int variance_aq
Variance adaptive quantization.
Definition: libxvid.c:81
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:594
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: packet.c:63
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:221
AVCodecContext::b_quant_factor
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
Definition: avcodec.h:786
BUFFER_SIZE
#define BUFFER_SIZE
Buffer management macros.
Definition: libxvid.c:56
xvid_context::vol_flags
int vol_flags
VOL flags for Xvid encoder.
Definition: libxvid.c:70
xvid_ff_2pass_before
static int xvid_ff_2pass_before(struct xvid_context *ref, xvid_plg_data_t *param)
Enable fast encode mode during the first pass.
Definition: libxvid.c:167
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
frame_type
frame_type
Definition: jpeg2000_parser.c:32
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:522
xvid_context::inter_matrix
unsigned char * inter_matrix
I-Frame Quant Matrix.
Definition: libxvid.c:79
internal.h
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
AVCodecContext::height
int height
Definition: avcodec.h:600
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:639
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:760
avcodec.h
xvid_ff_2pass_destroy
static int xvid_ff_2pass_destroy(struct xvid_context *ref, xvid_plg_destroy_t *param)
Destroy the two-pass plugin context.
Definition: libxvid.c:150
AV_CODEC_FLAG_CLOSED_GOP
#define AV_CODEC_FLAG_CLOSED_GOP
Definition: avcodec.h:332
ret
ret
Definition: filter_design.txt:187
VE
#define VE
Definition: libxvid.c:881
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:81
av_malloc
void * av_malloc(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:98
xvid_encode_init
static av_cold int xvid_encode_init(AVCodecContext *avctx)
Definition: libxvid.c:372
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVCodecContext
main external API structure.
Definition: avcodec.h:439
xvid_context::mpeg_quant
int mpeg_quant
Quantization type.
Definition: libxvid.c:86
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:280
xvid_context::ssim
int ssim
SSIM information display mode.
Definition: libxvid.c:82
AVCodecContext::qmin
int qmin
minimum quantizer
Definition: avcodec.h:1242
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:117
xvid_context::lumi_aq
int lumi_aq
Lumi masking as an aq method.
Definition: libxvid.c:80
xvid_context::twopassbuffer
char * twopassbuffer
Character buffer for two-pass.
Definition: libxvid.c:74
ff_libxvid_encoder
const FFCodec ff_libxvid_encoder
Definition: libxvid.c:903
FF_MB_DECISION_RD
#define FF_MB_DECISION_RD
rate distortion
Definition: avcodec.h:947
xvid_ff_pass1
Structure for the private first-pass plugin.
Definition: libxvid.c:92
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:279
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:200
mem.h
AVCodecContext::max_b_frames
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
Definition: avcodec.h:777
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:464
FF_MB_DECISION_BITS
#define FF_MB_DECISION_BITS
chooses the one which needs the fewest bits
Definition: avcodec.h:946
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:466
AVPacket
This structure stores compressed data.
Definition: packet.h:565
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecContext::inter_matrix
uint16_t * inter_matrix
custom inter quantization matrix Must be allocated with the av_malloc() family of functions,...
Definition: avcodec.h:965
xvid_context::ysize
int ysize
Frame y size.
Definition: libxvid.c:68
xvid_ff_2pass_create
static int xvid_ff_2pass_create(xvid_plg_create_t *param, void **handle)
Initialize the two-pass plugin and context.
Definition: libxvid.c:119
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:600
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:472
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
xvid_ff_2pass_after
static int xvid_ff_2pass_after(struct xvid_context *ref, xvid_plg_data_t *param)
Capture statistic data and write it during first pass.
Definition: libxvid.c:211
xvid_correct_framerate
static void xvid_correct_framerate(AVCodecContext *avctx)
Routine to correct a possibly erroneous framerate being fed to us.
Definition: libxvid.c:322
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:226
xvid_context::gmc
int gmc
Definition: libxvid.c:84
AV_PICTURE_TYPE_S
@ AV_PICTURE_TYPE_S
S(GMC)-VOP MPEG-4.
Definition: avutil.h:281
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
snprintf
#define snprintf
Definition: snprintf.h:34
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:61
AVCodecContext::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition: avcodec.h:624
AV_CODEC_FLAG_PASS1
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:290
ff_check_codec_matrices
int ff_check_codec_matrices(AVCodecContext *avctx, unsigned types, uint16_t min, uint16_t max)
Definition: encode.c:944
FF_MATRIX_TYPE_INTER
#define FF_MATRIX_TYPE_INTER
Definition: encode.h:107
frame_types
static const int8_t frame_types[4]
Definition: rv60dec.c:38