Libav
mpegvideo.c
Go to the documentation of this file.
1 /*
2  * The simplest mpeg encoder (well, it was the simplest!)
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * 4MV & hq & B-frame encoding stuff by Michael Niedermayer <michaelni@gmx.at>
7  *
8  * This file is part of Libav.
9  *
10  * Libav is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * Libav is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with Libav; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/internal.h"
34 #include "libavutil/timer.h"
35 #include "avcodec.h"
36 #include "blockdsp.h"
37 #include "idctdsp.h"
38 #include "internal.h"
39 #include "mathops.h"
40 #include "mpegutils.h"
41 #include "mpegvideo.h"
42 #include "mjpegenc.h"
43 #include "msmpeg4.h"
44 #include "qpeldsp.h"
45 #include "xvmc_internal.h"
46 #include "thread.h"
47 #include <limits.h>
48 
50 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
51  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
52  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
53 };
54 
56 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
57  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
58  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
59  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
60  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
61  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
62  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
63  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
64  8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
65 };
66 
67 static const uint8_t mpeg2_dc_scale_table1[128] = {
68 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
69  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
70  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
71  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
72  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
73  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
74  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
75  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
76  4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
77 };
78 
79 static const uint8_t mpeg2_dc_scale_table2[128] = {
80 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
81  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
82  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
83  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
84  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
85  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
86  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
87  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
88  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
89 };
90 
91 static const uint8_t mpeg2_dc_scale_table3[128] = {
92 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
93  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
94  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
95  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
96  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
97  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
98  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
99  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
100  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
101 };
102 
103 const uint8_t *const ff_mpeg2_dc_scale_table[4] = {
108 };
109 
111  0, 1, 2, 3, 8, 9, 16, 17,
112  10, 11, 4, 5, 6, 7, 15, 14,
113  13, 12, 19, 18, 24, 25, 32, 33,
114  26, 27, 20, 21, 22, 23, 28, 29,
115  30, 31, 34, 35, 40, 41, 48, 49,
116  42, 43, 36, 37, 38, 39, 44, 45,
117  46, 47, 50, 51, 56, 57, 58, 59,
118  52, 53, 54, 55, 60, 61, 62, 63,
119 };
120 
122  0, 8, 16, 24, 1, 9, 2, 10,
123  17, 25, 32, 40, 48, 56, 57, 49,
124  41, 33, 26, 18, 3, 11, 4, 12,
125  19, 27, 34, 42, 50, 58, 35, 43,
126  51, 59, 20, 28, 5, 13, 6, 14,
127  21, 29, 36, 44, 52, 60, 37, 45,
128  53, 61, 22, 30, 7, 15, 23, 31,
129  38, 46, 54, 62, 39, 47, 55, 63,
130 };
131 
133  int16_t *block, int n, int qscale)
134 {
135  int i, level, nCoeffs;
136  const uint16_t *quant_matrix;
137 
138  nCoeffs= s->block_last_index[n];
139 
140  if (n < 4)
141  block[0] = block[0] * s->y_dc_scale;
142  else
143  block[0] = block[0] * s->c_dc_scale;
144  /* XXX: only mpeg1 */
145  quant_matrix = s->intra_matrix;
146  for(i=1;i<=nCoeffs;i++) {
147  int j= s->intra_scantable.permutated[i];
148  level = block[j];
149  if (level) {
150  if (level < 0) {
151  level = -level;
152  level = (int)(level * qscale * quant_matrix[j]) >> 3;
153  level = (level - 1) | 1;
154  level = -level;
155  } else {
156  level = (int)(level * qscale * quant_matrix[j]) >> 3;
157  level = (level - 1) | 1;
158  }
159  block[j] = level;
160  }
161  }
162 }
163 
165  int16_t *block, int n, int qscale)
166 {
167  int i, level, nCoeffs;
168  const uint16_t *quant_matrix;
169 
170  nCoeffs= s->block_last_index[n];
171 
172  quant_matrix = s->inter_matrix;
173  for(i=0; i<=nCoeffs; i++) {
174  int j= s->intra_scantable.permutated[i];
175  level = block[j];
176  if (level) {
177  if (level < 0) {
178  level = -level;
179  level = (((level << 1) + 1) * qscale *
180  ((int) (quant_matrix[j]))) >> 4;
181  level = (level - 1) | 1;
182  level = -level;
183  } else {
184  level = (((level << 1) + 1) * qscale *
185  ((int) (quant_matrix[j]))) >> 4;
186  level = (level - 1) | 1;
187  }
188  block[j] = level;
189  }
190  }
191 }
192 
194  int16_t *block, int n, int qscale)
195 {
196  int i, level, nCoeffs;
197  const uint16_t *quant_matrix;
198 
199  if(s->alternate_scan) nCoeffs= 63;
200  else nCoeffs= s->block_last_index[n];
201 
202  if (n < 4)
203  block[0] = block[0] * s->y_dc_scale;
204  else
205  block[0] = block[0] * s->c_dc_scale;
206  quant_matrix = s->intra_matrix;
207  for(i=1;i<=nCoeffs;i++) {
208  int j= s->intra_scantable.permutated[i];
209  level = block[j];
210  if (level) {
211  if (level < 0) {
212  level = -level;
213  level = (int)(level * qscale * quant_matrix[j]) >> 3;
214  level = -level;
215  } else {
216  level = (int)(level * qscale * quant_matrix[j]) >> 3;
217  }
218  block[j] = level;
219  }
220  }
221 }
222 
224  int16_t *block, int n, int qscale)
225 {
226  int i, level, nCoeffs;
227  const uint16_t *quant_matrix;
228  int sum=-1;
229 
230  if(s->alternate_scan) nCoeffs= 63;
231  else nCoeffs= s->block_last_index[n];
232 
233  if (n < 4)
234  block[0] = block[0] * s->y_dc_scale;
235  else
236  block[0] = block[0] * s->c_dc_scale;
237  quant_matrix = s->intra_matrix;
238  for(i=1;i<=nCoeffs;i++) {
239  int j= s->intra_scantable.permutated[i];
240  level = block[j];
241  if (level) {
242  if (level < 0) {
243  level = -level;
244  level = (int)(level * qscale * quant_matrix[j]) >> 3;
245  level = -level;
246  } else {
247  level = (int)(level * qscale * quant_matrix[j]) >> 3;
248  }
249  block[j] = level;
250  sum+=level;
251  }
252  }
253  block[63]^=sum&1;
254 }
255 
257  int16_t *block, int n, int qscale)
258 {
259  int i, level, nCoeffs;
260  const uint16_t *quant_matrix;
261  int sum=-1;
262 
263  if(s->alternate_scan) nCoeffs= 63;
264  else nCoeffs= s->block_last_index[n];
265 
266  quant_matrix = s->inter_matrix;
267  for(i=0; i<=nCoeffs; i++) {
268  int j= s->intra_scantable.permutated[i];
269  level = block[j];
270  if (level) {
271  if (level < 0) {
272  level = -level;
273  level = (((level << 1) + 1) * qscale *
274  ((int) (quant_matrix[j]))) >> 4;
275  level = -level;
276  } else {
277  level = (((level << 1) + 1) * qscale *
278  ((int) (quant_matrix[j]))) >> 4;
279  }
280  block[j] = level;
281  sum+=level;
282  }
283  }
284  block[63]^=sum&1;
285 }
286 
288  int16_t *block, int n, int qscale)
289 {
290  int i, level, qmul, qadd;
291  int nCoeffs;
292 
293  assert(s->block_last_index[n]>=0);
294 
295  qmul = qscale << 1;
296 
297  if (!s->h263_aic) {
298  if (n < 4)
299  block[0] = block[0] * s->y_dc_scale;
300  else
301  block[0] = block[0] * s->c_dc_scale;
302  qadd = (qscale - 1) | 1;
303  }else{
304  qadd = 0;
305  }
306  if(s->ac_pred)
307  nCoeffs=63;
308  else
309  nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
310 
311  for(i=1; i<=nCoeffs; i++) {
312  level = block[i];
313  if (level) {
314  if (level < 0) {
315  level = level * qmul - qadd;
316  } else {
317  level = level * qmul + qadd;
318  }
319  block[i] = level;
320  }
321  }
322 }
323 
325  int16_t *block, int n, int qscale)
326 {
327  int i, level, qmul, qadd;
328  int nCoeffs;
329 
330  assert(s->block_last_index[n]>=0);
331 
332  qadd = (qscale - 1) | 1;
333  qmul = qscale << 1;
334 
335  nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
336 
337  for(i=0; i<=nCoeffs; i++) {
338  level = block[i];
339  if (level) {
340  if (level < 0) {
341  level = level * qmul - qadd;
342  } else {
343  level = level * qmul + qadd;
344  }
345  block[i] = level;
346  }
347  }
348 }
349 
350 static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
351  int (*mv)[2][4][2],
352  int mb_x, int mb_y, int mb_intra, int mb_skipped)
353 {
354  MpegEncContext *s = opaque;
355 
356  s->mv_dir = mv_dir;
357  s->mv_type = mv_type;
358  s->mb_intra = mb_intra;
359  s->mb_skipped = mb_skipped;
360  s->mb_x = mb_x;
361  s->mb_y = mb_y;
362  memcpy(s->mv, mv, sizeof(*mv));
363 
366 
367  s->bdsp.clear_blocks(s->block[0]);
368 
369  s->dest[0] = s->current_picture.f->data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
370  s->dest[1] = s->current_picture.f->data[1] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
371  s->dest[2] = s->current_picture.f->data[2] + (s->mb_y * (16 >> s->chroma_y_shift) * s->uvlinesize) + s->mb_x * (16 >> s->chroma_x_shift);
372 
373  assert(ref == 0);
374  ff_mpv_decode_mb(s, s->block);
375 }
376 
377 /* init common dct for both encoder and decoder */
379 {
380  ff_blockdsp_init(&s->bdsp, s->avctx);
381  ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
382  ff_me_cmp_init(&s->mecc, s->avctx);
385 
391  if (s->flags & CODEC_FLAG_BITEXACT)
394 
397 
398  if (ARCH_ARM)
400  if (ARCH_PPC)
402  if (ARCH_X86)
404 
405  return 0;
406 }
407 
409 {
410  ff_idctdsp_init(&s->idsp, s->avctx);
411 
412  /* load & permutate scantables
413  * note: only wmv uses different ones
414  */
415  if (s->alternate_scan) {
418  } else {
421  }
424 }
425 
426 static int frame_size_alloc(MpegEncContext *s, int linesize)
427 {
428  int alloc_size = FFALIGN(FFABS(linesize) + 32, 32);
429 
430  // edge emu needs blocksize + filter length - 1
431  // (= 17x17 for halfpel / 21x21 for h264)
432  // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
433  // at uvlinesize. It supports only YUV420 so 24x24 is enough
434  // linesize * interlaced * MBsize
435  FF_ALLOCZ_OR_GOTO(s->avctx, s->edge_emu_buffer, alloc_size * 2 * 24,
436  fail);
437 
438  FF_ALLOCZ_OR_GOTO(s->avctx, s->me.scratchpad, alloc_size * 2 * 16 * 3,
439  fail)
440  s->me.temp = s->me.scratchpad;
441  s->rd_scratchpad = s->me.scratchpad;
442  s->b_scratchpad = s->me.scratchpad;
443  s->obmc_scratchpad = s->me.scratchpad + 16;
444 
445  return 0;
446 fail:
448  return AVERROR(ENOMEM);
449 }
450 
455 {
456  int edges_needed = av_codec_is_encoder(s->avctx->codec);
457  int r, ret;
458 
459  pic->tf.f = pic->f;
460  if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
462  s->codec_id != AV_CODEC_ID_MSS2) {
463  if (edges_needed) {
464  pic->f->width = s->avctx->width + 2 * EDGE_WIDTH;
465  pic->f->height = s->avctx->height + 2 * EDGE_WIDTH;
466  }
467 
468  r = ff_thread_get_buffer(s->avctx, &pic->tf,
469  pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
470  } else {
471  pic->f->width = s->avctx->width;
472  pic->f->height = s->avctx->height;
473  pic->f->format = s->avctx->pix_fmt;
474  r = avcodec_default_get_buffer2(s->avctx, pic->f, 0);
475  }
476 
477  if (r < 0 || !pic->f->buf[0]) {
478  av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
479  r, pic->f->data[0]);
480  return -1;
481  }
482 
483  if (edges_needed) {
484  int i;
485  for (i = 0; pic->f->data[i]; i++) {
486  int offset = (EDGE_WIDTH >> (i ? s->chroma_y_shift : 0)) *
487  pic->f->linesize[i] +
488  (EDGE_WIDTH >> (i ? s->chroma_x_shift : 0));
489  pic->f->data[i] += offset;
490  }
491  pic->f->width = s->avctx->width;
492  pic->f->height = s->avctx->height;
493  }
494 
495  if (s->avctx->hwaccel) {
496  assert(!pic->hwaccel_picture_private);
499  if (!pic->hwaccel_priv_buf) {
500  av_log(s->avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
501  return -1;
502  }
504  }
505  }
506 
507  if (s->linesize && (s->linesize != pic->f->linesize[0] ||
508  s->uvlinesize != pic->f->linesize[1])) {
510  "get_buffer() failed (stride changed)\n");
511  ff_mpeg_unref_picture(s, pic);
512  return -1;
513  }
514 
515  if (pic->f->linesize[1] != pic->f->linesize[2]) {
517  "get_buffer() failed (uv stride mismatch)\n");
518  ff_mpeg_unref_picture(s, pic);
519  return -1;
520  }
521 
522  if (!s->edge_emu_buffer &&
523  (ret = frame_size_alloc(s, pic->f->linesize[0])) < 0) {
525  "get_buffer() failed to allocate context scratch buffers.\n");
526  ff_mpeg_unref_picture(s, pic);
527  return ret;
528  }
529 
530  return 0;
531 }
532 
534 {
535  int i;
536 
543 
544  for (i = 0; i < 2; i++) {
546  av_buffer_unref(&pic->ref_index_buf[i]);
547  }
548 }
549 
551 {
552  const int big_mb_num = s->mb_stride * (s->mb_height + 1) + 1;
553  const int mb_array_size = s->mb_stride * s->mb_height;
554  const int b8_array_size = s->b8_stride * s->mb_height * 2;
555  int i;
556 
557 
558  pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
559  pic->qscale_table_buf = av_buffer_allocz(big_mb_num + s->mb_stride);
560  pic->mb_type_buf = av_buffer_allocz((big_mb_num + s->mb_stride) *
561  sizeof(uint32_t));
562  if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
563  return AVERROR(ENOMEM);
564 
565  if (s->encoding) {
566  pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
567  pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
568  pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
569  if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
570  return AVERROR(ENOMEM);
571  }
572 
573  if (s->out_format == FMT_H263 || s->encoding) {
574  int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
575  int ref_index_size = 4 * mb_array_size;
576 
577  for (i = 0; mv_size && i < 2; i++) {
578  pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
579  pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
580  if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
581  return AVERROR(ENOMEM);
582  }
583  }
584 
585  return 0;
586 }
587 
589 {
590  int ret, i;
591 #define MAKE_WRITABLE(table) \
592 do {\
593  if (pic->table &&\
594  (ret = av_buffer_make_writable(&pic->table)) < 0)\
595  return ret;\
596 } while (0)
597 
598  MAKE_WRITABLE(mb_var_buf);
599  MAKE_WRITABLE(mc_mb_var_buf);
600  MAKE_WRITABLE(mb_mean_buf);
601  MAKE_WRITABLE(mbskip_table_buf);
602  MAKE_WRITABLE(qscale_table_buf);
603  MAKE_WRITABLE(mb_type_buf);
604 
605  for (i = 0; i < 2; i++) {
606  MAKE_WRITABLE(motion_val_buf[i]);
607  MAKE_WRITABLE(ref_index_buf[i]);
608  }
609 
610  return 0;
611 }
612 
617 int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
618 {
619  int i, ret;
620 
621  if (shared) {
622  assert(pic->f->data[0]);
623  pic->shared = 1;
624  } else {
625  assert(!pic->f->buf[0]);
626 
627  if (alloc_frame_buffer(s, pic) < 0)
628  return -1;
629 
630  s->linesize = pic->f->linesize[0];
631  s->uvlinesize = pic->f->linesize[1];
632  }
633 
634  if (!pic->qscale_table_buf)
635  ret = alloc_picture_tables(s, pic);
636  else
637  ret = make_tables_writable(pic);
638  if (ret < 0)
639  goto fail;
640 
641  if (s->encoding) {
642  pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
643  pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
644  pic->mb_mean = pic->mb_mean_buf->data;
645  }
646 
647  pic->mbskip_table = pic->mbskip_table_buf->data;
648  pic->qscale_table = pic->qscale_table_buf->data + 2 * s->mb_stride + 1;
649  pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * s->mb_stride + 1;
650 
651  if (pic->motion_val_buf[0]) {
652  for (i = 0; i < 2; i++) {
653  pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
654  pic->ref_index[i] = pic->ref_index_buf[i]->data;
655  }
656  }
657 
658  return 0;
659 fail:
660  av_log(s->avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
661  ff_mpeg_unref_picture(s, pic);
663  return AVERROR(ENOMEM);
664 }
665 
670 {
671  int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
672 
673  pic->tf.f = pic->f;
674  /* WM Image / Screen codecs allocate internal buffers with different
675  * dimensions / colorspaces; ignore user-defined callbacks for these. */
676  if (s->codec_id != AV_CODEC_ID_WMV3IMAGE &&
679  ff_thread_release_buffer(s->avctx, &pic->tf);
680  else if (pic->f)
681  av_frame_unref(pic->f);
682 
684 
685  if (pic->needs_realloc)
687 
688  memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
689 }
690 
691 static int update_picture_tables(Picture *dst, Picture *src)
692 {
693  int i;
694 
695 #define UPDATE_TABLE(table)\
696 do {\
697  if (src->table &&\
698  (!dst->table || dst->table->buffer != src->table->buffer)) {\
699  av_buffer_unref(&dst->table);\
700  dst->table = av_buffer_ref(src->table);\
701  if (!dst->table) {\
702  ff_free_picture_tables(dst);\
703  return AVERROR(ENOMEM);\
704  }\
705  }\
706 } while (0)
707 
708  UPDATE_TABLE(mb_var_buf);
709  UPDATE_TABLE(mc_mb_var_buf);
710  UPDATE_TABLE(mb_mean_buf);
711  UPDATE_TABLE(mbskip_table_buf);
712  UPDATE_TABLE(qscale_table_buf);
713  UPDATE_TABLE(mb_type_buf);
714  for (i = 0; i < 2; i++) {
715  UPDATE_TABLE(motion_val_buf[i]);
716  UPDATE_TABLE(ref_index_buf[i]);
717  }
718 
719  dst->mb_var = src->mb_var;
720  dst->mc_mb_var = src->mc_mb_var;
721  dst->mb_mean = src->mb_mean;
722  dst->mbskip_table = src->mbskip_table;
723  dst->qscale_table = src->qscale_table;
724  dst->mb_type = src->mb_type;
725  for (i = 0; i < 2; i++) {
726  dst->motion_val[i] = src->motion_val[i];
727  dst->ref_index[i] = src->ref_index[i];
728  }
729 
730  return 0;
731 }
732 
734 {
735  int ret;
736 
737  av_assert0(!dst->f->buf[0]);
738  av_assert0(src->f->buf[0]);
739 
740  src->tf.f = src->f;
741  dst->tf.f = dst->f;
742  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
743  if (ret < 0)
744  goto fail;
745 
746  ret = update_picture_tables(dst, src);
747  if (ret < 0)
748  goto fail;
749 
750  if (src->hwaccel_picture_private) {
752  if (!dst->hwaccel_priv_buf)
753  goto fail;
755  }
756 
757  dst->field_picture = src->field_picture;
758  dst->mb_var_sum = src->mb_var_sum;
759  dst->mc_mb_var_sum = src->mc_mb_var_sum;
760  dst->b_frame_score = src->b_frame_score;
761  dst->needs_realloc = src->needs_realloc;
762  dst->reference = src->reference;
763  dst->shared = src->shared;
764 
765  return 0;
766 fail:
767  ff_mpeg_unref_picture(s, dst);
768  return ret;
769 }
770 
772 {
773  int16_t (*tmp)[64];
774 
775  tmp = s->pblocks[4];
776  s->pblocks[4] = s->pblocks[5];
777  s->pblocks[5] = tmp;
778 }
779 
781 {
782  int y_size = s->b8_stride * (2 * s->mb_height + 1);
783  int c_size = s->mb_stride * (s->mb_height + 1);
784  int yc_size = y_size + 2 * c_size;
785  int i;
786 
787  s->edge_emu_buffer =
788  s->me.scratchpad =
789  s->me.temp =
790  s->rd_scratchpad =
791  s->b_scratchpad =
792  s->obmc_scratchpad = NULL;
793 
794  if (s->encoding) {
795  FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
796  ME_MAP_SIZE * sizeof(uint32_t), fail)
798  ME_MAP_SIZE * sizeof(uint32_t), fail)
799  if (s->avctx->noise_reduction) {
801  2 * 64 * sizeof(int), fail)
802  }
803  }
804  FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
805  s->block = s->blocks[0];
806 
807  for (i = 0; i < 12; i++) {
808  s->pblocks[i] = &s->block[i];
809  }
810  if (s->avctx->codec_tag == AV_RL32("VCR2"))
811  exchange_uv(s);
812 
813  if (s->out_format == FMT_H263) {
814  /* ac values */
816  yc_size * sizeof(int16_t) * 16, fail);
817  s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
818  s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
819  s->ac_val[2] = s->ac_val[1] + c_size;
820  }
821 
822  return 0;
823 fail:
824  return -1; // free() through ff_mpv_common_end()
825 }
826 
828 {
829  if (!s)
830  return;
831 
833  av_freep(&s->me.scratchpad);
834  s->me.temp =
835  s->rd_scratchpad =
836  s->b_scratchpad =
837  s->obmc_scratchpad = NULL;
838 
839  av_freep(&s->dct_error_sum);
840  av_freep(&s->me.map);
841  av_freep(&s->me.score_map);
842  av_freep(&s->blocks);
843  av_freep(&s->ac_val_base);
844  s->block = NULL;
845 }
846 
848 {
849 #define COPY(a) bak->a = src->a
850  COPY(edge_emu_buffer);
851  COPY(me.scratchpad);
852  COPY(me.temp);
853  COPY(rd_scratchpad);
854  COPY(b_scratchpad);
855  COPY(obmc_scratchpad);
856  COPY(me.map);
857  COPY(me.score_map);
858  COPY(blocks);
859  COPY(block);
860  COPY(start_mb_y);
861  COPY(end_mb_y);
862  COPY(me.map_generation);
863  COPY(pb);
864  COPY(dct_error_sum);
865  COPY(dct_count[0]);
866  COPY(dct_count[1]);
867  COPY(ac_val_base);
868  COPY(ac_val[0]);
869  COPY(ac_val[1]);
870  COPY(ac_val[2]);
871 #undef COPY
872 }
873 
875 {
876  MpegEncContext bak;
877  int i, ret;
878  // FIXME copy only needed parts
879  // START_TIMER
880  backup_duplicate_context(&bak, dst);
881  memcpy(dst, src, sizeof(MpegEncContext));
882  backup_duplicate_context(dst, &bak);
883  for (i = 0; i < 12; i++) {
884  dst->pblocks[i] = &dst->block[i];
885  }
886  if (dst->avctx->codec_tag == AV_RL32("VCR2"))
887  exchange_uv(dst);
888  if (!dst->edge_emu_buffer &&
889  (ret = frame_size_alloc(dst, dst->linesize)) < 0) {
890  av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
891  "scratch buffers.\n");
892  return ret;
893  }
894  // STOP_TIMER("update_duplicate_context")
895  // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
896  return 0;
897 }
898 
900  const AVCodecContext *src)
901 {
902  int i, ret;
903  MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
904 
905  if (dst == src || !s1->context_initialized)
906  return 0;
907 
908  // FIXME can parameters change on I-frames?
909  // in that case dst may need a reinit
910  if (!s->context_initialized) {
911  memcpy(s, s1, sizeof(MpegEncContext));
912 
913  s->avctx = dst;
914  s->bitstream_buffer = NULL;
916 
917  ff_mpv_idct_init(s);
919  }
920 
921  if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
922  int err;
923  s->context_reinit = 0;
924  s->height = s1->height;
925  s->width = s1->width;
926  if ((err = ff_mpv_common_frame_size_change(s)) < 0)
927  return err;
928  }
929 
930  s->avctx->coded_height = s1->avctx->coded_height;
931  s->avctx->coded_width = s1->avctx->coded_width;
932  s->avctx->width = s1->avctx->width;
933  s->avctx->height = s1->avctx->height;
934 
935  s->coded_picture_number = s1->coded_picture_number;
936  s->picture_number = s1->picture_number;
937 
938  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
939  ff_mpeg_unref_picture(s, &s->picture[i]);
940  if (s1->picture[i].f->buf[0] &&
941  (ret = ff_mpeg_ref_picture(s, &s->picture[i], &s1->picture[i])) < 0)
942  return ret;
943  }
944 
945 #define UPDATE_PICTURE(pic)\
946 do {\
947  ff_mpeg_unref_picture(s, &s->pic);\
948  if (s1->pic.f->buf[0])\
949  ret = ff_mpeg_ref_picture(s, &s->pic, &s1->pic);\
950  else\
951  ret = update_picture_tables(&s->pic, &s1->pic);\
952  if (ret < 0)\
953  return ret;\
954 } while (0)
955 
956  UPDATE_PICTURE(current_picture);
957  UPDATE_PICTURE(last_picture);
958  UPDATE_PICTURE(next_picture);
959 
960  s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
961  s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
962  s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
963 
964  // Error/bug resilience
965  s->next_p_frame_damaged = s1->next_p_frame_damaged;
966  s->workaround_bugs = s1->workaround_bugs;
967 
968  // MPEG4 timing info
969  memcpy(&s->last_time_base, &s1->last_time_base,
970  (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
971  (char *) &s1->last_time_base);
972 
973  // B-frame info
974  s->max_b_frames = s1->max_b_frames;
975  s->low_delay = s1->low_delay;
976  s->droppable = s1->droppable;
977 
978  // DivX handling (doesn't work)
979  s->divx_packed = s1->divx_packed;
980 
981  if (s1->bitstream_buffer) {
982  if (s1->bitstream_buffer_size +
986  s1->allocated_bitstream_buffer_size);
987  s->bitstream_buffer_size = s1->bitstream_buffer_size;
988  memcpy(s->bitstream_buffer, s1->bitstream_buffer,
989  s1->bitstream_buffer_size);
990  memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
992  }
993 
994  // linesize dependend scratch buffer allocation
995  if (!s->edge_emu_buffer)
996  if (s1->linesize) {
997  if (frame_size_alloc(s, s1->linesize) < 0) {
998  av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
999  "scratch buffers.\n");
1000  return AVERROR(ENOMEM);
1001  }
1002  } else {
1003  av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
1004  "be allocated due to unknown size.\n");
1005  return AVERROR_BUG;
1006  }
1007 
1008  // MPEG2/interlacing info
1009  memcpy(&s->progressive_sequence, &s1->progressive_sequence,
1010  (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
1011 
1012  if (!s1->first_field) {
1013  s->last_pict_type = s1->pict_type;
1014  if (s1->current_picture_ptr)
1015  s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
1016  }
1017 
1018  return 0;
1019 }
1020 
1028 {
1029  s->y_dc_scale_table =
1032  s->progressive_frame = 1;
1033  s->progressive_sequence = 1;
1035 
1036  s->coded_picture_number = 0;
1037  s->picture_number = 0;
1038 
1039  s->f_code = 1;
1040  s->b_code = 1;
1041 
1042  s->slice_context_count = 1;
1043 }
1044 
1051 {
1053 }
1054 
1055 static int init_er(MpegEncContext *s)
1056 {
1057  ERContext *er = &s->er;
1058  int mb_array_size = s->mb_height * s->mb_stride;
1059  int i;
1060 
1061  er->avctx = s->avctx;
1062  er->mecc = &s->mecc;
1063 
1064  er->mb_index2xy = s->mb_index2xy;
1065  er->mb_num = s->mb_num;
1066  er->mb_width = s->mb_width;
1067  er->mb_height = s->mb_height;
1068  er->mb_stride = s->mb_stride;
1069  er->b8_stride = s->b8_stride;
1070 
1072  er->error_status_table = av_mallocz(mb_array_size);
1073  if (!er->er_temp_buffer || !er->error_status_table)
1074  goto fail;
1075 
1076  er->mbskip_table = s->mbskip_table;
1077  er->mbintra_table = s->mbintra_table;
1078 
1079  for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
1080  er->dc_val[i] = s->dc_val[i];
1081 
1083  er->opaque = s;
1084 
1085  return 0;
1086 fail:
1087  av_freep(&er->er_temp_buffer);
1089  return AVERROR(ENOMEM);
1090 }
1091 
1096 {
1097  int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
1098 
1099  s->mb_width = (s->width + 15) / 16;
1100  s->mb_stride = s->mb_width + 1;
1101  s->b8_stride = s->mb_width * 2 + 1;
1102  mb_array_size = s->mb_height * s->mb_stride;
1103  mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
1104 
1105  /* set default edge pos, will be overriden
1106  * in decode_header if needed */
1107  s->h_edge_pos = s->mb_width * 16;
1108  s->v_edge_pos = s->mb_height * 16;
1109 
1110  s->mb_num = s->mb_width * s->mb_height;
1111 
1112  s->block_wrap[0] =
1113  s->block_wrap[1] =
1114  s->block_wrap[2] =
1115  s->block_wrap[3] = s->b8_stride;
1116  s->block_wrap[4] =
1117  s->block_wrap[5] = s->mb_stride;
1118 
1119  y_size = s->b8_stride * (2 * s->mb_height + 1);
1120  c_size = s->mb_stride * (s->mb_height + 1);
1121  yc_size = y_size + 2 * c_size;
1122 
1123  FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int),
1124  fail); // error ressilience code looks cleaner with this
1125  for (y = 0; y < s->mb_height; y++)
1126  for (x = 0; x < s->mb_width; x++)
1127  s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
1128 
1129  s->mb_index2xy[s->mb_height * s->mb_width] =
1130  (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
1131 
1132  if (s->encoding) {
1133  /* Allocate MV tables */
1135  mv_table_size * 2 * sizeof(int16_t), fail);
1137  mv_table_size * 2 * sizeof(int16_t), fail);
1139  mv_table_size * 2 * sizeof(int16_t), fail);
1141  mv_table_size * 2 * sizeof(int16_t), fail);
1143  mv_table_size * 2 * sizeof(int16_t), fail);
1145  mv_table_size * 2 * sizeof(int16_t), fail);
1146  s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
1150  s->mb_stride + 1;
1152  s->mb_stride + 1;
1154 
1155  /* Allocate MB type table */
1156  FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size *
1157  sizeof(uint16_t), fail); // needed for encoding
1158 
1159  FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size *
1160  sizeof(int), fail);
1161 
1163  mb_array_size * sizeof(float), fail);
1165  mb_array_size * sizeof(float), fail);
1166 
1167  }
1168 
1169  if (s->codec_id == AV_CODEC_ID_MPEG4 ||
1171  /* interlaced direct mode decoding tables */
1172  for (i = 0; i < 2; i++) {
1173  int j, k;
1174  for (j = 0; j < 2; j++) {
1175  for (k = 0; k < 2; k++) {
1177  s->b_field_mv_table_base[i][j][k],
1178  mv_table_size * 2 * sizeof(int16_t),
1179  fail);
1180  s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
1181  s->mb_stride + 1;
1182  }
1184  mb_array_size * 2 * sizeof(uint8_t), fail);
1186  mv_table_size * 2 * sizeof(int16_t), fail);
1187  s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j]
1188  + s->mb_stride + 1;
1189  }
1191  mb_array_size * 2 * sizeof(uint8_t), fail);
1192  }
1193  }
1194  if (s->out_format == FMT_H263) {
1195  /* cbp values */
1196  FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size, fail);
1197  s->coded_block = s->coded_block_base + s->b8_stride + 1;
1198 
1199  /* cbp, ac_pred, pred_dir */
1201  mb_array_size * sizeof(uint8_t), fail);
1203  mb_array_size * sizeof(uint8_t), fail);
1204  }
1205 
1206  if (s->h263_pred || s->h263_plus || !s->encoding) {
1207  /* dc values */
1208  // MN: we need these for error resilience of intra-frames
1210  yc_size * sizeof(int16_t), fail);
1211  s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
1212  s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
1213  s->dc_val[2] = s->dc_val[1] + c_size;
1214  for (i = 0; i < yc_size; i++)
1215  s->dc_val_base[i] = 1024;
1216  }
1217 
1218  /* which mb is a intra block */
1219  FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
1220  memset(s->mbintra_table, 1, mb_array_size);
1221 
1222  /* init macroblock skip table */
1223  FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
1224  // Note the + 1 is for a quicker mpeg4 slice_end detection
1225 
1226  return init_er(s);
1227 fail:
1228  return AVERROR(ENOMEM);
1229 }
1230 
1232 {
1233  int i, j, k;
1234 
1235  memset(&s->next_picture, 0, sizeof(s->next_picture));
1236  memset(&s->last_picture, 0, sizeof(s->last_picture));
1237  memset(&s->current_picture, 0, sizeof(s->current_picture));
1238  memset(&s->new_picture, 0, sizeof(s->new_picture));
1239 
1240  memset(s->thread_context, 0, sizeof(s->thread_context));
1241 
1242  s->me.map = NULL;
1243  s->me.score_map = NULL;
1244  s->dct_error_sum = NULL;
1245  s->block = NULL;
1246  s->blocks = NULL;
1247  memset(s->pblocks, 0, sizeof(s->pblocks));
1248  s->ac_val_base = NULL;
1249  s->ac_val[0] =
1250  s->ac_val[1] =
1251  s->ac_val[2] =NULL;
1252  s->edge_emu_buffer = NULL;
1253  s->me.scratchpad = NULL;
1254  s->me.temp =
1255  s->rd_scratchpad =
1256  s->b_scratchpad =
1257  s->obmc_scratchpad = NULL;
1258 
1259  s->parse_context.buffer = NULL;
1260  s->parse_context.buffer_size = 0;
1261  s->bitstream_buffer = NULL;
1263  s->picture = NULL;
1264  s->mb_type = NULL;
1265  s->p_mv_table_base = NULL;
1271  s->p_mv_table = NULL;
1272  s->b_forw_mv_table = NULL;
1273  s->b_back_mv_table = NULL;
1276  s->b_direct_mv_table = NULL;
1277  for (i = 0; i < 2; i++) {
1278  for (j = 0; j < 2; j++) {
1279  for (k = 0; k < 2; k++) {
1280  s->b_field_mv_table_base[i][j][k] = NULL;
1281  s->b_field_mv_table[i][j][k] = NULL;
1282  }
1283  s->b_field_select_table[i][j] = NULL;
1284  s->p_field_mv_table_base[i][j] = NULL;
1285  s->p_field_mv_table[i][j] = NULL;
1286  }
1287  s->p_field_select_table[i] = NULL;
1288  }
1289 
1290  s->dc_val_base = NULL;
1291  s->coded_block_base = NULL;
1292  s->mbintra_table = NULL;
1293  s->cbp_table = NULL;
1294  s->pred_dir_table = NULL;
1295 
1296  s->mbskip_table = NULL;
1297 
1298  s->er.error_status_table = NULL;
1299  s->er.er_temp_buffer = NULL;
1300  s->mb_index2xy = NULL;
1301  s->lambda_table = NULL;
1302 
1303  s->cplx_tab = NULL;
1304  s->bits_tab = NULL;
1305 }
1306 
1312 {
1313  int i;
1314  int nb_slices = (HAVE_THREADS &&
1316  s->avctx->thread_count : 1;
1317 
1318  clear_context(s);
1319 
1320  if (s->encoding && s->avctx->slices)
1321  nb_slices = s->avctx->slices;
1322 
1324  s->mb_height = (s->height + 31) / 32 * 2;
1325  else
1326  s->mb_height = (s->height + 15) / 16;
1327 
1328  if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
1330  "decoding to AV_PIX_FMT_NONE is not supported.\n");
1331  return -1;
1332  }
1333 
1334  if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
1335  int max_slices;
1336  if (s->mb_height)
1337  max_slices = FFMIN(MAX_THREADS, s->mb_height);
1338  else
1339  max_slices = MAX_THREADS;
1340  av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
1341  " reducing to %d\n", nb_slices, max_slices);
1342  nb_slices = max_slices;
1343  }
1344 
1345  if ((s->width || s->height) &&
1346  av_image_check_size(s->width, s->height, 0, s->avctx))
1347  return -1;
1348 
1349  dct_init(s);
1350 
1351  s->flags = s->avctx->flags;
1352  s->flags2 = s->avctx->flags2;
1353 
1354  /* set chroma shifts */
1356  &s->chroma_x_shift,
1357  &s->chroma_y_shift);
1358 
1359  /* convert fourcc to upper case */
1361 
1363 
1365  MAX_PICTURE_COUNT * sizeof(Picture), fail);
1366  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1367  s->picture[i].f = av_frame_alloc();
1368  if (!s->picture[i].f)
1369  goto fail;
1370  }
1371  s->next_picture.f = av_frame_alloc();
1372  if (!s->next_picture.f)
1373  goto fail;
1374  s->last_picture.f = av_frame_alloc();
1375  if (!s->last_picture.f)
1376  goto fail;
1378  if (!s->current_picture.f)
1379  goto fail;
1380  s->new_picture.f = av_frame_alloc();
1381  if (!s->new_picture.f)
1382  goto fail;
1383 
1384  if (s->width && s->height) {
1385  if (init_context_frame(s))
1386  goto fail;
1387 
1388  s->parse_context.state = -1;
1389  }
1390 
1391  s->context_initialized = 1;
1392  s->thread_context[0] = s;
1393 
1394  if (s->width && s->height) {
1395  if (nb_slices > 1) {
1396  for (i = 1; i < nb_slices; i++) {
1397  s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1398  memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1399  }
1400 
1401  for (i = 0; i < nb_slices; i++) {
1402  if (init_duplicate_context(s->thread_context[i]) < 0)
1403  goto fail;
1404  s->thread_context[i]->start_mb_y =
1405  (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1406  s->thread_context[i]->end_mb_y =
1407  (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1408  }
1409  } else {
1410  if (init_duplicate_context(s) < 0)
1411  goto fail;
1412  s->start_mb_y = 0;
1413  s->end_mb_y = s->mb_height;
1414  }
1415  s->slice_context_count = nb_slices;
1416  }
1417 
1418  return 0;
1419  fail:
1420  ff_mpv_common_end(s);
1421  return -1;
1422 }
1423 
1430 {
1431  int i, j, k;
1432 
1433  av_freep(&s->mb_type);
1440  s->p_mv_table = NULL;
1441  s->b_forw_mv_table = NULL;
1442  s->b_back_mv_table = NULL;
1445  s->b_direct_mv_table = NULL;
1446  for (i = 0; i < 2; i++) {
1447  for (j = 0; j < 2; j++) {
1448  for (k = 0; k < 2; k++) {
1449  av_freep(&s->b_field_mv_table_base[i][j][k]);
1450  s->b_field_mv_table[i][j][k] = NULL;
1451  }
1452  av_freep(&s->b_field_select_table[i][j]);
1453  av_freep(&s->p_field_mv_table_base[i][j]);
1454  s->p_field_mv_table[i][j] = NULL;
1455  }
1457  }
1458 
1459  av_freep(&s->dc_val_base);
1461  av_freep(&s->mbintra_table);
1462  av_freep(&s->cbp_table);
1463  av_freep(&s->pred_dir_table);
1464 
1465  av_freep(&s->mbskip_table);
1466 
1468  av_freep(&s->er.er_temp_buffer);
1469  av_freep(&s->mb_index2xy);
1470  av_freep(&s->lambda_table);
1471  av_freep(&s->cplx_tab);
1472  av_freep(&s->bits_tab);
1473 
1474  s->linesize = s->uvlinesize = 0;
1475 
1476  return 0;
1477 }
1478 
1480 {
1481  int i, err = 0;
1482 
1483  if (s->slice_context_count > 1) {
1484  for (i = 0; i < s->slice_context_count; i++) {
1486  }
1487  for (i = 1; i < s->slice_context_count; i++) {
1488  av_freep(&s->thread_context[i]);
1489  }
1490  } else
1492 
1493  if ((err = free_context_frame(s)) < 0)
1494  return err;
1495 
1496  if (s->picture)
1497  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1498  s->picture[i].needs_realloc = 1;
1499  }
1500 
1501  s->last_picture_ptr =
1502  s->next_picture_ptr =
1504 
1505  // init
1507  s->mb_height = (s->height + 31) / 32 * 2;
1508  else
1509  s->mb_height = (s->height + 15) / 16;
1510 
1511  if ((s->width || s->height) &&
1512  av_image_check_size(s->width, s->height, 0, s->avctx))
1513  return AVERROR_INVALIDDATA;
1514 
1515  if ((err = init_context_frame(s)))
1516  goto fail;
1517 
1518  s->thread_context[0] = s;
1519 
1520  if (s->width && s->height) {
1521  int nb_slices = s->slice_context_count;
1522  if (nb_slices > 1) {
1523  for (i = 1; i < nb_slices; i++) {
1524  s->thread_context[i] = av_malloc(sizeof(MpegEncContext));
1525  memcpy(s->thread_context[i], s, sizeof(MpegEncContext));
1526  }
1527 
1528  for (i = 0; i < nb_slices; i++) {
1529  if (init_duplicate_context(s->thread_context[i]) < 0)
1530  goto fail;
1531  s->thread_context[i]->start_mb_y =
1532  (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1533  s->thread_context[i]->end_mb_y =
1534  (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1535  }
1536  } else {
1537  if (init_duplicate_context(s) < 0)
1538  goto fail;
1539  s->start_mb_y = 0;
1540  s->end_mb_y = s->mb_height;
1541  }
1542  s->slice_context_count = nb_slices;
1543  }
1544 
1545  return 0;
1546  fail:
1547  ff_mpv_common_end(s);
1548  return err;
1549 }
1550 
1551 /* init common structure for both encoder and decoder */
1553 {
1554  int i;
1555 
1556  if (s->slice_context_count > 1) {
1557  for (i = 0; i < s->slice_context_count; i++) {
1559  }
1560  for (i = 1; i < s->slice_context_count; i++) {
1561  av_freep(&s->thread_context[i]);
1562  }
1563  s->slice_context_count = 1;
1564  } else free_duplicate_context(s);
1565 
1567  s->parse_context.buffer_size = 0;
1568 
1571 
1572  if (s->picture) {
1573  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1575  ff_mpeg_unref_picture(s, &s->picture[i]);
1576  av_frame_free(&s->picture[i].f);
1577  }
1578  }
1579  av_freep(&s->picture);
1592 
1593  free_context_frame(s);
1594 
1595  s->context_initialized = 0;
1596  s->last_picture_ptr =
1597  s->next_picture_ptr =
1599  s->linesize = s->uvlinesize = 0;
1600 }
1601 
1603  uint8_t static_store[2][2 * MAX_RUN + MAX_LEVEL + 3])
1604 {
1605  int8_t max_level[MAX_RUN + 1], max_run[MAX_LEVEL + 1];
1606  uint8_t index_run[MAX_RUN + 1];
1607  int last, run, level, start, end, i;
1608 
1609  /* If table is static, we can quit if rl->max_level[0] is not NULL */
1610  if (static_store && rl->max_level[0])
1611  return;
1612 
1613  /* compute max_level[], max_run[] and index_run[] */
1614  for (last = 0; last < 2; last++) {
1615  if (last == 0) {
1616  start = 0;
1617  end = rl->last;
1618  } else {
1619  start = rl->last;
1620  end = rl->n;
1621  }
1622 
1623  memset(max_level, 0, MAX_RUN + 1);
1624  memset(max_run, 0, MAX_LEVEL + 1);
1625  memset(index_run, rl->n, MAX_RUN + 1);
1626  for (i = start; i < end; i++) {
1627  run = rl->table_run[i];
1628  level = rl->table_level[i];
1629  if (index_run[run] == rl->n)
1630  index_run[run] = i;
1631  if (level > max_level[run])
1632  max_level[run] = level;
1633  if (run > max_run[level])
1634  max_run[level] = run;
1635  }
1636  if (static_store)
1637  rl->max_level[last] = static_store[last];
1638  else
1639  rl->max_level[last] = av_malloc(MAX_RUN + 1);
1640  memcpy(rl->max_level[last], max_level, MAX_RUN + 1);
1641  if (static_store)
1642  rl->max_run[last] = static_store[last] + MAX_RUN + 1;
1643  else
1644  rl->max_run[last] = av_malloc(MAX_LEVEL + 1);
1645  memcpy(rl->max_run[last], max_run, MAX_LEVEL + 1);
1646  if (static_store)
1647  rl->index_run[last] = static_store[last] + MAX_RUN + MAX_LEVEL + 2;
1648  else
1649  rl->index_run[last] = av_malloc(MAX_RUN + 1);
1650  memcpy(rl->index_run[last], index_run, MAX_RUN + 1);
1651  }
1652 }
1653 
1655 {
1656  int i, q;
1657 
1658  for (q = 0; q < 32; q++) {
1659  int qmul = q * 2;
1660  int qadd = (q - 1) | 1;
1661 
1662  if (q == 0) {
1663  qmul = 1;
1664  qadd = 0;
1665  }
1666  for (i = 0; i < rl->vlc.table_size; i++) {
1667  int code = rl->vlc.table[i][0];
1668  int len = rl->vlc.table[i][1];
1669  int level, run;
1670 
1671  if (len == 0) { // illegal code
1672  run = 66;
1673  level = MAX_LEVEL;
1674  } else if (len < 0) { // more bits needed
1675  run = 0;
1676  level = code;
1677  } else {
1678  if (code == rl->n) { // esc
1679  run = 66;
1680  level = 0;
1681  } else {
1682  run = rl->table_run[code] + 1;
1683  level = rl->table_level[code] * qmul + qadd;
1684  if (code >= rl->last) run += 192;
1685  }
1686  }
1687  rl->rl_vlc[q][i].len = len;
1688  rl->rl_vlc[q][i].level = level;
1689  rl->rl_vlc[q][i].run = run;
1690  }
1691  }
1692 }
1693 
1695 {
1696  int i;
1697 
1698  /* release non reference frames */
1699  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1700  if (!s->picture[i].reference)
1701  ff_mpeg_unref_picture(s, &s->picture[i]);
1702  }
1703 }
1704 
1705 static inline int pic_is_unused(MpegEncContext *s, Picture *pic)
1706 {
1707  if (!pic->f->buf[0])
1708  return 1;
1709  if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
1710  return 1;
1711  return 0;
1712 }
1713 
1714 static int find_unused_picture(MpegEncContext *s, int shared)
1715 {
1716  int i;
1717 
1718  if (shared) {
1719  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1720  if (!s->picture[i].f->buf[0])
1721  return i;
1722  }
1723  } else {
1724  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1725  if (pic_is_unused(s, &s->picture[i]))
1726  return i;
1727  }
1728  }
1729 
1730  return AVERROR_INVALIDDATA;
1731 }
1732 
1734 {
1735  int ret = find_unused_picture(s, shared);
1736 
1737  if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
1738  if (s->picture[ret].needs_realloc) {
1739  s->picture[ret].needs_realloc = 0;
1740  ff_free_picture_tables(&s->picture[ret]);
1741  ff_mpeg_unref_picture(s, &s->picture[ret]);
1742  }
1743  }
1744  return ret;
1745 }
1746 
1752 {
1753  int i, ret;
1754  Picture *pic;
1755  s->mb_skipped = 0;
1756 
1757  /* mark & release old frames */
1758  if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1760  s->last_picture_ptr->f->buf[0]) {
1762  }
1763 
1764  /* release forgotten pictures */
1765  /* if (mpeg124/h263) */
1766  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1767  if (&s->picture[i] != s->last_picture_ptr &&
1768  &s->picture[i] != s->next_picture_ptr &&
1769  s->picture[i].reference && !s->picture[i].needs_realloc) {
1770  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1771  av_log(avctx, AV_LOG_ERROR,
1772  "releasing zombie picture\n");
1773  ff_mpeg_unref_picture(s, &s->picture[i]);
1774  }
1775  }
1776 
1778 
1780 
1781  if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
1782  // we already have a unused image
1783  // (maybe it was set before reading the header)
1784  pic = s->current_picture_ptr;
1785  } else {
1786  i = ff_find_unused_picture(s, 0);
1787  if (i < 0) {
1788  av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1789  return i;
1790  }
1791  pic = &s->picture[i];
1792  }
1793 
1794  pic->reference = 0;
1795  if (!s->droppable) {
1796  if (s->pict_type != AV_PICTURE_TYPE_B)
1797  pic->reference = 3;
1798  }
1799 
1801 
1802  if (ff_alloc_picture(s, pic, 0) < 0)
1803  return -1;
1804 
1805  s->current_picture_ptr = pic;
1806  // FIXME use only the vars from current_pic
1808  if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
1810  if (s->picture_structure != PICT_FRAME)
1813  }
1817 
1819  // if (s->flags && CODEC_FLAG_QSCALE)
1820  // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
1822 
1823  if ((ret = ff_mpeg_ref_picture(s, &s->current_picture,
1824  s->current_picture_ptr)) < 0)
1825  return ret;
1826 
1827  if (s->pict_type != AV_PICTURE_TYPE_B) {
1829  if (!s->droppable)
1831  }
1832  av_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
1834  s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
1835  s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
1837  s->pict_type, s->droppable);
1838 
1839  if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
1840  (s->pict_type != AV_PICTURE_TYPE_I ||
1841  s->picture_structure != PICT_FRAME)) {
1842  int h_chroma_shift, v_chroma_shift;
1844  &h_chroma_shift, &v_chroma_shift);
1845  if (s->pict_type != AV_PICTURE_TYPE_I)
1846  av_log(avctx, AV_LOG_ERROR,
1847  "warning: first frame is no keyframe\n");
1848  else if (s->picture_structure != PICT_FRAME)
1849  av_log(avctx, AV_LOG_INFO,
1850  "allocate dummy last picture for field based first keyframe\n");
1851 
1852  /* Allocate a dummy frame */
1853  i = ff_find_unused_picture(s, 0);
1854  if (i < 0) {
1855  av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1856  return i;
1857  }
1858  s->last_picture_ptr = &s->picture[i];
1859 
1860  s->last_picture_ptr->reference = 3;
1862 
1863  if (ff_alloc_picture(s, s->last_picture_ptr, 0) < 0) {
1864  s->last_picture_ptr = NULL;
1865  return -1;
1866  }
1867 
1868  memset(s->last_picture_ptr->f->data[0], 0,
1869  avctx->height * s->last_picture_ptr->f->linesize[0]);
1870  memset(s->last_picture_ptr->f->data[1], 0x80,
1871  (avctx->height >> v_chroma_shift) *
1872  s->last_picture_ptr->f->linesize[1]);
1873  memset(s->last_picture_ptr->f->data[2], 0x80,
1874  (avctx->height >> v_chroma_shift) *
1875  s->last_picture_ptr->f->linesize[2]);
1876 
1877  ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
1878  ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
1879  }
1880  if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
1881  s->pict_type == AV_PICTURE_TYPE_B) {
1882  /* Allocate a dummy frame */
1883  i = ff_find_unused_picture(s, 0);
1884  if (i < 0) {
1885  av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1886  return i;
1887  }
1888  s->next_picture_ptr = &s->picture[i];
1889 
1890  s->next_picture_ptr->reference = 3;
1892 
1893  if (ff_alloc_picture(s, s->next_picture_ptr, 0) < 0) {
1894  s->next_picture_ptr = NULL;
1895  return -1;
1896  }
1897  ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
1898  ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
1899  }
1900 
1901  if (s->last_picture_ptr) {
1903  if (s->last_picture_ptr->f->buf[0] &&
1904  (ret = ff_mpeg_ref_picture(s, &s->last_picture,
1905  s->last_picture_ptr)) < 0)
1906  return ret;
1907  }
1908  if (s->next_picture_ptr) {
1910  if (s->next_picture_ptr->f->buf[0] &&
1911  (ret = ff_mpeg_ref_picture(s, &s->next_picture,
1912  s->next_picture_ptr)) < 0)
1913  return ret;
1914  }
1915 
1916  if (s->pict_type != AV_PICTURE_TYPE_I &&
1917  !(s->last_picture_ptr && s->last_picture_ptr->f->buf[0])) {
1918  av_log(s, AV_LOG_ERROR,
1919  "Non-reference picture received and no reference available\n");
1920  return AVERROR_INVALIDDATA;
1921  }
1922 
1923  if (s->picture_structure!= PICT_FRAME) {
1924  int i;
1925  for (i = 0; i < 4; i++) {
1927  s->current_picture.f->data[i] +=
1928  s->current_picture.f->linesize[i];
1929  }
1930  s->current_picture.f->linesize[i] *= 2;
1931  s->last_picture.f->linesize[i] *= 2;
1932  s->next_picture.f->linesize[i] *= 2;
1933  }
1934  }
1935 
1936  s->err_recognition = avctx->err_recognition;
1937 
1938  /* set dequantizer, we can't do it during init as
1939  * it might change for mpeg4 and we can't do it in the header
1940  * decode as init is not called for mpeg4 there yet */
1941  if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1944  } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1947  } else {
1950  }
1951 
1952 #if FF_API_XVMC
1954  if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration)
1955  return ff_xvmc_field_start(s, avctx);
1957 #endif /* FF_API_XVMC */
1958 
1959  return 0;
1960 }
1961 
1962 /* called after a frame has been decoded. */
1964 {
1965 #if FF_API_XVMC
1967  /* redraw edges for the frame if decoding didn't complete */
1968  // just to make sure that all data is rendered.
1969  if (CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration) {
1970  ff_xvmc_field_end(s);
1971  } else
1973 #endif /* FF_API_XVMC */
1974 
1975  emms_c();
1976 
1977  if (s->current_picture.reference)
1979 }
1980 
1985 {
1986  AVFrame *pict;
1987  if (s->avctx->hwaccel || !p || !p->mb_type)
1988  return;
1989  pict = p->f;
1990 
1992  int x,y;
1993 
1994  av_log(s->avctx,AV_LOG_DEBUG,"New frame, type: ");
1995  switch (pict->pict_type) {
1996  case AV_PICTURE_TYPE_I:
1997  av_log(s->avctx,AV_LOG_DEBUG,"I\n");
1998  break;
1999  case AV_PICTURE_TYPE_P:
2000  av_log(s->avctx,AV_LOG_DEBUG,"P\n");
2001  break;
2002  case AV_PICTURE_TYPE_B:
2003  av_log(s->avctx,AV_LOG_DEBUG,"B\n");
2004  break;
2005  case AV_PICTURE_TYPE_S:
2006  av_log(s->avctx,AV_LOG_DEBUG,"S\n");
2007  break;
2008  case AV_PICTURE_TYPE_SI:
2009  av_log(s->avctx,AV_LOG_DEBUG,"SI\n");
2010  break;
2011  case AV_PICTURE_TYPE_SP:
2012  av_log(s->avctx,AV_LOG_DEBUG,"SP\n");
2013  break;
2014  }
2015  for (y = 0; y < s->mb_height; y++) {
2016  for (x = 0; x < s->mb_width; x++) {
2017  if (s->avctx->debug & FF_DEBUG_SKIP) {
2018  int count = s->mbskip_table[x + y * s->mb_stride];
2019  if (count > 9)
2020  count = 9;
2021  av_log(s->avctx, AV_LOG_DEBUG, "%1d", count);
2022  }
2023  if (s->avctx->debug & FF_DEBUG_QP) {
2024  av_log(s->avctx, AV_LOG_DEBUG, "%2d",
2025  p->qscale_table[x + y * s->mb_stride]);
2026  }
2027  if (s->avctx->debug & FF_DEBUG_MB_TYPE) {
2028  int mb_type = p->mb_type[x + y * s->mb_stride];
2029  // Type & MV direction
2030  if (IS_PCM(mb_type))
2031  av_log(s->avctx, AV_LOG_DEBUG, "P");
2032  else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
2033  av_log(s->avctx, AV_LOG_DEBUG, "A");
2034  else if (IS_INTRA4x4(mb_type))
2035  av_log(s->avctx, AV_LOG_DEBUG, "i");
2036  else if (IS_INTRA16x16(mb_type))
2037  av_log(s->avctx, AV_LOG_DEBUG, "I");
2038  else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
2039  av_log(s->avctx, AV_LOG_DEBUG, "d");
2040  else if (IS_DIRECT(mb_type))
2041  av_log(s->avctx, AV_LOG_DEBUG, "D");
2042  else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
2043  av_log(s->avctx, AV_LOG_DEBUG, "g");
2044  else if (IS_GMC(mb_type))
2045  av_log(s->avctx, AV_LOG_DEBUG, "G");
2046  else if (IS_SKIP(mb_type))
2047  av_log(s->avctx, AV_LOG_DEBUG, "S");
2048  else if (!USES_LIST(mb_type, 1))
2049  av_log(s->avctx, AV_LOG_DEBUG, ">");
2050  else if (!USES_LIST(mb_type, 0))
2051  av_log(s->avctx, AV_LOG_DEBUG, "<");
2052  else {
2053  assert(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
2054  av_log(s->avctx, AV_LOG_DEBUG, "X");
2055  }
2056 
2057  // segmentation
2058  if (IS_8X8(mb_type))
2059  av_log(s->avctx, AV_LOG_DEBUG, "+");
2060  else if (IS_16X8(mb_type))
2061  av_log(s->avctx, AV_LOG_DEBUG, "-");
2062  else if (IS_8X16(mb_type))
2063  av_log(s->avctx, AV_LOG_DEBUG, "|");
2064  else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
2065  av_log(s->avctx, AV_LOG_DEBUG, " ");
2066  else
2067  av_log(s->avctx, AV_LOG_DEBUG, "?");
2068 
2069 
2070  if (IS_INTERLACED(mb_type))
2071  av_log(s->avctx, AV_LOG_DEBUG, "=");
2072  else
2073  av_log(s->avctx, AV_LOG_DEBUG, " ");
2074  }
2075  }
2076  av_log(s->avctx, AV_LOG_DEBUG, "\n");
2077  }
2078  }
2079 }
2080 
2085 {
2086  int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
2087  int my, off, i, mvs;
2088 
2089  if (s->picture_structure != PICT_FRAME || s->mcsel)
2090  goto unhandled;
2091 
2092  switch (s->mv_type) {
2093  case MV_TYPE_16X16:
2094  mvs = 1;
2095  break;
2096  case MV_TYPE_16X8:
2097  mvs = 2;
2098  break;
2099  case MV_TYPE_8X8:
2100  mvs = 4;
2101  break;
2102  default:
2103  goto unhandled;
2104  }
2105 
2106  for (i = 0; i < mvs; i++) {
2107  my = s->mv[dir][i][1]<<qpel_shift;
2108  my_max = FFMAX(my_max, my);
2109  my_min = FFMIN(my_min, my);
2110  }
2111 
2112  off = (FFMAX(-my_min, my_max) + 63) >> 6;
2113 
2114  return FFMIN(FFMAX(s->mb_y + off, 0), s->mb_height-1);
2115 unhandled:
2116  return s->mb_height-1;
2117 }
2118 
2119 /* put block[] to dest[] */
2120 static inline void put_dct(MpegEncContext *s,
2121  int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2122 {
2123  s->dct_unquantize_intra(s, block, i, qscale);
2124  s->idsp.idct_put(dest, line_size, block);
2125 }
2126 
2127 /* add block[] to dest[] */
2128 static inline void add_dct(MpegEncContext *s,
2129  int16_t *block, int i, uint8_t *dest, int line_size)
2130 {
2131  if (s->block_last_index[i] >= 0) {
2132  s->idsp.idct_add(dest, line_size, block);
2133  }
2134 }
2135 
2136 static inline void add_dequant_dct(MpegEncContext *s,
2137  int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2138 {
2139  if (s->block_last_index[i] >= 0) {
2140  s->dct_unquantize_inter(s, block, i, qscale);
2141 
2142  s->idsp.idct_add(dest, line_size, block);
2143  }
2144 }
2145 
2150 {
2151  int wrap = s->b8_stride;
2152  int xy = s->block_index[0];
2153 
2154  s->dc_val[0][xy ] =
2155  s->dc_val[0][xy + 1 ] =
2156  s->dc_val[0][xy + wrap] =
2157  s->dc_val[0][xy + 1 + wrap] = 1024;
2158  /* ac pred */
2159  memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
2160  memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
2161  if (s->msmpeg4_version>=3) {
2162  s->coded_block[xy ] =
2163  s->coded_block[xy + 1 ] =
2164  s->coded_block[xy + wrap] =
2165  s->coded_block[xy + 1 + wrap] = 0;
2166  }
2167  /* chroma */
2168  wrap = s->mb_stride;
2169  xy = s->mb_x + s->mb_y * wrap;
2170  s->dc_val[1][xy] =
2171  s->dc_val[2][xy] = 1024;
2172  /* ac pred */
2173  memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
2174  memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
2175 
2176  s->mbintra_table[xy]= 0;
2177 }
2178 
2179 /* generic function called after a macroblock has been parsed by the
2180  decoder or after it has been encoded by the encoder.
2181 
2182  Important variables used:
2183  s->mb_intra : true if intra macroblock
2184  s->mv_dir : motion vector direction
2185  s->mv_type : motion vector type
2186  s->mv : motion vector
2187  s->interlaced_dct : true if interlaced dct used (mpeg2)
2188  */
2189 static av_always_inline
2191  int is_mpeg12)
2192 {
2193  const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
2194 
2195 #if FF_API_XVMC
2197  if(CONFIG_MPEG_XVMC_DECODER && s->avctx->xvmc_acceleration){
2198  ff_xvmc_decode_mb(s);//xvmc uses pblocks
2199  return;
2200  }
2202 #endif /* FF_API_XVMC */
2203 
2204  if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
2205  /* print DCT coefficients */
2206  int i,j;
2207  av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
2208  for(i=0; i<6; i++){
2209  for(j=0; j<64; j++){
2210  av_log(s->avctx, AV_LOG_DEBUG, "%5d",
2211  block[i][s->idsp.idct_permutation[j]]);
2212  }
2213  av_log(s->avctx, AV_LOG_DEBUG, "\n");
2214  }
2215  }
2216 
2217  s->current_picture.qscale_table[mb_xy] = s->qscale;
2218 
2219  /* update DC predictors for P macroblocks */
2220  if (!s->mb_intra) {
2221  if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
2222  if(s->mbintra_table[mb_xy])
2224  } else {
2225  s->last_dc[0] =
2226  s->last_dc[1] =
2227  s->last_dc[2] = 128 << s->intra_dc_precision;
2228  }
2229  }
2230  else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
2231  s->mbintra_table[mb_xy]=1;
2232 
2233  if ((s->flags&CODEC_FLAG_PSNR) || !(s->encoding && (s->intra_only || s->pict_type==AV_PICTURE_TYPE_B) && s->avctx->mb_decision != FF_MB_DECISION_RD)) { //FIXME precalc
2234  uint8_t *dest_y, *dest_cb, *dest_cr;
2235  int dct_linesize, dct_offset;
2236  op_pixels_func (*op_pix)[4];
2237  qpel_mc_func (*op_qpix)[16];
2238  const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
2239  const int uvlinesize = s->current_picture.f->linesize[1];
2240  const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band;
2241  const int block_size = 8;
2242 
2243  /* avoid copy if macroblock skipped in last frame too */
2244  /* skip only during decoding as we might trash the buffers during encoding a bit */
2245  if(!s->encoding){
2246  uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
2247 
2248  if (s->mb_skipped) {
2249  s->mb_skipped= 0;
2250  assert(s->pict_type!=AV_PICTURE_TYPE_I);
2251  *mbskip_ptr = 1;
2252  } else if(!s->current_picture.reference) {
2253  *mbskip_ptr = 1;
2254  } else{
2255  *mbskip_ptr = 0; /* not skipped */
2256  }
2257  }
2258 
2259  dct_linesize = linesize << s->interlaced_dct;
2260  dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
2261 
2262  if(readable){
2263  dest_y= s->dest[0];
2264  dest_cb= s->dest[1];
2265  dest_cr= s->dest[2];
2266  }else{
2267  dest_y = s->b_scratchpad;
2268  dest_cb= s->b_scratchpad+16*linesize;
2269  dest_cr= s->b_scratchpad+32*linesize;
2270  }
2271 
2272  if (!s->mb_intra) {
2273  /* motion handling */
2274  /* decoding or more than one mb_type (MC was already done otherwise) */
2275  if(!s->encoding){
2276 
2278  if (s->mv_dir & MV_DIR_FORWARD) {
2281  0);
2282  }
2283  if (s->mv_dir & MV_DIR_BACKWARD) {
2286  0);
2287  }
2288  }
2289 
2290  op_qpix= s->me.qpel_put;
2291  if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
2292  op_pix = s->hdsp.put_pixels_tab;
2293  }else{
2294  op_pix = s->hdsp.put_no_rnd_pixels_tab;
2295  }
2296  if (s->mv_dir & MV_DIR_FORWARD) {
2297  ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
2298  op_pix = s->hdsp.avg_pixels_tab;
2299  op_qpix= s->me.qpel_avg;
2300  }
2301  if (s->mv_dir & MV_DIR_BACKWARD) {
2302  ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
2303  }
2304  }
2305 
2306  /* skip dequant / idct if we are really late ;) */
2307  if(s->avctx->skip_idct){
2310  || s->avctx->skip_idct >= AVDISCARD_ALL)
2311  goto skip_idct;
2312  }
2313 
2314  /* add dct residue */
2316  || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
2317  add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
2318  add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
2319  add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
2320  add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
2321 
2322  if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2323  if (s->chroma_y_shift){
2324  add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
2325  add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
2326  }else{
2327  dct_linesize >>= 1;
2328  dct_offset >>=1;
2329  add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
2330  add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
2331  add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
2332  add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
2333  }
2334  }
2335  } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
2336  add_dct(s, block[0], 0, dest_y , dct_linesize);
2337  add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
2338  add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
2339  add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
2340 
2341  if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2342  if(s->chroma_y_shift){//Chroma420
2343  add_dct(s, block[4], 4, dest_cb, uvlinesize);
2344  add_dct(s, block[5], 5, dest_cr, uvlinesize);
2345  }else{
2346  //chroma422
2347  dct_linesize = uvlinesize << s->interlaced_dct;
2348  dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
2349 
2350  add_dct(s, block[4], 4, dest_cb, dct_linesize);
2351  add_dct(s, block[5], 5, dest_cr, dct_linesize);
2352  add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
2353  add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
2354  if(!s->chroma_x_shift){//Chroma444
2355  add_dct(s, block[8], 8, dest_cb+8, dct_linesize);
2356  add_dct(s, block[9], 9, dest_cr+8, dct_linesize);
2357  add_dct(s, block[10], 10, dest_cb+8+dct_offset, dct_linesize);
2358  add_dct(s, block[11], 11, dest_cr+8+dct_offset, dct_linesize);
2359  }
2360  }
2361  }//fi gray
2362  }
2364  ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
2365  }
2366  } else {
2367  /* dct only in intra block */
2369  put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
2370  put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
2371  put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
2372  put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
2373 
2374  if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2375  if(s->chroma_y_shift){
2376  put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
2377  put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
2378  }else{
2379  dct_offset >>=1;
2380  dct_linesize >>=1;
2381  put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
2382  put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
2383  put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
2384  put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
2385  }
2386  }
2387  }else{
2388  s->idsp.idct_put(dest_y, dct_linesize, block[0]);
2389  s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
2390  s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
2391  s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
2392 
2393  if(!CONFIG_GRAY || !(s->flags&CODEC_FLAG_GRAY)){
2394  if(s->chroma_y_shift){
2395  s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
2396  s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
2397  }else{
2398 
2399  dct_linesize = uvlinesize << s->interlaced_dct;
2400  dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize * 8;
2401 
2402  s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
2403  s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
2404  s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
2405  s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
2406  if(!s->chroma_x_shift){//Chroma444
2407  s->idsp.idct_put(dest_cb + 8, dct_linesize, block[8]);
2408  s->idsp.idct_put(dest_cr + 8, dct_linesize, block[9]);
2409  s->idsp.idct_put(dest_cb + 8 + dct_offset, dct_linesize, block[10]);
2410  s->idsp.idct_put(dest_cr + 8 + dct_offset, dct_linesize, block[11]);
2411  }
2412  }
2413  }//gray
2414  }
2415  }
2416 skip_idct:
2417  if(!readable){
2418  s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
2419  s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
2420  s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
2421  }
2422  }
2423 }
2424 
2425 void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64])
2426 {
2427 #if !CONFIG_SMALL
2428  if(s->out_format == FMT_MPEG1) {
2429  mpv_decode_mb_internal(s, block, 1);
2430  } else
2431 #endif
2432  mpv_decode_mb_internal(s, block, 0);
2433 }
2434 
2436 {
2438  s->last_picture.f, y, h, s->picture_structure,
2439  s->first_field, s->low_delay);
2440 }
2441 
2442 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
2443  const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
2444  const int uvlinesize = s->current_picture.f->linesize[1];
2445  const int mb_size= 4;
2446 
2447  s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
2448  s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
2449  s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
2450  s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
2451  s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
2452  s->block_index[5]= s->mb_stride*(s->mb_y + s->mb_height + 2) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
2453  //block_index is not used by mpeg2, so it is not affected by chroma_format
2454 
2455  s->dest[0] = s->current_picture.f->data[0] + (s->mb_x - 1) * (1 << mb_size);
2456  s->dest[1] = s->current_picture.f->data[1] + (s->mb_x - 1) * (1 << (mb_size - s->chroma_x_shift));
2457  s->dest[2] = s->current_picture.f->data[2] + (s->mb_x - 1) * (1 << (mb_size - s->chroma_x_shift));
2458 
2460  {
2461  if(s->picture_structure==PICT_FRAME){
2462  s->dest[0] += s->mb_y * linesize << mb_size;
2463  s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
2464  s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
2465  }else{
2466  s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
2467  s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
2468  s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
2469  assert((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
2470  }
2471  }
2472 }
2473 
2482 void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
2483 {
2484  int i;
2485  int16_t temp[64];
2486 
2487  if(last<=0) return;
2488  //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
2489 
2490  for(i=0; i<=last; i++){
2491  const int j= scantable[i];
2492  temp[j]= block[j];
2493  block[j]=0;
2494  }
2495 
2496  for(i=0; i<=last; i++){
2497  const int j= scantable[i];
2498  const int perm_j= permutation[j];
2499  block[perm_j]= temp[j];
2500  }
2501 }
2502 
2504  int i;
2505  MpegEncContext *s = avctx->priv_data;
2506 
2507  if (!s || !s->picture)
2508  return;
2509 
2510  for (i = 0; i < MAX_PICTURE_COUNT; i++)
2511  ff_mpeg_unref_picture(s, &s->picture[i]);
2513 
2517 
2518  s->mb_x= s->mb_y= 0;
2519 
2520  s->parse_context.state= -1;
2522  s->parse_context.overread= 0;
2524  s->parse_context.index= 0;
2525  s->parse_context.last_index= 0;
2526  s->bitstream_buffer_size=0;
2527  s->pp_time=0;
2528 }
2529 
2533 void ff_set_qscale(MpegEncContext * s, int qscale)
2534 {
2535  if (qscale < 1)
2536  qscale = 1;
2537  else if (qscale > 31)
2538  qscale = 31;
2539 
2540  s->qscale = qscale;
2541  s->chroma_qscale= s->chroma_qscale_table[qscale];
2542 
2543  s->y_dc_scale= s->y_dc_scale_table[ qscale ];
2545 }
2546 
2548 {
2551 }
int last_time_base
Definition: mpegvideo.h:501
int bitstream_buffer_size
Definition: mpegvideo.h:529
uint8_t * scratchpad
data area for the ME algo, so that the ME does not need to malloc/free
Definition: mpegvideo.h:153
av_cold void ff_me_cmp_init(MECmpContext *c, AVCodecContext *avctx)
Definition: me_cmp.c:893
int last
number of values for last = 0
Definition: rl.h:40
int ff_xvmc_field_start(MpegEncContext *s, AVCodecContext *avctx)
IDCTDSPContext idsp
Definition: mpegvideo.h:354
int ff_mpeg_ref_picture(MpegEncContext *s, Picture *dst, Picture *src)
Definition: mpegvideo.c:733
MECmpContext * mecc
static int init_duplicate_context(MpegEncContext *s)
Definition: mpegvideo.c:780
const struct AVCodec * codec
Definition: avcodec.h:1059
int16_t(* b_bidir_back_mv_table_base)[2]
Definition: mpegvideo.h:368
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
av_cold void ff_mpv_common_init_arm(MpegEncContext *s)
Definition: mpegvideo_arm.c:43
int table_size
Definition: get_bits.h:67
discard all frames except keyframes
Definition: avcodec.h:567
int8_t * ref_index[2]
Definition: mpegvideo.h:116
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:2442
unsigned int stream_codec_tag
fourcc from the AVI stream header (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A')...
Definition: avcodec.h:1090
#define ARCH_ARM
Definition: config.h:14
int picture_number
Definition: mpegvideo.h:253
#define MAX_PICTURE_COUNT
Definition: mpegvideo.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
av_cold void ff_mpv_common_init_neon(MpegEncContext *s)
Definition: mpegvideo.c:126
ScanTable intra_v_scantable
Definition: mpegvideo.h:216
AVBufferRef * mb_var_buf
Definition: mpegvideo.h:118
av_cold void ff_mpegvideodsp_init(MpegVideoDSPContext *c)
Definition: mpegvideodsp.c:110
S(GMC)-VOP MPEG4.
Definition: avutil.h:256
void(* dct_unquantize_inter)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:622
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:106
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
int ff_alloc_picture(MpegEncContext *s, Picture *pic, int shared)
Allocate a Picture.
Definition: mpegvideo.c:617
int16_t(* p_mv_table)[2]
MV table (1MV per MB) p-frame encoding.
Definition: mpegvideo.h:372
uint8_t * rd_scratchpad
scratchpad for rate distortion mb decision
Definition: mpegvideo.h:328
int start_mb_y
start mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y) ...
Definition: mpegvideo.h:279
#define MAKE_WRITABLE(table)
const uint8_t * y_dc_scale_table
qscale -> y_dc_scale table
Definition: mpegvideo.h:314
uint8_t * mb_mean
Table for MB luminance.
Definition: mpegvideo.h:125
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1244
op_pixels_func avg_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition: hpeldsp.h:68
#define IS_GMC(a)
Definition: mpegutils.h:81
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
AVFrame * f
Definition: thread.h:36
const uint8_t ff_alternate_horizontal_scan[64]
Definition: mpegvideo.c:110
uint8_t * coded_block_base
Definition: mpegvideo.h:317
static int update_picture_tables(Picture *dst, Picture *src)
Definition: mpegvideo.c:691
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:393
int end_mb_y
end mb_y of this thread (so current thread should process start_mb_y <= row < end_mb_y) ...
Definition: mpegvideo.h:280
uint16_t * mb_var
Table for MB variances.
Definition: mpegvideo.h:119
void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
Permute an 8x8 block.
Definition: mpegvideo.c:2482
int16_t(*[3] ac_val)[16]
used for for mpeg4 AC prediction, all 3 arrays must be continuous
Definition: mpegvideo.h:320
MJPEG encoder.
int v_edge_pos
horizontal / vertical position of the right/bottom edge (pixel replication)
Definition: mpegvideo.h:258
int msmpeg4_version
0=not msmpeg4, 1=mp41, 2=mp42, 3=mp43/divx3 4=wmv1/7 5=wmv2/8
Definition: mpegvideo.h:548
static const uint8_t mpeg2_dc_scale_table3[128]
Definition: mpegvideo.c:91
int needs_realloc
Picture needs to be reallocated (eg due to a frame size change)
Definition: mpegvideo.h:139
uint8_t * bitstream_buffer
Definition: mpegvideo.h:528
enum AVCodecID codec_id
Definition: mpegvideo.h:235
void(* clear_blocks)(int16_t *blocks)
Definition: blockdsp.h:36
#define ARCH_X86
Definition: config.h:33
int field_picture
whether or not the picture was encoded in separate fields
Definition: mpegvideo.h:133
void ff_print_debug_info(MpegEncContext *s, Picture *p)
Print debugging info for the given picture.
Definition: mpegvideo.c:1984
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
int16_t(*[2][2] p_field_mv_table)[2]
MV table (2MV per MB) interlaced p-frame encoding.
Definition: mpegvideo.h:378
int16_t(* p_mv_table_base)[2]
Definition: mpegvideo.h:364
static int make_tables_writable(Picture *pic)
Definition: mpegvideo.c:588
uint8_t raster_end[64]
Definition: idctdsp.h:32
void(* qpel_mc_func)(uint8_t *dst, const uint8_t *src, ptrdiff_t stride)
Definition: qpeldsp.h:65
uint32_t * score_map
map to store the scores
Definition: mpegvideo.h:159
mpegvideo header.
#define FF_ARRAY_ELEMS(a)
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
discard all
Definition: avcodec.h:568
uint8_t permutated[64]
Definition: idctdsp.h:31
const int8_t * table_level
Definition: rl.h:43
uint8_t run
Definition: svq3.c:146
static void free_duplicate_context(MpegEncContext *s)
Definition: mpegvideo.c:827
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2530
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
int mb_num
number of MBs of a picture
Definition: mpegvideo.h:259
void ff_draw_horiz_band(AVCodecContext *avctx, AVFrame *cur, AVFrame *last, int y, int h, int picture_structure, int first_field, int low_delay)
Draw a horizontal band if supported.
Definition: mpegutils.c:30
int frame_start_found
Definition: parser.h:34
int qscale
QP.
Definition: mpegvideo.h:332
RLTable.
Definition: rl.h:38
int h263_aic
Advanded INTRA Coding (AIC)
Definition: mpegvideo.h:210
int16_t(* b_back_mv_table)[2]
MV table (1MV per MB) backward mode b-frame encoding.
Definition: mpegvideo.h:374
int chroma_x_shift
Definition: mpegvideo.h:584
int encoding
true if we are encoding (vs decoding)
Definition: mpegvideo.h:237
void(* dct_unquantize_h263_intra)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:616
void(* dct_unquantize_intra)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:620
int block_wrap[6]
Definition: mpegvideo.h:416
static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:164
Macro definitions for various function/variable attributes.
#define FFALIGN(x, a)
Definition: common.h:62
int16_t(* b_back_mv_table_base)[2]
Definition: mpegvideo.h:366
static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
Definition: mpegvideo.c:847
#define REBASE_PICTURE(pic, new_ctx, old_ctx)
Definition: h264_slice.c:426
void ff_clean_intra_table_entries(MpegEncContext *s)
Clean dc, ac, coded_block for the current non-intra MB.
Definition: mpegvideo.c:2149
void(* dct_unquantize_h263_inter)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:618
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
Switching Intra.
Definition: avutil.h:257
int b_frame_score
Definition: mpegvideo.h:138
#define MAX_THREADS
Definition: mpegvideo.h:66
int av_codec_is_encoder(const AVCodec *codec)
Definition: utils.c:95
#define CODEC_FLAG_PSNR
error[?] variables will be set during encoding.
Definition: avcodec.h:645
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2448
#define USES_LIST(a, list)
Definition: mpegutils.h:95
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void ff_mpeg_draw_horiz_band(MpegEncContext *s, int y, int h)
Definition: mpegvideo.c:2435
int8_t * max_run[2]
encoding & decoding
Definition: rl.h:46
int context_reinit
Definition: mpegvideo.h:636
int16_t * dc_val_base
Definition: mpegvideo.h:312
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
enum OutputFormat out_format
output format
Definition: mpegvideo.h:227
int ff_mpv_common_frame_size_change(MpegEncContext *s)
Definition: mpegvideo.c:1479
void ff_mpv_motion(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int dir, uint8_t **ref_picture, op_pixels_func(*pix_op)[4], qpel_mc_func(*qpix_op)[16])
uint8_t * pred_dir_table
used to store pred_dir for partitioned decoding
Definition: mpegvideo.h:326
Multithreading support functions.
qpel_mc_func(* qpel_put)[16]
Definition: mpegvideo.h:191
#define emms_c()
Definition: internal.h:47
static void release_unused_pictures(MpegEncContext *s)
Definition: mpegvideo.c:1694
int no_rounding
apply no rounding to motion compensation (MPEG4, msmpeg4, ...) for b-frames rounding mode is always 0...
Definition: mpegvideo.h:406
int interlaced_dct
Definition: mpegvideo.h:589
void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64])
Definition: mpegvideo.c:2425
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:306
int intra_dc_precision
Definition: mpegvideo.h:572
static int pic_is_unused(MpegEncContext *s, Picture *pic)
Definition: mpegvideo.c:1705
quarterpel DSP functions
void ff_mpv_common_init_ppc(MpegEncContext *s)
static int alloc_picture_tables(MpegEncContext *s, Picture *pic)
Definition: mpegvideo.c:550
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:34
int16_t(* b_bidir_forw_mv_table)[2]
MV table (1MV per MB) bidir mode b-frame encoding.
Definition: mpegvideo.h:375
float * cplx_tab
Definition: mpegvideo.h:632
int8_t * max_level[2]
encoding & decoding
Definition: rl.h:45
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:2349
#define FF_DEBUG_MB_TYPE
Definition: avcodec.h:2382
uint16_t pp_time
time distance between the last 2 p,s,i frames
Definition: mpegvideo.h:505
AVBufferRef * mb_type_buf
Definition: mpegvideo.h:109
uint8_t * b_scratchpad
scratchpad used for writing into write only buffers
Definition: mpegvideo.h:330
static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type, int(*mv)[2][4][2], int mb_x, int mb_y, int mb_intra, int mb_skipped)
Definition: mpegvideo.c:350
int flags2
AVCodecContext.flags2.
Definition: mpegvideo.h:239
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:320
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:658
av_cold void ff_mpv_idct_init(MpegEncContext *s)
Definition: mpegvideo.c:408
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:255
int codec_tag
internal codec_tag upper case converted from avctx codec_tag
Definition: mpegvideo.h:245
high precision timer, useful to profile code
int16_t(*[2][2] p_field_mv_table_base)[2]
Definition: mpegvideo.h:370
static int free_context_frame(MpegEncContext *s)
Frees and resets MpegEncContext fields depending on the resolution.
Definition: mpegvideo.c:1429
#define MAX_LEVEL
Definition: rl.h:35
static void ff_update_block_index(MpegEncContext *s)
Definition: mpegvideo.h:756
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
Definition: mpegvideo.c:2533
AVBufferRef * mb_mean_buf
Definition: mpegvideo.h:124
#define r
Definition: input.c:51
int intra_only
if true, only intra pictures are generated
Definition: mpegvideo.h:225
ThreadFrame tf
Definition: mpegvideo.h:101
int16_t * dc_val[3]
used for mpeg4 DC prediction, all 3 arrays must be continuous
Definition: mpegvideo.h:313
int h263_plus
h263 plus headers
Definition: mpegvideo.h:232
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:282
unsigned int buffer_size
Definition: parser.h:32
int width
width and height of the video frame
Definition: frame.h:174
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
uint8_t * mbskip_table
Definition: mpegvideo.h:113
int stream_codec_tag
internal stream_codec_tag upper case converted from avctx stream_codec_tag
Definition: mpegvideo.h:246
int last_dc[3]
last DC values for MPEG1
Definition: mpegvideo.h:311
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
static void add_dct(MpegEncContext *s, int16_t *block, int i, uint8_t *dest, int line_size)
Definition: mpegvideo.c:2128
#define CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
Definition: avcodec.h:662
int mb_skipped
MUST BE SET only during DECODING.
Definition: mpegvideo.h:321
int chroma_y_shift
Definition: mpegvideo.h:585
static int find_unused_picture(MpegEncContext *s, int shared)
Definition: mpegvideo.c:1714
int partitioned_frame
is current frame partitioned
Definition: mpegvideo.h:518
av_cold void ff_init_vlc_rl(RLTable *rl)
Definition: mpegvideo.c:1654
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
ERContext er
Definition: mpegvideo.h:638
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2575
int last_lambda_for[5]
last lambda for a specific pict type
Definition: mpegvideo.h:344
int reference
Definition: mpegvideo.h:141
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
uint8_t * edge_emu_buffer
temporary buffer for if MVs point to out-of-frame data
Definition: mpegvideo.h:327
static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:223
void(* dct_unquantize_mpeg2_inter)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:614
void(* decode_mb)(void *opaque, int ref, int mv_dir, int mv_type, int(*mv)[2][4][2], int mb_x, int mb_y, int mb_intra, int mb_skipped)
void(* dct_unquantize_mpeg1_intra)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:608
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1144
static enum AVDiscard skip_idct
Definition: avplay.c:253
#define wrap(func)
Definition: neontest.h:62
static void put_dct(MpegEncContext *s, int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
Definition: mpegvideo.c:2120
simple assert() macros that are a bit more flexible than ISO C assert().
int overread_index
the index into ParseContext.buffer of the overread bytes
Definition: parser.h:36
#define PICT_TOP_FIELD
Definition: mpegutils.h:33
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:132
#define IS_SKIP(a)
Definition: mpegutils.h:77
#define CONFIG_WMV2_DECODER
Definition: config.h:628
int quarter_sample
1->qpel, 0->half pel ME/MC
Definition: mpegvideo.h:514
uint16_t * mb_type
Table for candidate MB types for encoding (defines in mpegutils.h)
Definition: mpegvideo.h:413
int low_delay
no reordering needed / has no b-frames
Definition: mpegvideo.h:519
void ff_mpeg_unref_picture(MpegEncContext *s, Picture *pic)
Deallocate a picture.
Definition: mpegvideo.c:669
VLC vlc
decoding only deprecated FIXME remove
Definition: rl.h:47
uint8_t *[2][2] b_field_select_table
Definition: mpegvideo.h:381
void ff_mpv_common_end(MpegEncContext *s)
Definition: mpegvideo.c:1552
#define FFMAX(a, b)
Definition: common.h:55
int8_t len
Definition: get_bits.h:72
av_cold void ff_mpv_common_init_x86(MpegEncContext *s)
Definition: mpegvideo.c:447
#define HAVE_INTRINSICS_NEON
Definition: config.h:198
uint8_t * mbintra_table
int * mb_index2xy
static const uint8_t ff_default_chroma_qscale_table[32]
Definition: mpegvideo.c:49
void ff_mpeg_flush(AVCodecContext *avctx)
Definition: mpegvideo.c:2503
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition: hpeldsp.c:338
int coded_picture_number
used to set pic->coded_picture_number, should not be used for/by anything else
Definition: mpegvideo.h:252
int * lambda_table
Definition: mpegvideo.h:336
uint8_t * error_status_table
AVBufferRef * hwaccel_priv_buf
Definition: mpegvideo.h:127
common internal API header
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:531
const uint8_t ff_alternate_vertical_scan[64]
Definition: mpegvideo.c:121
int n
number of entries of table_vlc minus 1
Definition: rl.h:39
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:37
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:222
int err_recognition
Definition: mpegvideo.h:477
AVBufferRef * motion_val_buf[2]
Definition: mpegvideo.h:106
void(* op_pixels_func)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)
Definition: hpeldsp.h:38
void(* draw_horiz_band)(struct AVCodecContext *s, const AVFrame *src, int offset[AV_NUM_DATA_POINTERS], int y, int type, int height)
If non NULL, 'draw_horiz_band' is called by the libavcodec decoder to draw a horizontal band...
Definition: avcodec.h:1304
int progressive_frame
Definition: mpegvideo.h:587
#define IS_16X8(a)
Definition: mpegutils.h:83
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
#define UPDATE_PICTURE(pic)
int top_field_first
Definition: mpegvideo.h:574
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2422
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2567
uint8_t * er_temp_buffer
int overread
the number of bytes which where irreversibly read from the next frame
Definition: parser.h:35
#define FFMIN(a, b)
Definition: common.h:57
int last_index
Definition: parser.h:31
#define IS_DIRECT(a)
Definition: mpegutils.h:80
int next_p_frame_damaged
set if the next p frame is damaged, to avoid showing trashed b frames
Definition: mpegvideo.h:476
static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:256
Picture new_picture
copy of the source picture structure for encoding.
Definition: mpegvideo.h:300
int width
picture width / height.
Definition: avcodec.h:1229
uint8_t * mbskip_table
used to avoid copy if macroblock skipped (for black regions for example) and used for b-frame encodin...
Definition: mpegvideo.h:322
int16_t(*[2] motion_val)[2]
Definition: mpegvideo.h:107
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:310
Picture.
Definition: mpegvideo.h:99
#define CONFIG_GRAY
Definition: config.h:359
int alternate_scan
Definition: mpegvideo.h:578
unsigned int allocated_bitstream_buffer_size
Definition: mpegvideo.h:530
void * hwaccel_picture_private
hardware accelerator private data
Definition: mpegvideo.h:131
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
int16_t(* ac_val_base)[16]
Definition: mpegvideo.h:319
const int8_t * table_run
Definition: rl.h:42
#define FF_DEBUG_SKIP
Definition: avcodec.h:2391
#define FFABS(a)
Definition: common.h:52
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:2568
int16_t(*[2][2][2] b_field_mv_table_base)[2]
Definition: mpegvideo.h:371
int16_t(* b_forw_mv_table_base)[2]
Definition: mpegvideo.h:365
#define AV_RL32
Definition: intreadwrite.h:146
int16_t(*[12] pblocks)[64]
Definition: mpegvideo.h:598
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:209
MotionEstContext me
Definition: mpegvideo.h:404
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:94
int mb_decision
macroblock decision mode
Definition: avcodec.h:1597
void(* idct_add)(uint8_t *dest, int line_size, int16_t *block)
block -> idct -> add dest -> clip to unsigned 8 bit -> dest.
Definition: idctdsp.h:77
uint8_t * mbintra_table
used to avoid setting {ac, dc, cbp}-pred stuff to zero on inter MB decoding
Definition: mpegvideo.h:324
#define ME_MAP_SIZE
Definition: mpegvideo.h:72
#define HAVE_THREADS
Definition: config.h:302
#define DELAYED_PIC_REF
Value of Picture.reference when Picture is not a reference picture, but is held for delayed output...
Definition: mpegutils.h:41
int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: mpegvideo.c:899
RL_VLC_ELEM * rl_vlc[32]
decoding only
Definition: rl.h:48
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:110
void ff_mpv_decode_defaults(MpegEncContext *s)
Set the given MpegEncContext to defaults for decoding.
Definition: mpegvideo.c:1050
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2556
int block_index[6]
index to current MB in block based arrays with edges
Definition: mpegvideo.h:415
#define IS_INTRA16x16(a)
Definition: mpegutils.h:72
if(ac->has_optimized_func)
int * mb_index2xy
mb_index -> mb_x + mb_y*mb_stride
Definition: mpegvideo.h:419
int first_field
is 1 for the first field of a field picture 0 otherwise
Definition: mpegvideo.h:590
int ff_mpv_lowest_referenced_row(MpegEncContext *s, int dir)
find the lowest MB row referenced in the MVs
Definition: mpegvideo.c:2084
static const int8_t mv[256][2]
Definition: 4xm.c:75
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:186
void(* idct_put)(uint8_t *dest, int line_size, int16_t *block)
block -> idct -> clip to unsigned 8 bit -> dest.
Definition: idctdsp.h:70
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:388
static void clear_context(MpegEncContext *s)
Definition: mpegvideo.c:1231
void ff_wmv2_add_mb(MpegEncContext *s, int16_t block[6][64], uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr)
Definition: wmv2.c:78
NULL
Definition: eval.c:55
uint16_t * mc_mb_var
Table for motion compensated MB variances.
Definition: mpegvideo.h:122
AVBufferRef * qscale_table_buf
Definition: mpegvideo.h:103
#define MV_DIR_BACKWARD
Definition: mpegvideo.h:385
int16_t(* b_bidir_forw_mv_table_base)[2]
Definition: mpegvideo.h:367
const uint8_t *const ff_mpeg2_dc_scale_table[4]
Definition: mpegvideo.c:103
int coded_picture_number
picture number in bitstream order
Definition: frame.h:226
#define AV_LOG_INFO
Standard information.
Definition: log.h:134
uint16_t inter_matrix[64]
Definition: mpegvideo.h:424
#define IS_INTERLACED(a)
Definition: mpegutils.h:79
uint8_t * buffer
Definition: parser.h:29
struct MpegEncContext * thread_context[MAX_THREADS]
Definition: mpegvideo.h:281
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
Definition: utils.c:518
Libavcodec external API header.
void ff_free_picture_tables(Picture *pic)
Definition: mpegvideo.c:533
ptrdiff_t linesize
line size, in bytes, may be different from width
Definition: mpegvideo.h:260
BlockDSPContext bdsp
Definition: mpegvideo.h:351
av_cold void ff_blockdsp_init(BlockDSPContext *c, AVCodecContext *avctx)
Definition: blockdsp.c:58
enum AVDiscard skip_idct
Definition: avcodec.h:2736
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
int debug
debug
Definition: avcodec.h:2378
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:1050
ScanTable intra_scantable
Definition: mpegvideo.h:214
uint8_t * data
The data buffer.
Definition: buffer.h:89
uint8_t * coded_block
used for coded block pattern prediction (msmpeg4v3, wmv1)
Definition: mpegvideo.h:318
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:223
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:193
const uint8_t ff_mpeg1_dc_scale_table[128]
Definition: mpegvideo.c:55
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition: hpeldsp.h:56
static av_always_inline void mpv_decode_mb_internal(MpegEncContext *s, int16_t block[12][64], int is_mpeg12)
Definition: mpegvideo.c:2190
#define MV_TYPE_16X8
2 vectors, one per 16x8 block
Definition: mpegvideo.h:390
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
Picture * picture
main picture buffer
Definition: mpegvideo.h:262
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:82
#define AVERROR_BUG
Bug detected, please report the issue.
Definition: error.h:60
int16_t(* blocks)[8][64]
Definition: mpegvideo.h:601
int progressive_sequence
Definition: mpegvideo.h:566
int coded_height
Definition: avcodec.h:1244
Switching Predicted.
Definition: avutil.h:258
#define IS_16X16(a)
Definition: mpegutils.h:82
#define CONFIG_WMV2_ENCODER
Definition: config.h:1002
ScanTable intra_h_scantable
Definition: mpegvideo.h:215
int16_t(*[2][2][2] b_field_mv_table)[2]
MV table (4MV per MB) interlaced b-frame encoding.
Definition: mpegvideo.h:379
uint8_t * cbp_table
used to store cbp, ac_pred for partitioned decoding
Definition: mpegvideo.h:325
#define UPDATE_TABLE(table)
unsigned int avpriv_toupper4(unsigned int x)
Definition: utils.c:2341
#define FF_DEBUG_DCT_COEFF
Definition: avcodec.h:2390
struct AVFrame * f
Definition: mpegvideo.h:100
#define FF_MB_DECISION_RD
rate distortion
Definition: avcodec.h:1600
#define IS_8X16(a)
Definition: mpegutils.h:84
uint8_t * index_run[2]
encoding only
Definition: rl.h:44
op_pixels_func put_no_rnd_pixels_tab[2][4]
Halfpel motion compensation with no rounding (a+b)>>1.
Definition: hpeldsp.h:80
int context_initialized
Definition: mpegvideo.h:250
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:115
ptrdiff_t uvlinesize
line size, for chroma in bytes, may be different from width
Definition: mpegvideo.h:261
int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
generic function called after decoding the header and before a frame is decoded.
Definition: mpegvideo.c:1751
int f_code
forward MV resolution
Definition: mpegvideo.h:362
#define COPY(a)
AVCodecContext * avctx
#define MV_DIR_FORWARD
Definition: mpegvideo.h:384
int max_b_frames
max number of b-frames for encoding
Definition: mpegvideo.h:240
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:339
int h263_pred
use mpeg4/h263 ac/dc predictions
Definition: mpegvideo.h:228
int16_t(* b_bidir_back_mv_table)[2]
MV table (1MV per MB) bidir mode b-frame encoding.
Definition: mpegvideo.h:376
#define ARCH_PPC
Definition: config.h:24
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:283
#define EDGE_WIDTH
Definition: mpegvideo.h:78
static int init_context_frame(MpegEncContext *s)
Initialize and allocates MpegEncContext fields dependent on the resolution.
Definition: mpegvideo.c:1095
#define IS_PCM(a)
Definition: mpegutils.h:73
uint8_t *[2] p_field_select_table
Definition: mpegvideo.h:380
int16_t(* b_direct_mv_table)[2]
MV table (1MV per MB) direct mode b-frame encoding.
Definition: mpegvideo.h:377
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
const uint8_t * c_dc_scale_table
qscale -> c_dc_scale table
Definition: mpegvideo.h:315
uint8_t level
Definition: svq3.c:147
qpel_mc_func(* qpel_avg)[16]
Definition: mpegvideo.h:192
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
Definition: mpegvideo.h:398
int16_t(* b_forw_mv_table)[2]
MV table (1MV per MB) forward mode b-frame encoding.
Definition: mpegvideo.h:373
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
Definition: mpegvideo.h:257
int noise_reduction
noise reduction strength
Definition: avcodec.h:1629
static void dct_unquantize_h263_intra_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:287
MpegEncContext.
Definition: mpegvideo.h:204
uint8_t run
Definition: get_bits.h:73
Picture * next_picture_ptr
pointer to the next picture (for bidir pred)
Definition: mpegvideo.h:309
int8_t * qscale_table
Definition: mpegvideo.h:104
#define MAX_RUN
Definition: rl.h:34
struct AVCodecContext * avctx
Definition: mpegvideo.h:221
discard all non reference
Definition: avcodec.h:565
static void exchange_uv(MpegEncContext *s)
Definition: mpegvideo.c:771
MpegVideoDSPContext mdsp
Definition: mpegvideo.h:356
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:388
int(* dct_error_sum)[64]
Definition: mpegvideo.h:449
MECmpContext mecc
Definition: mpegvideo.h:355
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:76
common internal api header.
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
Definition: mpegvideo.h:256
AVBufferRef * mbskip_table_buf
Definition: mpegvideo.h:112
#define CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:637
uint8_t * dest[3]
Definition: mpegvideo.h:417
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
Definition: internal.h:117
int shared
Definition: mpegvideo.h:142
static av_cold int dct_init(MpegEncContext *s)
Definition: mpegvideo.c:378
int last_pict_type
Definition: mpegvideo.h:340
static void dct_unquantize_h263_inter_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:324
int16_t * dc_val[3]
Picture last_picture
copy of the previous picture structure.
Definition: mpegvideo.h:288
uint8_t * obmc_scratchpad
Definition: mpegvideo.h:329
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
#define FF_DEBUG_QP
Definition: avcodec.h:2383
static int alloc_frame_buffer(MpegEncContext *s, Picture *pic)
Allocate a frame buffer.
Definition: mpegvideo.c:454
Picture * last_picture_ptr
pointer to the previous picture.
Definition: mpegvideo.h:308
Bi-dir predicted.
Definition: avutil.h:255
int index
Definition: parser.h:30
FF_ENABLE_DEPRECATION_WARNINGS int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:1625
const uint8_t * chroma_qscale_table
qscale -> chroma_qscale (h263)
Definition: mpegvideo.h:316
uint32_t * map
map to avoid duplicate evaluations
Definition: mpegvideo.h:158
int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
Definition: mpegvideo.c:874
void(* dct_unquantize_mpeg1_inter)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:610
#define IS_INTRA(x, y)
int slices
Number of slices.
Definition: avcodec.h:1798
void * priv_data
Definition: avcodec.h:1092
#define PICT_FRAME
Definition: mpegutils.h:35
av_cold int ff_mpv_common_init(MpegEncContext *s)
init common structure for both encoder and decoder.
Definition: mpegvideo.c:1311
#define IS_INTRA4x4(a)
Definition: mpegutils.h:71
int picture_structure
Definition: mpegvideo.h:570
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: idctdsp.c:28
VideoDSPContext vdsp
Definition: mpegvideo.h:360
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: idctdsp.c:156
#define IS_8X8(a)
Definition: mpegutils.h:85
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:325
void ff_xvmc_decode_mb(MpegEncContext *s)
int len
void ff_mpv_frame_end(MpegEncContext *s)
Definition: mpegvideo.c:1963
int frame_priv_data_size
Size of per-frame hardware accelerator private data.
Definition: avcodec.h:3005
av_cold void ff_init_rl(RLTable *rl, uint8_t static_store[2][2 *MAX_RUN+MAX_LEVEL+3])
Definition: mpegvideo.c:1602
int16_t(* block)[64]
points to one of the following blocks
Definition: mpegvideo.h:600
ParseContext parse_context
Definition: mpegvideo.h:479
VLC_TYPE(* table)[2]
code, bits
Definition: get_bits.h:66
static void add_dequant_dct(MpegEncContext *s, int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
Definition: mpegvideo.c:2136
Picture next_picture
copy of the next picture structure.
Definition: mpegvideo.h:294
AVBufferRef * mc_mb_var_buf
Definition: mpegvideo.h:121
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
static const uint8_t mpeg2_dc_scale_table1[128]
Definition: mpegvideo.c:67
#define IS_ACPRED(a)
Definition: mpegutils.h:90
int16_t level
Definition: get_bits.h:71
int flags2
CODEC_FLAG2_*.
Definition: avcodec.h:1151
static int init_er(MpegEncContext *s)
Definition: mpegvideo.c:1055
static const uint8_t mpeg2_dc_scale_table2[128]
Definition: mpegvideo.c:79
int chroma_qscale
chroma QP
Definition: mpegvideo.h:333
void(* dct_unquantize_mpeg2_intra)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:612
int mb_var_sum
sum of MB variance for current frame
Definition: mpegvideo.h:135
void ff_mpv_common_defaults(MpegEncContext *s)
Set the given MpegEncContext to common defaults (same for encoding and decoding). ...
Definition: mpegvideo.c:1027
int height
Definition: frame.h:174
int flags
AVCodecContext.flags (HQ, MV4, ...)
Definition: mpegvideo.h:238
int mc_mb_var_sum
motion compensated MB variance for current frame
Definition: mpegvideo.h:136
#define CONFIG_MPEG_XVMC_DECODER
Definition: config.h:542
uint16_t intra_matrix[64]
matrix transmitted in the bitstream
Definition: mpegvideo.h:422
uint32_t * mb_type
types and macros are defined in mpegutils.h
Definition: mpegvideo.h:110
int workaround_bugs
workaround bugs in encoders which cannot be detected automatically
Definition: mpegvideo.h:244
ScanTable inter_scantable
if inter == intra then intra should be used to reduce tha cache usage
Definition: mpegvideo.h:213
#define av_always_inline
Definition: attributes.h:40
uint8_t * temp
Definition: mpegvideo.h:156
int ff_find_unused_picture(MpegEncContext *s, int shared)
Definition: mpegvideo.c:1733
#define MV_TYPE_8X8
4 vectors (h263, mpeg4 4MV)
Definition: mpegvideo.h:389
void ff_xvmc_field_end(MpegEncContext *s)
int16_t(* b_direct_mv_table_base)[2]
Definition: mpegvideo.h:369
int b_code
backward MV resolution for B Frames (mpeg4)
Definition: mpegvideo.h:363
float * bits_tab
Definition: mpegvideo.h:632
uint8_t * mbskip_table
void ff_mpv_report_decode_progress(MpegEncContext *s)
Definition: mpegvideo.c:2547
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:850
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
static int frame_size_alloc(MpegEncContext *s, int linesize)
Definition: mpegvideo.c:426
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)
Definition: internal.h:126
Predicted.
Definition: avutil.h:254
AVBufferRef * ref_index_buf[2]
Definition: mpegvideo.h:115
HpelDSPContext hdsp
Definition: mpegvideo.h:353
static int16_t block[64]
Definition: dct-test.c:88