FFmpeg  2.7.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 FFmpeg.
9  *
10  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 /**
26  * @file
27  * The simplest mpeg encoder (well, it was the simplest!).
28  */
29 
30 #include "libavutil/attributes.h"
31 #include "libavutil/avassert.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/internal.h"
35 #include "libavutil/timer.h"
36 #include "avcodec.h"
37 #include "blockdsp.h"
38 #include "h264chroma.h"
39 #include "idctdsp.h"
40 #include "internal.h"
41 #include "mathops.h"
42 #include "mpegutils.h"
43 #include "mpegvideo.h"
44 #include "mpegvideodata.h"
45 #include "mjpegenc.h"
46 #include "msmpeg4.h"
47 #include "qpeldsp.h"
48 #include "thread.h"
49 #include "wmv2.h"
50 #include <limits.h>
51 
53  int16_t *block, int n, int qscale)
54 {
55  int i, level, nCoeffs;
56  const uint16_t *quant_matrix;
57 
58  nCoeffs= s->block_last_index[n];
59 
60  block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
61  /* XXX: only mpeg1 */
62  quant_matrix = s->intra_matrix;
63  for(i=1;i<=nCoeffs;i++) {
64  int j= s->intra_scantable.permutated[i];
65  level = block[j];
66  if (level) {
67  if (level < 0) {
68  level = -level;
69  level = (int)(level * qscale * quant_matrix[j]) >> 3;
70  level = (level - 1) | 1;
71  level = -level;
72  } else {
73  level = (int)(level * qscale * quant_matrix[j]) >> 3;
74  level = (level - 1) | 1;
75  }
76  block[j] = level;
77  }
78  }
79 }
80 
82  int16_t *block, int n, int qscale)
83 {
84  int i, level, nCoeffs;
85  const uint16_t *quant_matrix;
86 
87  nCoeffs= s->block_last_index[n];
88 
89  quant_matrix = s->inter_matrix;
90  for(i=0; i<=nCoeffs; i++) {
91  int j= s->intra_scantable.permutated[i];
92  level = block[j];
93  if (level) {
94  if (level < 0) {
95  level = -level;
96  level = (((level << 1) + 1) * qscale *
97  ((int) (quant_matrix[j]))) >> 4;
98  level = (level - 1) | 1;
99  level = -level;
100  } else {
101  level = (((level << 1) + 1) * qscale *
102  ((int) (quant_matrix[j]))) >> 4;
103  level = (level - 1) | 1;
104  }
105  block[j] = level;
106  }
107  }
108 }
109 
111  int16_t *block, int n, int qscale)
112 {
113  int i, level, nCoeffs;
114  const uint16_t *quant_matrix;
115 
116  if(s->alternate_scan) nCoeffs= 63;
117  else nCoeffs= s->block_last_index[n];
118 
119  block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
120  quant_matrix = s->intra_matrix;
121  for(i=1;i<=nCoeffs;i++) {
122  int j= s->intra_scantable.permutated[i];
123  level = block[j];
124  if (level) {
125  if (level < 0) {
126  level = -level;
127  level = (int)(level * qscale * quant_matrix[j]) >> 3;
128  level = -level;
129  } else {
130  level = (int)(level * qscale * quant_matrix[j]) >> 3;
131  }
132  block[j] = level;
133  }
134  }
135 }
136 
138  int16_t *block, int n, int qscale)
139 {
140  int i, level, nCoeffs;
141  const uint16_t *quant_matrix;
142  int sum=-1;
143 
144  if(s->alternate_scan) nCoeffs= 63;
145  else nCoeffs= s->block_last_index[n];
146 
147  block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
148  sum += block[0];
149  quant_matrix = s->intra_matrix;
150  for(i=1;i<=nCoeffs;i++) {
151  int j= s->intra_scantable.permutated[i];
152  level = block[j];
153  if (level) {
154  if (level < 0) {
155  level = -level;
156  level = (int)(level * qscale * quant_matrix[j]) >> 3;
157  level = -level;
158  } else {
159  level = (int)(level * qscale * quant_matrix[j]) >> 3;
160  }
161  block[j] = level;
162  sum+=level;
163  }
164  }
165  block[63]^=sum&1;
166 }
167 
169  int16_t *block, int n, int qscale)
170 {
171  int i, level, nCoeffs;
172  const uint16_t *quant_matrix;
173  int sum=-1;
174 
175  if(s->alternate_scan) nCoeffs= 63;
176  else nCoeffs= s->block_last_index[n];
177 
178  quant_matrix = s->inter_matrix;
179  for(i=0; i<=nCoeffs; i++) {
180  int j= s->intra_scantable.permutated[i];
181  level = block[j];
182  if (level) {
183  if (level < 0) {
184  level = -level;
185  level = (((level << 1) + 1) * qscale *
186  ((int) (quant_matrix[j]))) >> 4;
187  level = -level;
188  } else {
189  level = (((level << 1) + 1) * qscale *
190  ((int) (quant_matrix[j]))) >> 4;
191  }
192  block[j] = level;
193  sum+=level;
194  }
195  }
196  block[63]^=sum&1;
197 }
198 
200  int16_t *block, int n, int qscale)
201 {
202  int i, level, qmul, qadd;
203  int nCoeffs;
204 
205  av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
206 
207  qmul = qscale << 1;
208 
209  if (!s->h263_aic) {
210  block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
211  qadd = (qscale - 1) | 1;
212  }else{
213  qadd = 0;
214  }
215  if(s->ac_pred)
216  nCoeffs=63;
217  else
218  nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
219 
220  for(i=1; i<=nCoeffs; i++) {
221  level = block[i];
222  if (level) {
223  if (level < 0) {
224  level = level * qmul - qadd;
225  } else {
226  level = level * qmul + qadd;
227  }
228  block[i] = level;
229  }
230  }
231 }
232 
234  int16_t *block, int n, int qscale)
235 {
236  int i, level, qmul, qadd;
237  int nCoeffs;
238 
239  av_assert2(s->block_last_index[n]>=0);
240 
241  qadd = (qscale - 1) | 1;
242  qmul = qscale << 1;
243 
244  nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
245 
246  for(i=0; i<=nCoeffs; i++) {
247  level = block[i];
248  if (level) {
249  if (level < 0) {
250  level = level * qmul - qadd;
251  } else {
252  level = level * qmul + qadd;
253  }
254  block[i] = level;
255  }
256  }
257 }
258 
259 static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
260  int (*mv)[2][4][2],
261  int mb_x, int mb_y, int mb_intra, int mb_skipped)
262 {
263  MpegEncContext *s = opaque;
264 
265  s->mv_dir = mv_dir;
266  s->mv_type = mv_type;
267  s->mb_intra = mb_intra;
268  s->mb_skipped = mb_skipped;
269  s->mb_x = mb_x;
270  s->mb_y = mb_y;
271  memcpy(s->mv, mv, sizeof(*mv));
272 
275 
276  s->bdsp.clear_blocks(s->block[0]);
277 
278  s->dest[0] = s->current_picture.f->data[0] + (s->mb_y * 16 * s->linesize) + s->mb_x * 16;
279  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);
280  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);
281 
282  if (ref)
284  "Interlaced error concealment is not fully implemented\n");
285  ff_mpv_decode_mb(s, s->block);
286 }
287 
288 static void gray16(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
289 {
290  while(h--)
291  memset(dst + h*linesize, 128, 16);
292 }
293 
294 static void gray8(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
295 {
296  while(h--)
297  memset(dst + h*linesize, 128, 8);
298 }
299 
300 /* init common dct for both encoder and decoder */
302 {
303  ff_blockdsp_init(&s->bdsp, s->avctx);
304  ff_h264chroma_init(&s->h264chroma, 8); //for lowres
305  ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
308 
309  if (s->avctx->debug & FF_DEBUG_NOMC) {
310  int i;
311  for (i=0; i<4; i++) {
312  s->hdsp.avg_pixels_tab[0][i] = gray16;
313  s->hdsp.put_pixels_tab[0][i] = gray16;
314  s->hdsp.put_no_rnd_pixels_tab[0][i] = gray16;
315 
316  s->hdsp.avg_pixels_tab[1][i] = gray8;
317  s->hdsp.put_pixels_tab[1][i] = gray8;
318  s->hdsp.put_no_rnd_pixels_tab[1][i] = gray8;
319  }
320  }
321 
327  if (s->avctx->flags & CODEC_FLAG_BITEXACT)
330 
333 
334  if (ARCH_ALPHA)
336  if (ARCH_ARM)
338  if (ARCH_PPC)
340  if (ARCH_X86)
342 
343  return 0;
344 }
345 
347 {
348  ff_idctdsp_init(&s->idsp, s->avctx);
349 
350  /* load & permutate scantables
351  * note: only wmv uses different ones
352  */
353  if (s->alternate_scan) {
356  } else {
359  }
362 }
363 
365  ScratchpadContext *sc, int linesize)
366 {
367  int alloc_size = FFALIGN(FFABS(linesize) + 64, 32);
368 
369  if (avctx->hwaccel || avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)
370  return 0;
371 
372  if (linesize < 24) {
373  av_log(avctx, AV_LOG_ERROR, "Image too small, temporary buffers cannot function\n");
374  return AVERROR_PATCHWELCOME;
375  }
376 
377  // edge emu needs blocksize + filter length - 1
378  // (= 17x17 for halfpel / 21x21 for h264)
379  // VC1 computes luma and chroma simultaneously and needs 19X19 + 9x9
380  // at uvlinesize. It supports only YUV420 so 24x24 is enough
381  // linesize * interlaced * MBsize
382  // we also use this buffer for encoding in encode_mb_internal() needig an additional 32 lines
383  FF_ALLOCZ_ARRAY_OR_GOTO(avctx, sc->edge_emu_buffer, alloc_size, 4 * 68,
384  fail);
385 
386  FF_ALLOCZ_ARRAY_OR_GOTO(avctx, me->scratchpad, alloc_size, 4 * 16 * 2,
387  fail)
388  me->temp = me->scratchpad;
389  sc->rd_scratchpad = me->scratchpad;
390  sc->b_scratchpad = me->scratchpad;
391  sc->obmc_scratchpad = me->scratchpad + 16;
392 
393  return 0;
394 fail:
396  return AVERROR(ENOMEM);
397 }
398 
399 /**
400  * Allocate a frame buffer
401  */
402 static int alloc_frame_buffer(AVCodecContext *avctx, Picture *pic,
404  int chroma_x_shift, int chroma_y_shift,
405  int linesize, int uvlinesize)
406 {
407  int edges_needed = av_codec_is_encoder(avctx->codec);
408  int r, ret;
409 
410  pic->tf.f = pic->f;
411  if (avctx->codec_id != AV_CODEC_ID_WMV3IMAGE &&
412  avctx->codec_id != AV_CODEC_ID_VC1IMAGE &&
413  avctx->codec_id != AV_CODEC_ID_MSS2) {
414  if (edges_needed) {
415  pic->f->width = avctx->width + 2 * EDGE_WIDTH;
416  pic->f->height = avctx->height + 2 * EDGE_WIDTH;
417  }
418 
419  r = ff_thread_get_buffer(avctx, &pic->tf,
420  pic->reference ? AV_GET_BUFFER_FLAG_REF : 0);
421  } else {
422  pic->f->width = avctx->width;
423  pic->f->height = avctx->height;
424  pic->f->format = avctx->pix_fmt;
425  r = avcodec_default_get_buffer2(avctx, pic->f, 0);
426  }
427 
428  if (r < 0 || !pic->f->buf[0]) {
429  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed (%d %p)\n",
430  r, pic->f->data[0]);
431  return -1;
432  }
433 
434  if (edges_needed) {
435  int i;
436  for (i = 0; pic->f->data[i]; i++) {
437  int offset = (EDGE_WIDTH >> (i ? chroma_y_shift : 0)) *
438  pic->f->linesize[i] +
439  (EDGE_WIDTH >> (i ? chroma_x_shift : 0));
440  pic->f->data[i] += offset;
441  }
442  pic->f->width = avctx->width;
443  pic->f->height = avctx->height;
444  }
445 
446  if (avctx->hwaccel) {
447  assert(!pic->hwaccel_picture_private);
448  if (avctx->hwaccel->frame_priv_data_size) {
450  if (!pic->hwaccel_priv_buf) {
451  av_log(avctx, AV_LOG_ERROR, "alloc_frame_buffer() failed (hwaccel private data allocation)\n");
452  return -1;
453  }
455  }
456  }
457 
458  if (linesize && (linesize != pic->f->linesize[0] ||
459  uvlinesize != pic->f->linesize[1])) {
460  av_log(avctx, AV_LOG_ERROR,
461  "get_buffer() failed (stride changed)\n");
462  ff_mpeg_unref_picture(avctx, pic);
463  return -1;
464  }
465 
466  if (pic->f->linesize[1] != pic->f->linesize[2]) {
467  av_log(avctx, AV_LOG_ERROR,
468  "get_buffer() failed (uv stride mismatch)\n");
469  ff_mpeg_unref_picture(avctx, pic);
470  return -1;
471  }
472 
473  if (!sc->edge_emu_buffer &&
474  (ret = ff_mpeg_framesize_alloc(avctx, me, sc,
475  pic->f->linesize[0])) < 0) {
476  av_log(avctx, AV_LOG_ERROR,
477  "get_buffer() failed to allocate context scratch buffers.\n");
478  ff_mpeg_unref_picture(avctx, pic);
479  return ret;
480  }
481 
482  return 0;
483 }
484 
486 {
487  int i;
488 
489  pic->alloc_mb_width =
490  pic->alloc_mb_height = 0;
491 
498 
499  for (i = 0; i < 2; i++) {
501  av_buffer_unref(&pic->ref_index_buf[i]);
502  }
503 }
504 
505 static int alloc_picture_tables(AVCodecContext *avctx, Picture *pic, int encoding, int out_format,
506  int mb_stride, int mb_width, int mb_height, int b8_stride)
507 {
508  const int big_mb_num = mb_stride * (mb_height + 1) + 1;
509  const int mb_array_size = mb_stride * mb_height;
510  const int b8_array_size = b8_stride * mb_height * 2;
511  int i;
512 
513 
514  pic->mbskip_table_buf = av_buffer_allocz(mb_array_size + 2);
515  pic->qscale_table_buf = av_buffer_allocz(big_mb_num + mb_stride);
516  pic->mb_type_buf = av_buffer_allocz((big_mb_num + mb_stride) *
517  sizeof(uint32_t));
518  if (!pic->mbskip_table_buf || !pic->qscale_table_buf || !pic->mb_type_buf)
519  return AVERROR(ENOMEM);
520 
521  if (encoding) {
522  pic->mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
523  pic->mc_mb_var_buf = av_buffer_allocz(mb_array_size * sizeof(int16_t));
524  pic->mb_mean_buf = av_buffer_allocz(mb_array_size);
525  if (!pic->mb_var_buf || !pic->mc_mb_var_buf || !pic->mb_mean_buf)
526  return AVERROR(ENOMEM);
527  }
528 
529  if (out_format == FMT_H263 || encoding || avctx->debug_mv ||
530  (avctx->flags2 & CODEC_FLAG2_EXPORT_MVS)) {
531  int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
532  int ref_index_size = 4 * mb_array_size;
533 
534  for (i = 0; mv_size && i < 2; i++) {
535  pic->motion_val_buf[i] = av_buffer_allocz(mv_size);
536  pic->ref_index_buf[i] = av_buffer_allocz(ref_index_size);
537  if (!pic->motion_val_buf[i] || !pic->ref_index_buf[i])
538  return AVERROR(ENOMEM);
539  }
540  }
541 
542  pic->alloc_mb_width = mb_width;
543  pic->alloc_mb_height = mb_height;
544 
545  return 0;
546 }
547 
549 {
550  int ret, i;
551 #define MAKE_WRITABLE(table) \
552 do {\
553  if (pic->table &&\
554  (ret = av_buffer_make_writable(&pic->table)) < 0)\
555  return ret;\
556 } while (0)
557 
558  MAKE_WRITABLE(mb_var_buf);
559  MAKE_WRITABLE(mc_mb_var_buf);
560  MAKE_WRITABLE(mb_mean_buf);
561  MAKE_WRITABLE(mbskip_table_buf);
562  MAKE_WRITABLE(qscale_table_buf);
563  MAKE_WRITABLE(mb_type_buf);
564 
565  for (i = 0; i < 2; i++) {
566  MAKE_WRITABLE(motion_val_buf[i]);
567  MAKE_WRITABLE(ref_index_buf[i]);
568  }
569 
570  return 0;
571 }
572 
573 static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
574 {
575  return ff_alloc_picture(s->avctx, pic, &s->me, &s->sc, shared, 0,
577  s->mb_stride, s->mb_width, s->mb_height, s->b8_stride,
578  &s->linesize, &s->uvlinesize);
579 }
580 
581 /**
582  * Allocate a Picture.
583  * The pixels are allocated/set by calling get_buffer() if shared = 0
584  */
586  ScratchpadContext *sc, int shared, int encoding,
587  int chroma_x_shift, int chroma_y_shift, int out_format,
588  int mb_stride, int mb_width, int mb_height, int b8_stride,
589  ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
590 {
591  int i, ret;
592 
593  if (pic->qscale_table_buf)
594  if ( pic->alloc_mb_width != mb_width
595  || pic->alloc_mb_height != mb_height)
597 
598  if (shared) {
599  av_assert0(pic->f->data[0]);
600  pic->shared = 1;
601  } else {
602  av_assert0(!pic->f->buf[0]);
603  if (alloc_frame_buffer(avctx, pic, me, sc,
604  chroma_x_shift, chroma_y_shift,
605  *linesize, *uvlinesize) < 0)
606  return -1;
607 
608  *linesize = pic->f->linesize[0];
609  *uvlinesize = pic->f->linesize[1];
610  }
611 
612  if (!pic->qscale_table_buf)
613  ret = alloc_picture_tables(avctx, pic, encoding, out_format,
614  mb_stride, mb_width, mb_height, b8_stride);
615  else
616  ret = make_tables_writable(pic);
617  if (ret < 0)
618  goto fail;
619 
620  if (encoding) {
621  pic->mb_var = (uint16_t*)pic->mb_var_buf->data;
622  pic->mc_mb_var = (uint16_t*)pic->mc_mb_var_buf->data;
623  pic->mb_mean = pic->mb_mean_buf->data;
624  }
625 
626  pic->mbskip_table = pic->mbskip_table_buf->data;
627  pic->qscale_table = pic->qscale_table_buf->data + 2 * mb_stride + 1;
628  pic->mb_type = (uint32_t*)pic->mb_type_buf->data + 2 * mb_stride + 1;
629 
630  if (pic->motion_val_buf[0]) {
631  for (i = 0; i < 2; i++) {
632  pic->motion_val[i] = (int16_t (*)[2])pic->motion_val_buf[i]->data + 4;
633  pic->ref_index[i] = pic->ref_index_buf[i]->data;
634  }
635  }
636 
637  return 0;
638 fail:
639  av_log(avctx, AV_LOG_ERROR, "Error allocating a picture.\n");
640  ff_mpeg_unref_picture(avctx, pic);
642  return AVERROR(ENOMEM);
643 }
644 
645 /**
646  * Deallocate a picture.
647  */
649 {
650  int off = offsetof(Picture, mb_mean) + sizeof(pic->mb_mean);
651 
652  pic->tf.f = pic->f;
653  /* WM Image / Screen codecs allocate internal buffers with different
654  * dimensions / colorspaces; ignore user-defined callbacks for these. */
655  if (avctx->codec->id != AV_CODEC_ID_WMV3IMAGE &&
656  avctx->codec->id != AV_CODEC_ID_VC1IMAGE &&
657  avctx->codec->id != AV_CODEC_ID_MSS2)
658  ff_thread_release_buffer(avctx, &pic->tf);
659  else if (pic->f)
660  av_frame_unref(pic->f);
661 
663 
664  if (pic->needs_realloc)
666 
667  memset((uint8_t*)pic + off, 0, sizeof(*pic) - off);
668 }
669 
671 {
672  int i;
673 
674 #define UPDATE_TABLE(table)\
675 do {\
676  if (src->table &&\
677  (!dst->table || dst->table->buffer != src->table->buffer)) {\
678  av_buffer_unref(&dst->table);\
679  dst->table = av_buffer_ref(src->table);\
680  if (!dst->table) {\
681  ff_free_picture_tables(dst);\
682  return AVERROR(ENOMEM);\
683  }\
684  }\
685 } while (0)
686 
687  UPDATE_TABLE(mb_var_buf);
688  UPDATE_TABLE(mc_mb_var_buf);
689  UPDATE_TABLE(mb_mean_buf);
690  UPDATE_TABLE(mbskip_table_buf);
691  UPDATE_TABLE(qscale_table_buf);
692  UPDATE_TABLE(mb_type_buf);
693  for (i = 0; i < 2; i++) {
694  UPDATE_TABLE(motion_val_buf[i]);
695  UPDATE_TABLE(ref_index_buf[i]);
696  }
697 
698  dst->mb_var = src->mb_var;
699  dst->mc_mb_var = src->mc_mb_var;
700  dst->mb_mean = src->mb_mean;
701  dst->mbskip_table = src->mbskip_table;
702  dst->qscale_table = src->qscale_table;
703  dst->mb_type = src->mb_type;
704  for (i = 0; i < 2; i++) {
705  dst->motion_val[i] = src->motion_val[i];
706  dst->ref_index[i] = src->ref_index[i];
707  }
708 
709  dst->alloc_mb_width = src->alloc_mb_width;
710  dst->alloc_mb_height = src->alloc_mb_height;
711 
712  return 0;
713 }
714 
716 {
717  int ret;
718 
719  av_assert0(!dst->f->buf[0]);
720  av_assert0(src->f->buf[0]);
721 
722  src->tf.f = src->f;
723  dst->tf.f = dst->f;
724  ret = ff_thread_ref_frame(&dst->tf, &src->tf);
725  if (ret < 0)
726  goto fail;
727 
728  ret = update_picture_tables(dst, src);
729  if (ret < 0)
730  goto fail;
731 
732  if (src->hwaccel_picture_private) {
734  if (!dst->hwaccel_priv_buf)
735  goto fail;
737  }
738 
739  dst->field_picture = src->field_picture;
740  dst->mb_var_sum = src->mb_var_sum;
741  dst->mc_mb_var_sum = src->mc_mb_var_sum;
742  dst->b_frame_score = src->b_frame_score;
743  dst->needs_realloc = src->needs_realloc;
744  dst->reference = src->reference;
745  dst->shared = src->shared;
746 
747  return 0;
748 fail:
749  ff_mpeg_unref_picture(avctx, dst);
750  return ret;
751 }
752 
754 {
755  int y_size = s->b8_stride * (2 * s->mb_height + 1);
756  int c_size = s->mb_stride * (s->mb_height + 1);
757  int yc_size = y_size + 2 * c_size;
758  int i;
759 
760  if (s->mb_height & 1)
761  yc_size += 2*s->b8_stride + 2*s->mb_stride;
762 
763  s->sc.edge_emu_buffer =
764  s->me.scratchpad =
765  s->me.temp =
766  s->sc.rd_scratchpad =
767  s->sc.b_scratchpad =
768  s->sc.obmc_scratchpad = NULL;
769 
770  if (s->encoding) {
771  FF_ALLOCZ_OR_GOTO(s->avctx, s->me.map,
772  ME_MAP_SIZE * sizeof(uint32_t), fail)
774  ME_MAP_SIZE * sizeof(uint32_t), fail)
775  if (s->avctx->noise_reduction) {
777  2 * 64 * sizeof(int), fail)
778  }
779  }
780  FF_ALLOCZ_OR_GOTO(s->avctx, s->blocks, 64 * 12 * 2 * sizeof(int16_t), fail)
781  s->block = s->blocks[0];
782 
783  for (i = 0; i < 12; i++) {
784  s->pblocks[i] = &s->block[i];
785  }
786  if (s->avctx->codec_tag == AV_RL32("VCR2")) {
787  // exchange uv
788  FFSWAP(void *, s->pblocks[4], s->pblocks[5]);
789  }
790 
791  if (s->out_format == FMT_H263) {
792  /* ac values */
794  yc_size * sizeof(int16_t) * 16, fail);
795  s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
796  s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
797  s->ac_val[2] = s->ac_val[1] + c_size;
798  }
799 
800  return 0;
801 fail:
802  return -1; // free() through ff_mpv_common_end()
803 }
804 
806 {
807  if (!s)
808  return;
809 
811  av_freep(&s->me.scratchpad);
812  s->me.temp =
813  s->sc.rd_scratchpad =
814  s->sc.b_scratchpad =
815  s->sc.obmc_scratchpad = NULL;
816 
817  av_freep(&s->dct_error_sum);
818  av_freep(&s->me.map);
819  av_freep(&s->me.score_map);
820  av_freep(&s->blocks);
821  av_freep(&s->ac_val_base);
822  s->block = NULL;
823 }
824 
826 {
827 #define COPY(a) bak->a = src->a
828  COPY(sc.edge_emu_buffer);
829  COPY(me.scratchpad);
830  COPY(me.temp);
831  COPY(sc.rd_scratchpad);
832  COPY(sc.b_scratchpad);
833  COPY(sc.obmc_scratchpad);
834  COPY(me.map);
835  COPY(me.score_map);
836  COPY(blocks);
837  COPY(block);
838  COPY(start_mb_y);
839  COPY(end_mb_y);
840  COPY(me.map_generation);
841  COPY(pb);
842  COPY(dct_error_sum);
843  COPY(dct_count[0]);
844  COPY(dct_count[1]);
845  COPY(ac_val_base);
846  COPY(ac_val[0]);
847  COPY(ac_val[1]);
848  COPY(ac_val[2]);
849 #undef COPY
850 }
851 
853 {
854  MpegEncContext bak;
855  int i, ret;
856  // FIXME copy only needed parts
857  // START_TIMER
858  backup_duplicate_context(&bak, dst);
859  memcpy(dst, src, sizeof(MpegEncContext));
860  backup_duplicate_context(dst, &bak);
861  for (i = 0; i < 12; i++) {
862  dst->pblocks[i] = &dst->block[i];
863  }
864  if (dst->avctx->codec_tag == AV_RL32("VCR2")) {
865  // exchange uv
866  FFSWAP(void *, dst->pblocks[4], dst->pblocks[5]);
867  }
868  if (!dst->sc.edge_emu_buffer &&
869  (ret = ff_mpeg_framesize_alloc(dst->avctx, &dst->me,
870  &dst->sc, dst->linesize)) < 0) {
871  av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
872  "scratch buffers.\n");
873  return ret;
874  }
875  // STOP_TIMER("update_duplicate_context")
876  // about 10k cycles / 0.01 sec for 1000frames on 1ghz with 2 threads
877  return 0;
878 }
879 
881  const AVCodecContext *src)
882 {
883  int i, ret;
884  MpegEncContext *s = dst->priv_data, *s1 = src->priv_data;
885 
886  if (dst == src)
887  return 0;
888 
889  av_assert0(s != s1);
890 
891  // FIXME can parameters change on I-frames?
892  // in that case dst may need a reinit
893  if (!s->context_initialized) {
894  int err;
895  memcpy(s, s1, sizeof(MpegEncContext));
896 
897  s->avctx = dst;
898  s->bitstream_buffer = NULL;
900 
901  if (s1->context_initialized){
902 // s->picture_range_start += MAX_PICTURE_COUNT;
903 // s->picture_range_end += MAX_PICTURE_COUNT;
904  ff_mpv_idct_init(s);
905  if((err = ff_mpv_common_init(s)) < 0){
906  memset(s, 0, sizeof(MpegEncContext));
907  s->avctx = dst;
908  return err;
909  }
910  }
911  }
912 
913  if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
914  s->context_reinit = 0;
915  s->height = s1->height;
916  s->width = s1->width;
917  if ((ret = ff_mpv_common_frame_size_change(s)) < 0)
918  return ret;
919  }
920 
921  s->avctx->coded_height = s1->avctx->coded_height;
922  s->avctx->coded_width = s1->avctx->coded_width;
923  s->avctx->width = s1->avctx->width;
924  s->avctx->height = s1->avctx->height;
925 
926  s->coded_picture_number = s1->coded_picture_number;
927  s->picture_number = s1->picture_number;
928 
929  av_assert0(!s->picture || s->picture != s1->picture);
930  if(s->picture)
931  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
932  ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
933  if (s1->picture[i].f->buf[0] &&
934  (ret = ff_mpeg_ref_picture(s->avctx, &s->picture[i], &s1->picture[i])) < 0)
935  return ret;
936  }
937 
938 #define UPDATE_PICTURE(pic)\
939 do {\
940  ff_mpeg_unref_picture(s->avctx, &s->pic);\
941  if (s1->pic.f && s1->pic.f->buf[0])\
942  ret = ff_mpeg_ref_picture(s->avctx, &s->pic, &s1->pic);\
943  else\
944  ret = update_picture_tables(&s->pic, &s1->pic);\
945  if (ret < 0)\
946  return ret;\
947 } while (0)
948 
949  UPDATE_PICTURE(current_picture);
950  UPDATE_PICTURE(last_picture);
951  UPDATE_PICTURE(next_picture);
952 
953 #define REBASE_PICTURE(pic, new_ctx, old_ctx) \
954  ((pic && pic >= old_ctx->picture && \
955  pic < old_ctx->picture + MAX_PICTURE_COUNT) ? \
956  &new_ctx->picture[pic - old_ctx->picture] : NULL)
957 
958  s->last_picture_ptr = REBASE_PICTURE(s1->last_picture_ptr, s, s1);
959  s->current_picture_ptr = REBASE_PICTURE(s1->current_picture_ptr, s, s1);
960  s->next_picture_ptr = REBASE_PICTURE(s1->next_picture_ptr, s, s1);
961 
962  // Error/bug resilience
963  s->next_p_frame_damaged = s1->next_p_frame_damaged;
964  s->workaround_bugs = s1->workaround_bugs;
965  s->padding_bug_score = s1->padding_bug_score;
966 
967  // MPEG4 timing info
968  memcpy(&s->last_time_base, &s1->last_time_base,
969  (char *) &s1->pb_field_time + sizeof(s1->pb_field_time) -
970  (char *) &s1->last_time_base);
971 
972  // B-frame info
973  s->max_b_frames = s1->max_b_frames;
974  s->low_delay = s1->low_delay;
975  s->droppable = s1->droppable;
976 
977  // DivX handling (doesn't work)
978  s->divx_packed = s1->divx_packed;
979 
980  if (s1->bitstream_buffer) {
981  if (s1->bitstream_buffer_size +
985  s1->allocated_bitstream_buffer_size);
986  if (!s->bitstream_buffer) {
987  s->bitstream_buffer_size = 0;
988  return AVERROR(ENOMEM);
989  }
990  }
991  s->bitstream_buffer_size = s1->bitstream_buffer_size;
992  memcpy(s->bitstream_buffer, s1->bitstream_buffer,
993  s1->bitstream_buffer_size);
994  memset(s->bitstream_buffer + s->bitstream_buffer_size, 0,
996  }
997 
998  // linesize dependend scratch buffer allocation
999  if (!s->sc.edge_emu_buffer)
1000  if (s1->linesize) {
1001  if (ff_mpeg_framesize_alloc(s->avctx, &s->me,
1002  &s->sc, s1->linesize) < 0) {
1003  av_log(s->avctx, AV_LOG_ERROR, "Failed to allocate context "
1004  "scratch buffers.\n");
1005  return AVERROR(ENOMEM);
1006  }
1007  } else {
1008  av_log(s->avctx, AV_LOG_ERROR, "Context scratch buffers could not "
1009  "be allocated due to unknown size.\n");
1010  }
1011 
1012  // MPEG2/interlacing info
1013  memcpy(&s->progressive_sequence, &s1->progressive_sequence,
1014  (char *) &s1->rtp_mode - (char *) &s1->progressive_sequence);
1015 
1016  if (!s1->first_field) {
1017  s->last_pict_type = s1->pict_type;
1018  if (s1->current_picture_ptr)
1019  s->last_lambda_for[s1->pict_type] = s1->current_picture_ptr->f->quality;
1020  }
1021 
1022  return 0;
1023 }
1024 
1025 /**
1026  * Set the given MpegEncContext to common defaults
1027  * (same for encoding and decoding).
1028  * The changed fields will not depend upon the
1029  * prior state of the MpegEncContext.
1030  */
1032 {
1033  s->y_dc_scale_table =
1036  s->progressive_frame = 1;
1037  s->progressive_sequence = 1;
1039 
1040  s->coded_picture_number = 0;
1041  s->picture_number = 0;
1042 
1043  s->f_code = 1;
1044  s->b_code = 1;
1045 
1046  s->slice_context_count = 1;
1047 }
1048 
1049 /**
1050  * Set the given MpegEncContext to defaults for decoding.
1051  * the changed fields will not depend upon
1052  * the prior state of the MpegEncContext.
1053  */
1055 {
1057 }
1058 
1060 {
1061  s->avctx = avctx;
1062  s->width = avctx->coded_width;
1063  s->height = avctx->coded_height;
1064  s->codec_id = avctx->codec->id;
1065  s->workaround_bugs = avctx->workaround_bugs;
1066 
1067  /* convert fourcc to upper case */
1068  s->codec_tag = avpriv_toupper4(avctx->codec_tag);
1069 }
1070 
1072 {
1073  ERContext *er = &s->er;
1074  int mb_array_size = s->mb_height * s->mb_stride;
1075  int i;
1076 
1077  er->avctx = s->avctx;
1078 
1079  er->mb_index2xy = s->mb_index2xy;
1080  er->mb_num = s->mb_num;
1081  er->mb_width = s->mb_width;
1082  er->mb_height = s->mb_height;
1083  er->mb_stride = s->mb_stride;
1084  er->b8_stride = s->b8_stride;
1085 
1087  er->error_status_table = av_mallocz(mb_array_size);
1088  if (!er->er_temp_buffer || !er->error_status_table)
1089  goto fail;
1090 
1091  er->mbskip_table = s->mbskip_table;
1092  er->mbintra_table = s->mbintra_table;
1093 
1094  for (i = 0; i < FF_ARRAY_ELEMS(s->dc_val); i++)
1095  er->dc_val[i] = s->dc_val[i];
1096 
1098  er->opaque = s;
1099 
1100  return 0;
1101 fail:
1102  av_freep(&er->er_temp_buffer);
1104  return AVERROR(ENOMEM);
1105 }
1106 
1107 /**
1108  * Initialize and allocates MpegEncContext fields dependent on the resolution.
1109  */
1111 {
1112  int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
1113 
1114  s->mb_width = (s->width + 15) / 16;
1115  s->mb_stride = s->mb_width + 1;
1116  s->b8_stride = s->mb_width * 2 + 1;
1117  mb_array_size = s->mb_height * s->mb_stride;
1118  mv_table_size = (s->mb_height + 2) * s->mb_stride + 1;
1119 
1120  /* set default edge pos, will be overridden
1121  * in decode_header if needed */
1122  s->h_edge_pos = s->mb_width * 16;
1123  s->v_edge_pos = s->mb_height * 16;
1124 
1125  s->mb_num = s->mb_width * s->mb_height;
1126 
1127  s->block_wrap[0] =
1128  s->block_wrap[1] =
1129  s->block_wrap[2] =
1130  s->block_wrap[3] = s->b8_stride;
1131  s->block_wrap[4] =
1132  s->block_wrap[5] = s->mb_stride;
1133 
1134  y_size = s->b8_stride * (2 * s->mb_height + 1);
1135  c_size = s->mb_stride * (s->mb_height + 1);
1136  yc_size = y_size + 2 * c_size;
1137 
1138  if (s->mb_height & 1)
1139  yc_size += 2*s->b8_stride + 2*s->mb_stride;
1140 
1141  FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_index2xy, (s->mb_num + 1) * sizeof(int), fail); // error ressilience code looks cleaner with this
1142  for (y = 0; y < s->mb_height; y++)
1143  for (x = 0; x < s->mb_width; x++)
1144  s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
1145 
1146  s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
1147 
1148  if (s->encoding) {
1149  /* Allocate MV tables */
1150  FF_ALLOCZ_OR_GOTO(s->avctx, s->p_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1151  FF_ALLOCZ_OR_GOTO(s->avctx, s->b_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1152  FF_ALLOCZ_OR_GOTO(s->avctx, s->b_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1153  FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_forw_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1154  FF_ALLOCZ_OR_GOTO(s->avctx, s->b_bidir_back_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1155  FF_ALLOCZ_OR_GOTO(s->avctx, s->b_direct_mv_table_base, mv_table_size * 2 * sizeof(int16_t), fail)
1156  s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1;
1162 
1163  /* Allocate MB type table */
1164  FF_ALLOCZ_OR_GOTO(s->avctx, s->mb_type, mb_array_size * sizeof(uint16_t), fail) // needed for encoding
1165 
1166  FF_ALLOCZ_OR_GOTO(s->avctx, s->lambda_table, mb_array_size * sizeof(int), fail)
1167 
1169  mb_array_size * sizeof(float), fail);
1171  mb_array_size * sizeof(float), fail);
1172 
1173  }
1174 
1175  if (s->codec_id == AV_CODEC_ID_MPEG4 ||
1177  /* interlaced direct mode decoding tables */
1178  for (i = 0; i < 2; i++) {
1179  int j, k;
1180  for (j = 0; j < 2; j++) {
1181  for (k = 0; k < 2; k++) {
1183  s->b_field_mv_table_base[i][j][k],
1184  mv_table_size * 2 * sizeof(int16_t),
1185  fail);
1186  s->b_field_mv_table[i][j][k] = s->b_field_mv_table_base[i][j][k] +
1187  s->mb_stride + 1;
1188  }
1189  FF_ALLOCZ_OR_GOTO(s->avctx, s->b_field_select_table [i][j], mb_array_size * 2 * sizeof(uint8_t), fail)
1190  FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_mv_table_base[i][j], mv_table_size * 2 * sizeof(int16_t), fail)
1191  s->p_field_mv_table[i][j] = s->p_field_mv_table_base[i][j] + s->mb_stride + 1;
1192  }
1193  FF_ALLOCZ_OR_GOTO(s->avctx, s->p_field_select_table[i], mb_array_size * 2 * sizeof(uint8_t), fail)
1194  }
1195  }
1196  if (s->out_format == FMT_H263) {
1197  /* cbp values */
1198  FF_ALLOCZ_OR_GOTO(s->avctx, s->coded_block_base, y_size + (s->mb_height&1)*2*s->b8_stride, fail);
1199  s->coded_block = s->coded_block_base + s->b8_stride + 1;
1200 
1201  /* cbp, ac_pred, pred_dir */
1202  FF_ALLOCZ_OR_GOTO(s->avctx, s->cbp_table , mb_array_size * sizeof(uint8_t), fail);
1203  FF_ALLOCZ_OR_GOTO(s->avctx, s->pred_dir_table, 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
1209  FF_ALLOCZ_OR_GOTO(s->avctx, s->dc_val_base, yc_size * sizeof(int16_t), fail);
1210  s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
1211  s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
1212  s->dc_val[2] = s->dc_val[1] + c_size;
1213  for (i = 0; i < yc_size; i++)
1214  s->dc_val_base[i] = 1024;
1215  }
1216 
1217  /* which mb is a intra block */
1218  FF_ALLOCZ_OR_GOTO(s->avctx, s->mbintra_table, mb_array_size, fail);
1219  memset(s->mbintra_table, 1, mb_array_size);
1220 
1221  /* init macroblock skip table */
1222  FF_ALLOCZ_OR_GOTO(s->avctx, s->mbskip_table, mb_array_size + 2, fail);
1223  // Note the + 1 is for a quicker mpeg4 slice_end detection
1224 
1225  return init_er(s);
1226 fail:
1227  return AVERROR(ENOMEM);
1228 }
1229 
1231 {
1232  int i, j, k;
1233 
1234  memset(&s->next_picture, 0, sizeof(s->next_picture));
1235  memset(&s->last_picture, 0, sizeof(s->last_picture));
1236  memset(&s->current_picture, 0, sizeof(s->current_picture));
1237  memset(&s->new_picture, 0, sizeof(s->new_picture));
1238 
1239  memset(s->thread_context, 0, sizeof(s->thread_context));
1240 
1241  s->me.map = NULL;
1242  s->me.score_map = NULL;
1243  s->dct_error_sum = NULL;
1244  s->block = NULL;
1245  s->blocks = NULL;
1246  memset(s->pblocks, 0, sizeof(s->pblocks));
1247  s->ac_val_base = NULL;
1248  s->ac_val[0] =
1249  s->ac_val[1] =
1250  s->ac_val[2] =NULL;
1251  s->sc.edge_emu_buffer = NULL;
1252  s->me.scratchpad = NULL;
1253  s->me.temp =
1254  s->sc.rd_scratchpad =
1255  s->sc.b_scratchpad =
1256  s->sc.obmc_scratchpad = NULL;
1257 
1258  s->parse_context.buffer = NULL;
1259  s->parse_context.buffer_size = 0;
1260  s->bitstream_buffer = NULL;
1262  s->picture = NULL;
1263  s->mb_type = NULL;
1264  s->p_mv_table_base = NULL;
1270  s->p_mv_table = NULL;
1271  s->b_forw_mv_table = NULL;
1272  s->b_back_mv_table = NULL;
1275  s->b_direct_mv_table = NULL;
1276  for (i = 0; i < 2; i++) {
1277  for (j = 0; j < 2; j++) {
1278  for (k = 0; k < 2; k++) {
1279  s->b_field_mv_table_base[i][j][k] = NULL;
1280  s->b_field_mv_table[i][j][k] = NULL;
1281  }
1282  s->b_field_select_table[i][j] = NULL;
1283  s->p_field_mv_table_base[i][j] = NULL;
1284  s->p_field_mv_table[i][j] = NULL;
1285  }
1286  s->p_field_select_table[i] = NULL;
1287  }
1288 
1289  s->dc_val_base = NULL;
1290  s->coded_block_base = NULL;
1291  s->mbintra_table = NULL;
1292  s->cbp_table = NULL;
1293  s->pred_dir_table = NULL;
1294 
1295  s->mbskip_table = NULL;
1296 
1297  s->er.error_status_table = NULL;
1298  s->er.er_temp_buffer = NULL;
1299  s->mb_index2xy = NULL;
1300  s->lambda_table = NULL;
1301 
1302  s->cplx_tab = NULL;
1303  s->bits_tab = NULL;
1304 }
1305 
1306 /**
1307  * init common structure for both encoder and decoder.
1308  * this assumes that some variables like width/height are already set
1309  */
1311 {
1312  int i;
1313  int nb_slices = (HAVE_THREADS &&
1315  s->avctx->thread_count : 1;
1316 
1317  clear_context(s);
1318 
1319  if (s->encoding && s->avctx->slices)
1320  nb_slices = s->avctx->slices;
1321 
1323  s->mb_height = (s->height + 31) / 32 * 2;
1324  else
1325  s->mb_height = (s->height + 15) / 16;
1326 
1327  if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
1329  "decoding to AV_PIX_FMT_NONE is not supported.\n");
1330  return -1;
1331  }
1332 
1333  if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
1334  int max_slices;
1335  if (s->mb_height)
1336  max_slices = FFMIN(MAX_THREADS, s->mb_height);
1337  else
1338  max_slices = MAX_THREADS;
1339  av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
1340  " reducing to %d\n", nb_slices, max_slices);
1341  nb_slices = max_slices;
1342  }
1343 
1344  if ((s->width || s->height) &&
1345  av_image_check_size(s->width, s->height, 0, s->avctx))
1346  return -1;
1347 
1348  dct_init(s);
1349 
1350  /* set chroma shifts */
1352  &s->chroma_x_shift,
1353  &s->chroma_y_shift);
1354 
1355 
1357  MAX_PICTURE_COUNT * sizeof(Picture), fail);
1358  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1359  s->picture[i].f = av_frame_alloc();
1360  if (!s->picture[i].f)
1361  goto fail;
1362  }
1363  s->next_picture.f = av_frame_alloc();
1364  if (!s->next_picture.f)
1365  goto fail;
1366  s->last_picture.f = av_frame_alloc();
1367  if (!s->last_picture.f)
1368  goto fail;
1370  if (!s->current_picture.f)
1371  goto fail;
1372  s->new_picture.f = av_frame_alloc();
1373  if (!s->new_picture.f)
1374  goto fail;
1375 
1376  if (init_context_frame(s))
1377  goto fail;
1378 
1379  s->parse_context.state = -1;
1380 
1381  s->context_initialized = 1;
1382  memset(s->thread_context, 0, sizeof(s->thread_context));
1383  s->thread_context[0] = s;
1384 
1385 // if (s->width && s->height) {
1386  if (nb_slices > 1) {
1387  for (i = 0; i < nb_slices; i++) {
1388  if (i) {
1389  s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));
1390  if (!s->thread_context[i])
1391  goto fail;
1392  }
1393  if (init_duplicate_context(s->thread_context[i]) < 0)
1394  goto fail;
1395  s->thread_context[i]->start_mb_y =
1396  (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1397  s->thread_context[i]->end_mb_y =
1398  (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1399  }
1400  } else {
1401  if (init_duplicate_context(s) < 0)
1402  goto fail;
1403  s->start_mb_y = 0;
1404  s->end_mb_y = s->mb_height;
1405  }
1406  s->slice_context_count = nb_slices;
1407 // }
1408 
1409  return 0;
1410  fail:
1411  ff_mpv_common_end(s);
1412  return -1;
1413 }
1414 
1415 /**
1416  * Frees and resets MpegEncContext fields depending on the resolution.
1417  * Is used during resolution changes to avoid a full reinitialization of the
1418  * codec.
1419  */
1421 {
1422  int i, j, k;
1423 
1424  av_freep(&s->mb_type);
1431  s->p_mv_table = NULL;
1432  s->b_forw_mv_table = NULL;
1433  s->b_back_mv_table = NULL;
1436  s->b_direct_mv_table = NULL;
1437  for (i = 0; i < 2; i++) {
1438  for (j = 0; j < 2; j++) {
1439  for (k = 0; k < 2; k++) {
1440  av_freep(&s->b_field_mv_table_base[i][j][k]);
1441  s->b_field_mv_table[i][j][k] = NULL;
1442  }
1443  av_freep(&s->b_field_select_table[i][j]);
1444  av_freep(&s->p_field_mv_table_base[i][j]);
1445  s->p_field_mv_table[i][j] = NULL;
1446  }
1448  }
1449 
1450  av_freep(&s->dc_val_base);
1452  av_freep(&s->mbintra_table);
1453  av_freep(&s->cbp_table);
1454  av_freep(&s->pred_dir_table);
1455 
1456  av_freep(&s->mbskip_table);
1457 
1459  av_freep(&s->er.er_temp_buffer);
1460  av_freep(&s->mb_index2xy);
1461  av_freep(&s->lambda_table);
1462 
1463  av_freep(&s->cplx_tab);
1464  av_freep(&s->bits_tab);
1465 
1466  s->linesize = s->uvlinesize = 0;
1467 }
1468 
1470 {
1471  int i, err = 0;
1472 
1473  if (!s->context_initialized)
1474  return AVERROR(EINVAL);
1475 
1476  if (s->slice_context_count > 1) {
1477  for (i = 0; i < s->slice_context_count; i++) {
1479  }
1480  for (i = 1; i < s->slice_context_count; i++) {
1481  av_freep(&s->thread_context[i]);
1482  }
1483  } else
1485 
1486  free_context_frame(s);
1487 
1488  if (s->picture)
1489  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1490  s->picture[i].needs_realloc = 1;
1491  }
1492 
1493  s->last_picture_ptr =
1494  s->next_picture_ptr =
1496 
1497  // init
1499  s->mb_height = (s->height + 31) / 32 * 2;
1500  else
1501  s->mb_height = (s->height + 15) / 16;
1502 
1503  if ((s->width || s->height) &&
1504  (err = av_image_check_size(s->width, s->height, 0, s->avctx)) < 0)
1505  goto fail;
1506 
1507  if ((err = init_context_frame(s)))
1508  goto fail;
1509 
1510  memset(s->thread_context, 0, sizeof(s->thread_context));
1511  s->thread_context[0] = s;
1512 
1513  if (s->width && s->height) {
1514  int nb_slices = s->slice_context_count;
1515  if (nb_slices > 1) {
1516  for (i = 0; i < nb_slices; i++) {
1517  if (i) {
1518  s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));
1519  if (!s->thread_context[i]) {
1520  err = AVERROR(ENOMEM);
1521  goto fail;
1522  }
1523  }
1524  if ((err = init_duplicate_context(s->thread_context[i])) < 0)
1525  goto fail;
1526  s->thread_context[i]->start_mb_y =
1527  (s->mb_height * (i) + nb_slices / 2) / nb_slices;
1528  s->thread_context[i]->end_mb_y =
1529  (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
1530  }
1531  } else {
1532  err = init_duplicate_context(s);
1533  if (err < 0)
1534  goto fail;
1535  s->start_mb_y = 0;
1536  s->end_mb_y = s->mb_height;
1537  }
1538  s->slice_context_count = nb_slices;
1539  }
1540 
1541  return 0;
1542  fail:
1543  ff_mpv_common_end(s);
1544  return err;
1545 }
1546 
1547 /* init common structure for both encoder and decoder */
1549 {
1550  int i;
1551 
1552  if (s->slice_context_count > 1) {
1553  for (i = 0; i < s->slice_context_count; i++) {
1555  }
1556  for (i = 1; i < s->slice_context_count; i++) {
1557  av_freep(&s->thread_context[i]);
1558  }
1559  s->slice_context_count = 1;
1560  } else free_duplicate_context(s);
1561 
1563  s->parse_context.buffer_size = 0;
1564 
1567 
1568  if (s->picture) {
1569  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1571  ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1572  av_frame_free(&s->picture[i].f);
1573  }
1574  }
1575  av_freep(&s->picture);
1588 
1589  free_context_frame(s);
1590 
1591  s->context_initialized = 0;
1592  s->last_picture_ptr =
1593  s->next_picture_ptr =
1595  s->linesize = s->uvlinesize = 0;
1596 }
1597 
1598 static void release_unused_pictures(AVCodecContext *avctx, Picture *picture)
1599 {
1600  int i;
1601 
1602  /* release non reference frames */
1603  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1604  if (!picture[i].reference)
1605  ff_mpeg_unref_picture(avctx, &picture[i]);
1606  }
1607 }
1608 
1609 static inline int pic_is_unused(Picture *pic)
1610 {
1611  if (!pic->f->buf[0])
1612  return 1;
1613  if (pic->needs_realloc && !(pic->reference & DELAYED_PIC_REF))
1614  return 1;
1615  return 0;
1616 }
1617 
1618 static int find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
1619 {
1620  int i;
1621 
1622  if (shared) {
1623  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1624  if (!picture[i].f->buf[0])
1625  return i;
1626  }
1627  } else {
1628  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1629  if (pic_is_unused(&picture[i]))
1630  return i;
1631  }
1632  }
1633 
1634  av_log(avctx, AV_LOG_FATAL,
1635  "Internal error, picture buffer overflow\n");
1636  /* We could return -1, but the codec would crash trying to draw into a
1637  * non-existing frame anyway. This is safer than waiting for a random crash.
1638  * Also the return of this is never useful, an encoder must only allocate
1639  * as much as allowed in the specification. This has no relationship to how
1640  * much libavcodec could allocate (and MAX_PICTURE_COUNT is always large
1641  * enough for such valid streams).
1642  * Plus, a decoder has to check stream validity and remove frames if too
1643  * many reference frames are around. Waiting for "OOM" is not correct at
1644  * all. Similarly, missing reference frames have to be replaced by
1645  * interpolated/MC frames, anything else is a bug in the codec ...
1646  */
1647  abort();
1648  return -1;
1649 }
1650 
1651 int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
1652 {
1653  int ret = find_unused_picture(avctx, picture, shared);
1654 
1655  if (ret >= 0 && ret < MAX_PICTURE_COUNT) {
1656  if (picture[ret].needs_realloc) {
1657  picture[ret].needs_realloc = 0;
1658  ff_free_picture_tables(&picture[ret]);
1659  ff_mpeg_unref_picture(avctx, &picture[ret]);
1660  }
1661  }
1662  return ret;
1663 }
1664 
1665 static void gray_frame(AVFrame *frame)
1666 {
1667  int i, h_chroma_shift, v_chroma_shift;
1668 
1669  av_pix_fmt_get_chroma_sub_sample(frame->format, &h_chroma_shift, &v_chroma_shift);
1670 
1671  for(i=0; i<frame->height; i++)
1672  memset(frame->data[0] + frame->linesize[0]*i, 0x80, frame->width);
1673  for(i=0; i<FF_CEIL_RSHIFT(frame->height, v_chroma_shift); i++) {
1674  memset(frame->data[1] + frame->linesize[1]*i,
1675  0x80, FF_CEIL_RSHIFT(frame->width, h_chroma_shift));
1676  memset(frame->data[2] + frame->linesize[2]*i,
1677  0x80, FF_CEIL_RSHIFT(frame->width, h_chroma_shift));
1678  }
1679 }
1680 
1681 /**
1682  * generic function called after decoding
1683  * the header and before a frame is decoded.
1684  */
1686 {
1687  int i, ret;
1688  Picture *pic;
1689  s->mb_skipped = 0;
1690 
1691  if (!ff_thread_can_start_frame(avctx)) {
1692  av_log(avctx, AV_LOG_ERROR, "Attempt to start a frame outside SETUP state\n");
1693  return -1;
1694  }
1695 
1696  /* mark & release old frames */
1697  if (s->pict_type != AV_PICTURE_TYPE_B && s->last_picture_ptr &&
1699  s->last_picture_ptr->f->buf[0]) {
1701  }
1702 
1703  /* release forgotten pictures */
1704  /* if (mpeg124/h263) */
1705  for (i = 0; i < MAX_PICTURE_COUNT; i++) {
1706  if (&s->picture[i] != s->last_picture_ptr &&
1707  &s->picture[i] != s->next_picture_ptr &&
1708  s->picture[i].reference && !s->picture[i].needs_realloc) {
1709  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1710  av_log(avctx, AV_LOG_ERROR,
1711  "releasing zombie picture\n");
1712  ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
1713  }
1714  }
1715 
1717 
1719 
1720  if (s->current_picture_ptr && !s->current_picture_ptr->f->buf[0]) {
1721  // we already have a unused image
1722  // (maybe it was set before reading the header)
1723  pic = s->current_picture_ptr;
1724  } else {
1725  i = ff_find_unused_picture(s->avctx, s->picture, 0);
1726  if (i < 0) {
1727  av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1728  return i;
1729  }
1730  pic = &s->picture[i];
1731  }
1732 
1733  pic->reference = 0;
1734  if (!s->droppable) {
1735  if (s->pict_type != AV_PICTURE_TYPE_B)
1736  pic->reference = 3;
1737  }
1738 
1740 
1741  if (alloc_picture(s, pic, 0) < 0)
1742  return -1;
1743 
1744  s->current_picture_ptr = pic;
1745  // FIXME use only the vars from current_pic
1747  if (s->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
1749  if (s->picture_structure != PICT_FRAME)
1752  }
1756 
1758  // if (s->avctx->flags && CODEC_FLAG_QSCALE)
1759  // s->current_picture_ptr->quality = s->new_picture_ptr->quality;
1761 
1762  if ((ret = ff_mpeg_ref_picture(s->avctx, &s->current_picture,
1763  s->current_picture_ptr)) < 0)
1764  return ret;
1765 
1766  if (s->pict_type != AV_PICTURE_TYPE_B) {
1768  if (!s->droppable)
1770  }
1771  ff_dlog(s->avctx, "L%p N%p C%p L%p N%p C%p type:%d drop:%d\n",
1773  s->last_picture_ptr ? s->last_picture_ptr->f->data[0] : NULL,
1774  s->next_picture_ptr ? s->next_picture_ptr->f->data[0] : NULL,
1776  s->pict_type, s->droppable);
1777 
1778  if ((!s->last_picture_ptr || !s->last_picture_ptr->f->buf[0]) &&
1779  (s->pict_type != AV_PICTURE_TYPE_I ||
1780  s->picture_structure != PICT_FRAME)) {
1781  int h_chroma_shift, v_chroma_shift;
1783  &h_chroma_shift, &v_chroma_shift);
1785  av_log(avctx, AV_LOG_DEBUG,
1786  "allocating dummy last picture for B frame\n");
1787  else if (s->pict_type != AV_PICTURE_TYPE_I)
1788  av_log(avctx, AV_LOG_ERROR,
1789  "warning: first frame is no keyframe\n");
1790  else if (s->picture_structure != PICT_FRAME)
1791  av_log(avctx, AV_LOG_DEBUG,
1792  "allocate dummy last picture for field based first keyframe\n");
1793 
1794  /* Allocate a dummy frame */
1795  i = ff_find_unused_picture(s->avctx, s->picture, 0);
1796  if (i < 0) {
1797  av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1798  return i;
1799  }
1800  s->last_picture_ptr = &s->picture[i];
1801 
1802  s->last_picture_ptr->reference = 3;
1803  s->last_picture_ptr->f->key_frame = 0;
1805 
1806  if (alloc_picture(s, s->last_picture_ptr, 0) < 0) {
1807  s->last_picture_ptr = NULL;
1808  return -1;
1809  }
1810 
1811  if (!avctx->hwaccel && !(avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)) {
1812  for(i=0; i<avctx->height; i++)
1813  memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i,
1814  0x80, avctx->width);
1815  if (s->last_picture_ptr->f->data[2]) {
1816  for(i=0; i<FF_CEIL_RSHIFT(avctx->height, v_chroma_shift); i++) {
1817  memset(s->last_picture_ptr->f->data[1] + s->last_picture_ptr->f->linesize[1]*i,
1818  0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
1819  memset(s->last_picture_ptr->f->data[2] + s->last_picture_ptr->f->linesize[2]*i,
1820  0x80, FF_CEIL_RSHIFT(avctx->width, h_chroma_shift));
1821  }
1822  }
1823 
1825  for(i=0; i<avctx->height; i++)
1826  memset(s->last_picture_ptr->f->data[0] + s->last_picture_ptr->f->linesize[0]*i, 16, avctx->width);
1827  }
1828  }
1829 
1830  ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 0);
1831  ff_thread_report_progress(&s->last_picture_ptr->tf, INT_MAX, 1);
1832  }
1833  if ((!s->next_picture_ptr || !s->next_picture_ptr->f->buf[0]) &&
1834  s->pict_type == AV_PICTURE_TYPE_B) {
1835  /* Allocate a dummy frame */
1836  i = ff_find_unused_picture(s->avctx, s->picture, 0);
1837  if (i < 0) {
1838  av_log(s->avctx, AV_LOG_ERROR, "no frame buffer available\n");
1839  return i;
1840  }
1841  s->next_picture_ptr = &s->picture[i];
1842 
1843  s->next_picture_ptr->reference = 3;
1844  s->next_picture_ptr->f->key_frame = 0;
1846 
1847  if (alloc_picture(s, s->next_picture_ptr, 0) < 0) {
1848  s->next_picture_ptr = NULL;
1849  return -1;
1850  }
1851  ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 0);
1852  ff_thread_report_progress(&s->next_picture_ptr->tf, INT_MAX, 1);
1853  }
1854 
1855 #if 0 // BUFREF-FIXME
1856  memset(s->last_picture.f->data, 0, sizeof(s->last_picture.f->data));
1857  memset(s->next_picture.f->data, 0, sizeof(s->next_picture.f->data));
1858 #endif
1859  if (s->last_picture_ptr) {
1861  if (s->last_picture_ptr->f->buf[0] &&
1862  (ret = ff_mpeg_ref_picture(s->avctx, &s->last_picture,
1863  s->last_picture_ptr)) < 0)
1864  return ret;
1865  }
1866  if (s->next_picture_ptr) {
1868  if (s->next_picture_ptr->f->buf[0] &&
1869  (ret = ff_mpeg_ref_picture(s->avctx, &s->next_picture,
1870  s->next_picture_ptr)) < 0)
1871  return ret;
1872  }
1873 
1875  s->last_picture_ptr->f->buf[0]));
1876 
1877  if (s->picture_structure!= PICT_FRAME) {
1878  int i;
1879  for (i = 0; i < 4; i++) {
1881  s->current_picture.f->data[i] +=
1882  s->current_picture.f->linesize[i];
1883  }
1884  s->current_picture.f->linesize[i] *= 2;
1885  s->last_picture.f->linesize[i] *= 2;
1886  s->next_picture.f->linesize[i] *= 2;
1887  }
1888  }
1889 
1890  /* set dequantizer, we can't do it during init as
1891  * it might change for mpeg4 and we can't do it in the header
1892  * decode as init is not called for mpeg4 there yet */
1893  if (s->mpeg_quant || s->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
1896  } else if (s->out_format == FMT_H263 || s->out_format == FMT_H261) {
1899  } else {
1902  }
1903 
1904  if (s->avctx->debug & FF_DEBUG_NOMC) {
1906  }
1907 
1908  return 0;
1909 }
1910 
1911 /* called after a frame has been decoded. */
1913 {
1914  emms_c();
1915 
1916  if (s->current_picture.reference)
1918 }
1919 
1920 
1921 #if FF_API_VISMV
1922 static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
1923 {
1924  if(*sx > *ex)
1925  return clip_line(ex, ey, sx, sy, maxx);
1926 
1927  if (*sx < 0) {
1928  if (*ex < 0)
1929  return 1;
1930  *sy = *ey + (*sy - *ey) * (int64_t)*ex / (*ex - *sx);
1931  *sx = 0;
1932  }
1933 
1934  if (*ex > maxx) {
1935  if (*sx > maxx)
1936  return 1;
1937  *ey = *sy + (*ey - *sy) * (int64_t)(maxx - *sx) / (*ex - *sx);
1938  *ex = maxx;
1939  }
1940  return 0;
1941 }
1942 
1943 
1944 /**
1945  * Draw a line from (ex, ey) -> (sx, sy).
1946  * @param w width of the image
1947  * @param h height of the image
1948  * @param stride stride/linesize of the image
1949  * @param color color of the arrow
1950  */
1951 static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey,
1952  int w, int h, int stride, int color)
1953 {
1954  int x, y, fr, f;
1955 
1956  if (clip_line(&sx, &sy, &ex, &ey, w - 1))
1957  return;
1958  if (clip_line(&sy, &sx, &ey, &ex, h - 1))
1959  return;
1960 
1961  sx = av_clip(sx, 0, w - 1);
1962  sy = av_clip(sy, 0, h - 1);
1963  ex = av_clip(ex, 0, w - 1);
1964  ey = av_clip(ey, 0, h - 1);
1965 
1966  buf[sy * stride + sx] += color;
1967 
1968  if (FFABS(ex - sx) > FFABS(ey - sy)) {
1969  if (sx > ex) {
1970  FFSWAP(int, sx, ex);
1971  FFSWAP(int, sy, ey);
1972  }
1973  buf += sx + sy * stride;
1974  ex -= sx;
1975  f = ((ey - sy) << 16) / ex;
1976  for (x = 0; x <= ex; x++) {
1977  y = (x * f) >> 16;
1978  fr = (x * f) & 0xFFFF;
1979  buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
1980  if(fr) buf[(y + 1) * stride + x] += (color * fr ) >> 16;
1981  }
1982  } else {
1983  if (sy > ey) {
1984  FFSWAP(int, sx, ex);
1985  FFSWAP(int, sy, ey);
1986  }
1987  buf += sx + sy * stride;
1988  ey -= sy;
1989  if (ey)
1990  f = ((ex - sx) << 16) / ey;
1991  else
1992  f = 0;
1993  for(y= 0; y <= ey; y++){
1994  x = (y*f) >> 16;
1995  fr = (y*f) & 0xFFFF;
1996  buf[y * stride + x] += (color * (0x10000 - fr)) >> 16;
1997  if(fr) buf[y * stride + x + 1] += (color * fr ) >> 16;
1998  }
1999  }
2000 }
2001 
2002 /**
2003  * Draw an arrow from (ex, ey) -> (sx, sy).
2004  * @param w width of the image
2005  * @param h height of the image
2006  * @param stride stride/linesize of the image
2007  * @param color color of the arrow
2008  */
2009 static void draw_arrow(uint8_t *buf, int sx, int sy, int ex,
2010  int ey, int w, int h, int stride, int color, int tail, int direction)
2011 {
2012  int dx,dy;
2013 
2014  if (direction) {
2015  FFSWAP(int, sx, ex);
2016  FFSWAP(int, sy, ey);
2017  }
2018 
2019  sx = av_clip(sx, -100, w + 100);
2020  sy = av_clip(sy, -100, h + 100);
2021  ex = av_clip(ex, -100, w + 100);
2022  ey = av_clip(ey, -100, h + 100);
2023 
2024  dx = ex - sx;
2025  dy = ey - sy;
2026 
2027  if (dx * dx + dy * dy > 3 * 3) {
2028  int rx = dx + dy;
2029  int ry = -dx + dy;
2030  int length = ff_sqrt((rx * rx + ry * ry) << 8);
2031 
2032  // FIXME subpixel accuracy
2033  rx = ROUNDED_DIV(rx * 3 << 4, length);
2034  ry = ROUNDED_DIV(ry * 3 << 4, length);
2035 
2036  if (tail) {
2037  rx = -rx;
2038  ry = -ry;
2039  }
2040 
2041  draw_line(buf, sx, sy, sx + rx, sy + ry, w, h, stride, color);
2042  draw_line(buf, sx, sy, sx - ry, sy + rx, w, h, stride, color);
2043  }
2044  draw_line(buf, sx, sy, ex, ey, w, h, stride, color);
2045 }
2046 #endif
2047 
2048 static int add_mb(AVMotionVector *mb, uint32_t mb_type,
2049  int dst_x, int dst_y,
2050  int src_x, int src_y,
2051  int direction)
2052 {
2053  mb->w = IS_8X8(mb_type) || IS_8X16(mb_type) ? 8 : 16;
2054  mb->h = IS_8X8(mb_type) || IS_16X8(mb_type) ? 8 : 16;
2055  mb->src_x = src_x;
2056  mb->src_y = src_y;
2057  mb->dst_x = dst_x;
2058  mb->dst_y = dst_y;
2059  mb->source = direction ? 1 : -1;
2060  mb->flags = 0; // XXX: does mb_type contain extra information that could be exported here?
2061  return 1;
2062 }
2063 
2064 /**
2065  * Print debugging info for the given picture.
2066  */
2067 void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_table,
2068  uint32_t *mbtype_table, int8_t *qscale_table, int16_t (*motion_val[2])[2],
2069  int *low_delay,
2070  int mb_width, int mb_height, int mb_stride, int quarter_sample)
2071 {
2072  if ((avctx->flags2 & CODEC_FLAG2_EXPORT_MVS) && mbtype_table && motion_val[0]) {
2073  const int shift = 1 + quarter_sample;
2074  const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
2075  const int mv_stride = (mb_width << mv_sample_log2) +
2076  (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
2077  int mb_x, mb_y, mbcount = 0;
2078 
2079  /* size is width * height * 2 * 4 where 2 is for directions and 4 is
2080  * for the maximum number of MB (4 MB in case of IS_8x8) */
2081  AVMotionVector *mvs = av_malloc_array(mb_width * mb_height, 2 * 4 * sizeof(AVMotionVector));
2082  if (!mvs)
2083  return;
2084 
2085  for (mb_y = 0; mb_y < mb_height; mb_y++) {
2086  for (mb_x = 0; mb_x < mb_width; mb_x++) {
2087  int i, direction, mb_type = mbtype_table[mb_x + mb_y * mb_stride];
2088  for (direction = 0; direction < 2; direction++) {
2089  if (!USES_LIST(mb_type, direction))
2090  continue;
2091  if (IS_8X8(mb_type)) {
2092  for (i = 0; i < 4; i++) {
2093  int sx = mb_x * 16 + 4 + 8 * (i & 1);
2094  int sy = mb_y * 16 + 4 + 8 * (i >> 1);
2095  int xy = (mb_x * 2 + (i & 1) +
2096  (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
2097  int mx = (motion_val[direction][xy][0] >> shift) + sx;
2098  int my = (motion_val[direction][xy][1] >> shift) + sy;
2099  mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, direction);
2100  }
2101  } else if (IS_16X8(mb_type)) {
2102  for (i = 0; i < 2; i++) {
2103  int sx = mb_x * 16 + 8;
2104  int sy = mb_y * 16 + 4 + 8 * i;
2105  int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
2106  int mx = (motion_val[direction][xy][0] >> shift);
2107  int my = (motion_val[direction][xy][1] >> shift);
2108 
2109  if (IS_INTERLACED(mb_type))
2110  my *= 2;
2111 
2112  mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx + sx, my + sy, direction);
2113  }
2114  } else if (IS_8X16(mb_type)) {
2115  for (i = 0; i < 2; i++) {
2116  int sx = mb_x * 16 + 4 + 8 * i;
2117  int sy = mb_y * 16 + 8;
2118  int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
2119  int mx = motion_val[direction][xy][0] >> shift;
2120  int my = motion_val[direction][xy][1] >> shift;
2121 
2122  if (IS_INTERLACED(mb_type))
2123  my *= 2;
2124 
2125  mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx + sx, my + sy, direction);
2126  }
2127  } else {
2128  int sx = mb_x * 16 + 8;
2129  int sy = mb_y * 16 + 8;
2130  int xy = (mb_x + mb_y * mv_stride) << mv_sample_log2;
2131  int mx = (motion_val[direction][xy][0]>>shift) + sx;
2132  int my = (motion_val[direction][xy][1]>>shift) + sy;
2133  mbcount += add_mb(mvs + mbcount, mb_type, sx, sy, mx, my, direction);
2134  }
2135  }
2136  }
2137  }
2138 
2139  if (mbcount) {
2140  AVFrameSideData *sd;
2141 
2142  av_log(avctx, AV_LOG_DEBUG, "Adding %d MVs info to frame %d\n", mbcount, avctx->frame_number);
2144  if (!sd) {
2145  av_freep(&mvs);
2146  return;
2147  }
2148  memcpy(sd->data, mvs, mbcount * sizeof(AVMotionVector));
2149  }
2150 
2151  av_freep(&mvs);
2152  }
2153 
2154  /* TODO: export all the following to make them accessible for users (and filters) */
2155  if (avctx->hwaccel || !mbtype_table
2157  return;
2158 
2159 
2160  if (avctx->debug & (FF_DEBUG_SKIP | FF_DEBUG_QP | FF_DEBUG_MB_TYPE)) {
2161  int x,y;
2162 
2163  av_log(avctx, AV_LOG_DEBUG, "New frame, type: %c\n",
2165  for (y = 0; y < mb_height; y++) {
2166  for (x = 0; x < mb_width; x++) {
2167  if (avctx->debug & FF_DEBUG_SKIP) {
2168  int count = mbskip_table ? mbskip_table[x + y * mb_stride] : 0;
2169  if (count > 9)
2170  count = 9;
2171  av_log(avctx, AV_LOG_DEBUG, "%1d", count);
2172  }
2173  if (avctx->debug & FF_DEBUG_QP) {
2174  av_log(avctx, AV_LOG_DEBUG, "%2d",
2175  qscale_table[x + y * mb_stride]);
2176  }
2177  if (avctx->debug & FF_DEBUG_MB_TYPE) {
2178  int mb_type = mbtype_table[x + y * mb_stride];
2179  // Type & MV direction
2180  if (IS_PCM(mb_type))
2181  av_log(avctx, AV_LOG_DEBUG, "P");
2182  else if (IS_INTRA(mb_type) && IS_ACPRED(mb_type))
2183  av_log(avctx, AV_LOG_DEBUG, "A");
2184  else if (IS_INTRA4x4(mb_type))
2185  av_log(avctx, AV_LOG_DEBUG, "i");
2186  else if (IS_INTRA16x16(mb_type))
2187  av_log(avctx, AV_LOG_DEBUG, "I");
2188  else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type))
2189  av_log(avctx, AV_LOG_DEBUG, "d");
2190  else if (IS_DIRECT(mb_type))
2191  av_log(avctx, AV_LOG_DEBUG, "D");
2192  else if (IS_GMC(mb_type) && IS_SKIP(mb_type))
2193  av_log(avctx, AV_LOG_DEBUG, "g");
2194  else if (IS_GMC(mb_type))
2195  av_log(avctx, AV_LOG_DEBUG, "G");
2196  else if (IS_SKIP(mb_type))
2197  av_log(avctx, AV_LOG_DEBUG, "S");
2198  else if (!USES_LIST(mb_type, 1))
2199  av_log(avctx, AV_LOG_DEBUG, ">");
2200  else if (!USES_LIST(mb_type, 0))
2201  av_log(avctx, AV_LOG_DEBUG, "<");
2202  else {
2203  av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
2204  av_log(avctx, AV_LOG_DEBUG, "X");
2205  }
2206 
2207  // segmentation
2208  if (IS_8X8(mb_type))
2209  av_log(avctx, AV_LOG_DEBUG, "+");
2210  else if (IS_16X8(mb_type))
2211  av_log(avctx, AV_LOG_DEBUG, "-");
2212  else if (IS_8X16(mb_type))
2213  av_log(avctx, AV_LOG_DEBUG, "|");
2214  else if (IS_INTRA(mb_type) || IS_16X16(mb_type))
2215  av_log(avctx, AV_LOG_DEBUG, " ");
2216  else
2217  av_log(avctx, AV_LOG_DEBUG, "?");
2218 
2219 
2220  if (IS_INTERLACED(mb_type))
2221  av_log(avctx, AV_LOG_DEBUG, "=");
2222  else
2223  av_log(avctx, AV_LOG_DEBUG, " ");
2224  }
2225  }
2226  av_log(avctx, AV_LOG_DEBUG, "\n");
2227  }
2228  }
2229 
2230  if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) ||
2231  (avctx->debug_mv)) {
2232  int mb_y;
2233  int i;
2234  int h_chroma_shift, v_chroma_shift, block_height;
2235 #if FF_API_VISMV
2236  const int shift = 1 + quarter_sample;
2237  uint8_t *ptr;
2238  const int width = avctx->width;
2239  const int height = avctx->height;
2240 #endif
2241  const int mv_sample_log2 = avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_SVQ3 ? 2 : 1;
2242  const int mv_stride = (mb_width << mv_sample_log2) +
2243  (avctx->codec->id == AV_CODEC_ID_H264 ? 0 : 1);
2244 
2245  *low_delay = 0; // needed to see the vectors without trashing the buffers
2246 
2247  avcodec_get_chroma_sub_sample(avctx->pix_fmt, &h_chroma_shift, &v_chroma_shift);
2248 
2249  av_frame_make_writable(pict);
2250 
2251  pict->opaque = NULL;
2252 #if FF_API_VISMV
2253  ptr = pict->data[0];
2254 #endif
2255  block_height = 16 >> v_chroma_shift;
2256 
2257  for (mb_y = 0; mb_y < mb_height; mb_y++) {
2258  int mb_x;
2259  for (mb_x = 0; mb_x < mb_width; mb_x++) {
2260  const int mb_index = mb_x + mb_y * mb_stride;
2261 #if FF_API_VISMV
2262  if ((avctx->debug_mv) && motion_val[0]) {
2263  int type;
2264  for (type = 0; type < 3; type++) {
2265  int direction = 0;
2266  switch (type) {
2267  case 0:
2268  if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_P_FOR)) ||
2269  (pict->pict_type!= AV_PICTURE_TYPE_P))
2270  continue;
2271  direction = 0;
2272  break;
2273  case 1:
2274  if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_FOR)) ||
2275  (pict->pict_type!= AV_PICTURE_TYPE_B))
2276  continue;
2277  direction = 0;
2278  break;
2279  case 2:
2280  if ((!(avctx->debug_mv & FF_DEBUG_VIS_MV_B_BACK)) ||
2281  (pict->pict_type!= AV_PICTURE_TYPE_B))
2282  continue;
2283  direction = 1;
2284  break;
2285  }
2286  if (!USES_LIST(mbtype_table[mb_index], direction))
2287  continue;
2288 
2289  if (IS_8X8(mbtype_table[mb_index])) {
2290  int i;
2291  for (i = 0; i < 4; i++) {
2292  int sx = mb_x * 16 + 4 + 8 * (i & 1);
2293  int sy = mb_y * 16 + 4 + 8 * (i >> 1);
2294  int xy = (mb_x * 2 + (i & 1) +
2295  (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
2296  int mx = (motion_val[direction][xy][0] >> shift) + sx;
2297  int my = (motion_val[direction][xy][1] >> shift) + sy;
2298  draw_arrow(ptr, sx, sy, mx, my, width,
2299  height, pict->linesize[0], 100, 0, direction);
2300  }
2301  } else if (IS_16X8(mbtype_table[mb_index])) {
2302  int i;
2303  for (i = 0; i < 2; i++) {
2304  int sx = mb_x * 16 + 8;
2305  int sy = mb_y * 16 + 4 + 8 * i;
2306  int xy = (mb_x * 2 + (mb_y * 2 + i) * mv_stride) << (mv_sample_log2 - 1);
2307  int mx = (motion_val[direction][xy][0] >> shift);
2308  int my = (motion_val[direction][xy][1] >> shift);
2309 
2310  if (IS_INTERLACED(mbtype_table[mb_index]))
2311  my *= 2;
2312 
2313  draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
2314  height, pict->linesize[0], 100, 0, direction);
2315  }
2316  } else if (IS_8X16(mbtype_table[mb_index])) {
2317  int i;
2318  for (i = 0; i < 2; i++) {
2319  int sx = mb_x * 16 + 4 + 8 * i;
2320  int sy = mb_y * 16 + 8;
2321  int xy = (mb_x * 2 + i + mb_y * 2 * mv_stride) << (mv_sample_log2 - 1);
2322  int mx = motion_val[direction][xy][0] >> shift;
2323  int my = motion_val[direction][xy][1] >> shift;
2324 
2325  if (IS_INTERLACED(mbtype_table[mb_index]))
2326  my *= 2;
2327 
2328  draw_arrow(ptr, sx, sy, mx + sx, my + sy, width,
2329  height, pict->linesize[0], 100, 0, direction);
2330  }
2331  } else {
2332  int sx= mb_x * 16 + 8;
2333  int sy= mb_y * 16 + 8;
2334  int xy= (mb_x + mb_y * mv_stride) << mv_sample_log2;
2335  int mx= (motion_val[direction][xy][0]>>shift) + sx;
2336  int my= (motion_val[direction][xy][1]>>shift) + sy;
2337  draw_arrow(ptr, sx, sy, mx, my, width, height, pict->linesize[0], 100, 0, direction);
2338  }
2339  }
2340  }
2341 #endif
2342  if ((avctx->debug & FF_DEBUG_VIS_QP)) {
2343  uint64_t c = (qscale_table[mb_index] * 128 / 31) *
2344  0x0101010101010101ULL;
2345  int y;
2346  for (y = 0; y < block_height; y++) {
2347  *(uint64_t *)(pict->data[1] + 8 * mb_x +
2348  (block_height * mb_y + y) *
2349  pict->linesize[1]) = c;
2350  *(uint64_t *)(pict->data[2] + 8 * mb_x +
2351  (block_height * mb_y + y) *
2352  pict->linesize[2]) = c;
2353  }
2354  }
2355  if ((avctx->debug & FF_DEBUG_VIS_MB_TYPE) &&
2356  motion_val[0]) {
2357  int mb_type = mbtype_table[mb_index];
2358  uint64_t u,v;
2359  int y;
2360 #define COLOR(theta, r) \
2361  u = (int)(128 + r * cos(theta * 3.141592 / 180)); \
2362  v = (int)(128 + r * sin(theta * 3.141592 / 180));
2363 
2364 
2365  u = v = 128;
2366  if (IS_PCM(mb_type)) {
2367  COLOR(120, 48)
2368  } else if ((IS_INTRA(mb_type) && IS_ACPRED(mb_type)) ||
2369  IS_INTRA16x16(mb_type)) {
2370  COLOR(30, 48)
2371  } else if (IS_INTRA4x4(mb_type)) {
2372  COLOR(90, 48)
2373  } else if (IS_DIRECT(mb_type) && IS_SKIP(mb_type)) {
2374  // COLOR(120, 48)
2375  } else if (IS_DIRECT(mb_type)) {
2376  COLOR(150, 48)
2377  } else if (IS_GMC(mb_type) && IS_SKIP(mb_type)) {
2378  COLOR(170, 48)
2379  } else if (IS_GMC(mb_type)) {
2380  COLOR(190, 48)
2381  } else if (IS_SKIP(mb_type)) {
2382  // COLOR(180, 48)
2383  } else if (!USES_LIST(mb_type, 1)) {
2384  COLOR(240, 48)
2385  } else if (!USES_LIST(mb_type, 0)) {
2386  COLOR(0, 48)
2387  } else {
2388  av_assert2(USES_LIST(mb_type, 0) && USES_LIST(mb_type, 1));
2389  COLOR(300,48)
2390  }
2391 
2392  u *= 0x0101010101010101ULL;
2393  v *= 0x0101010101010101ULL;
2394  for (y = 0; y < block_height; y++) {
2395  *(uint64_t *)(pict->data[1] + 8 * mb_x +
2396  (block_height * mb_y + y) * pict->linesize[1]) = u;
2397  *(uint64_t *)(pict->data[2] + 8 * mb_x +
2398  (block_height * mb_y + y) * pict->linesize[2]) = v;
2399  }
2400 
2401  // segmentation
2402  if (IS_8X8(mb_type) || IS_16X8(mb_type)) {
2403  *(uint64_t *)(pict->data[0] + 16 * mb_x + 0 +
2404  (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
2405  *(uint64_t *)(pict->data[0] + 16 * mb_x + 8 +
2406  (16 * mb_y + 8) * pict->linesize[0]) ^= 0x8080808080808080ULL;
2407  }
2408  if (IS_8X8(mb_type) || IS_8X16(mb_type)) {
2409  for (y = 0; y < 16; y++)
2410  pict->data[0][16 * mb_x + 8 + (16 * mb_y + y) *
2411  pict->linesize[0]] ^= 0x80;
2412  }
2413  if (IS_8X8(mb_type) && mv_sample_log2 >= 2) {
2414  int dm = 1 << (mv_sample_log2 - 2);
2415  for (i = 0; i < 4; i++) {
2416  int sx = mb_x * 16 + 8 * (i & 1);
2417  int sy = mb_y * 16 + 8 * (i >> 1);
2418  int xy = (mb_x * 2 + (i & 1) +
2419  (mb_y * 2 + (i >> 1)) * mv_stride) << (mv_sample_log2 - 1);
2420  // FIXME bidir
2421  int32_t *mv = (int32_t *) &motion_val[0][xy];
2422  if (mv[0] != mv[dm] ||
2423  mv[dm * mv_stride] != mv[dm * (mv_stride + 1)])
2424  for (y = 0; y < 8; y++)
2425  pict->data[0][sx + 4 + (sy + y) * pict->linesize[0]] ^= 0x80;
2426  if (mv[0] != mv[dm * mv_stride] || mv[dm] != mv[dm * (mv_stride + 1)])
2427  *(uint64_t *)(pict->data[0] + sx + (sy + 4) *
2428  pict->linesize[0]) ^= 0x8080808080808080ULL;
2429  }
2430  }
2431 
2432  if (IS_INTERLACED(mb_type) &&
2433  avctx->codec->id == AV_CODEC_ID_H264) {
2434  // hmm
2435  }
2436  }
2437  if (mbskip_table)
2438  mbskip_table[mb_index] = 0;
2439  }
2440  }
2441  }
2442 }
2443 
2445 {
2447  p->qscale_table, p->motion_val, &s->low_delay,
2448  s->mb_width, s->mb_height, s->mb_stride, s->quarter_sample);
2449 }
2450 
2452 {
2454  int offset = 2*s->mb_stride + 1;
2455  if(!ref)
2456  return AVERROR(ENOMEM);
2457  av_assert0(ref->size >= offset + s->mb_stride * ((f->height+15)/16));
2458  ref->size -= offset;
2459  ref->data += offset;
2460  return av_frame_set_qp_table(f, ref, s->mb_stride, qp_type);
2461 }
2462 
2464  uint8_t *dest, uint8_t *src,
2465  int field_based, int field_select,
2466  int src_x, int src_y,
2467  int width, int height, ptrdiff_t stride,
2468  int h_edge_pos, int v_edge_pos,
2469  int w, int h, h264_chroma_mc_func *pix_op,
2470  int motion_x, int motion_y)
2471 {
2472  const int lowres = s->avctx->lowres;
2473  const int op_index = FFMIN(lowres, 3);
2474  const int s_mask = (2 << lowres) - 1;
2475  int emu = 0;
2476  int sx, sy;
2477 
2478  if (s->quarter_sample) {
2479  motion_x /= 2;
2480  motion_y /= 2;
2481  }
2482 
2483  sx = motion_x & s_mask;
2484  sy = motion_y & s_mask;
2485  src_x += motion_x >> lowres + 1;
2486  src_y += motion_y >> lowres + 1;
2487 
2488  src += src_y * stride + src_x;
2489 
2490  if ((unsigned)src_x > FFMAX( h_edge_pos - (!!sx) - w, 0) ||
2491  (unsigned)src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
2493  s->linesize, s->linesize,
2494  w + 1, (h + 1) << field_based,
2495  src_x, src_y << field_based,
2496  h_edge_pos, v_edge_pos);
2497  src = s->sc.edge_emu_buffer;
2498  emu = 1;
2499  }
2500 
2501  sx = (sx << 2) >> lowres;
2502  sy = (sy << 2) >> lowres;
2503  if (field_select)
2504  src += s->linesize;
2505  pix_op[op_index](dest, src, stride, h, sx, sy);
2506  return emu;
2507 }
2508 
2509 /* apply one mpeg motion vector to the three components */
2511  uint8_t *dest_y,
2512  uint8_t *dest_cb,
2513  uint8_t *dest_cr,
2514  int field_based,
2515  int bottom_field,
2516  int field_select,
2517  uint8_t **ref_picture,
2518  h264_chroma_mc_func *pix_op,
2519  int motion_x, int motion_y,
2520  int h, int mb_y)
2521 {
2522  uint8_t *ptr_y, *ptr_cb, *ptr_cr;
2523  int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, sx, sy, uvsx, uvsy;
2524  ptrdiff_t uvlinesize, linesize;
2525  const int lowres = s->avctx->lowres;
2526  const int op_index = FFMIN(lowres-1+s->chroma_x_shift, 3);
2527  const int block_s = 8>>lowres;
2528  const int s_mask = (2 << lowres) - 1;
2529  const int h_edge_pos = s->h_edge_pos >> lowres;
2530  const int v_edge_pos = s->v_edge_pos >> lowres;
2531  linesize = s->current_picture.f->linesize[0] << field_based;
2532  uvlinesize = s->current_picture.f->linesize[1] << field_based;
2533 
2534  // FIXME obviously not perfect but qpel will not work in lowres anyway
2535  if (s->quarter_sample) {
2536  motion_x /= 2;
2537  motion_y /= 2;
2538  }
2539 
2540  if(field_based){
2541  motion_y += (bottom_field - field_select)*((1 << lowres)-1);
2542  }
2543 
2544  sx = motion_x & s_mask;
2545  sy = motion_y & s_mask;
2546  src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);
2547  src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);
2548 
2549  if (s->out_format == FMT_H263) {
2550  uvsx = ((motion_x >> 1) & s_mask) | (sx & 1);
2551  uvsy = ((motion_y >> 1) & s_mask) | (sy & 1);
2552  uvsrc_x = src_x >> 1;
2553  uvsrc_y = src_y >> 1;
2554  } else if (s->out_format == FMT_H261) {
2555  // even chroma mv's are full pel in H261
2556  mx = motion_x / 4;
2557  my = motion_y / 4;
2558  uvsx = (2 * mx) & s_mask;
2559  uvsy = (2 * my) & s_mask;
2560  uvsrc_x = s->mb_x * block_s + (mx >> lowres);
2561  uvsrc_y = mb_y * block_s + (my >> lowres);
2562  } else {
2563  if(s->chroma_y_shift){
2564  mx = motion_x / 2;
2565  my = motion_y / 2;
2566  uvsx = mx & s_mask;
2567  uvsy = my & s_mask;
2568  uvsrc_x = s->mb_x * block_s + (mx >> lowres + 1);
2569  uvsrc_y = (mb_y * block_s >> field_based) + (my >> lowres + 1);
2570  } else {
2571  if(s->chroma_x_shift){
2572  //Chroma422
2573  mx = motion_x / 2;
2574  uvsx = mx & s_mask;
2575  uvsy = motion_y & s_mask;
2576  uvsrc_y = src_y;
2577  uvsrc_x = s->mb_x*block_s + (mx >> (lowres+1));
2578  } else {
2579  //Chroma444
2580  uvsx = motion_x & s_mask;
2581  uvsy = motion_y & s_mask;
2582  uvsrc_x = src_x;
2583  uvsrc_y = src_y;
2584  }
2585  }
2586  }
2587 
2588  ptr_y = ref_picture[0] + src_y * linesize + src_x;
2589  ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;
2590  ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;
2591 
2592  if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s, 0) || uvsrc_y<0 ||
2593  (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {
2594  s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr_y,
2595  linesize >> field_based, linesize >> field_based,
2596  17, 17 + field_based,
2597  src_x, src_y << field_based, h_edge_pos,
2598  v_edge_pos);
2599  ptr_y = s->sc.edge_emu_buffer;
2600  if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
2601  uint8_t *ubuf = s->sc.edge_emu_buffer + 18 * s->linesize;
2602  uint8_t *vbuf =ubuf + 9 * s->uvlinesize;
2603  s->vdsp.emulated_edge_mc(ubuf, ptr_cb,
2604  uvlinesize >> field_based, uvlinesize >> field_based,
2605  9, 9 + field_based,
2606  uvsrc_x, uvsrc_y << field_based,
2607  h_edge_pos >> 1, v_edge_pos >> 1);
2608  s->vdsp.emulated_edge_mc(vbuf, ptr_cr,
2609  uvlinesize >> field_based,uvlinesize >> field_based,
2610  9, 9 + field_based,
2611  uvsrc_x, uvsrc_y << field_based,
2612  h_edge_pos >> 1, v_edge_pos >> 1);
2613  ptr_cb = ubuf;
2614  ptr_cr = vbuf;
2615  }
2616  }
2617 
2618  // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f->data
2619  if (bottom_field) {
2620  dest_y += s->linesize;
2621  dest_cb += s->uvlinesize;
2622  dest_cr += s->uvlinesize;
2623  }
2624 
2625  if (field_select) {
2626  ptr_y += s->linesize;
2627  ptr_cb += s->uvlinesize;
2628  ptr_cr += s->uvlinesize;
2629  }
2630 
2631  sx = (sx << 2) >> lowres;
2632  sy = (sy << 2) >> lowres;
2633  pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);
2634 
2635  if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
2636  int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;
2637  uvsx = (uvsx << 2) >> lowres;
2638  uvsy = (uvsy << 2) >> lowres;
2639  if (hc) {
2640  pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);
2641  pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);
2642  }
2643  }
2644  // FIXME h261 lowres loop filter
2645 }
2646 
2648  uint8_t *dest_cb, uint8_t *dest_cr,
2649  uint8_t **ref_picture,
2650  h264_chroma_mc_func * pix_op,
2651  int mx, int my)
2652 {
2653  const int lowres = s->avctx->lowres;
2654  const int op_index = FFMIN(lowres, 3);
2655  const int block_s = 8 >> lowres;
2656  const int s_mask = (2 << lowres) - 1;
2657  const int h_edge_pos = s->h_edge_pos >> lowres + 1;
2658  const int v_edge_pos = s->v_edge_pos >> lowres + 1;
2659  int emu = 0, src_x, src_y, sx, sy;
2660  ptrdiff_t offset;
2661  uint8_t *ptr;
2662 
2663  if (s->quarter_sample) {
2664  mx /= 2;
2665  my /= 2;
2666  }
2667 
2668  /* In case of 8X8, we construct a single chroma motion vector
2669  with a special rounding */
2670  mx = ff_h263_round_chroma(mx);
2671  my = ff_h263_round_chroma(my);
2672 
2673  sx = mx & s_mask;
2674  sy = my & s_mask;
2675  src_x = s->mb_x * block_s + (mx >> lowres + 1);
2676  src_y = s->mb_y * block_s + (my >> lowres + 1);
2677 
2678  offset = src_y * s->uvlinesize + src_x;
2679  ptr = ref_picture[1] + offset;
2680  if ((unsigned) src_x > FFMAX(h_edge_pos - (!!sx) - block_s, 0) ||
2681  (unsigned) src_y > FFMAX(v_edge_pos - (!!sy) - block_s, 0)) {
2683  s->uvlinesize, s->uvlinesize,
2684  9, 9,
2685  src_x, src_y, h_edge_pos, v_edge_pos);
2686  ptr = s->sc.edge_emu_buffer;
2687  emu = 1;
2688  }
2689  sx = (sx << 2) >> lowres;
2690  sy = (sy << 2) >> lowres;
2691  pix_op[op_index](dest_cb, ptr, s->uvlinesize, block_s, sx, sy);
2692 
2693  ptr = ref_picture[2] + offset;
2694  if (emu) {
2696  s->uvlinesize, s->uvlinesize,
2697  9, 9,
2698  src_x, src_y, h_edge_pos, v_edge_pos);
2699  ptr = s->sc.edge_emu_buffer;
2700  }
2701  pix_op[op_index](dest_cr, ptr, s->uvlinesize, block_s, sx, sy);
2702 }
2703 
2704 /**
2705  * motion compensation of a single macroblock
2706  * @param s context
2707  * @param dest_y luma destination pointer
2708  * @param dest_cb chroma cb/u destination pointer
2709  * @param dest_cr chroma cr/v destination pointer
2710  * @param dir direction (0->forward, 1->backward)
2711  * @param ref_picture array[3] of pointers to the 3 planes of the reference picture
2712  * @param pix_op halfpel motion compensation function (average or put normally)
2713  * the motion vectors are taken from s->mv and the MV type from s->mv_type
2714  */
2715 static inline void MPV_motion_lowres(MpegEncContext *s,
2716  uint8_t *dest_y, uint8_t *dest_cb,
2717  uint8_t *dest_cr,
2718  int dir, uint8_t **ref_picture,
2719  h264_chroma_mc_func *pix_op)
2720 {
2721  int mx, my;
2722  int mb_x, mb_y, i;
2723  const int lowres = s->avctx->lowres;
2724  const int block_s = 8 >>lowres;
2725 
2726  mb_x = s->mb_x;
2727  mb_y = s->mb_y;
2728 
2729  switch (s->mv_type) {
2730  case MV_TYPE_16X16:
2731  mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2732  0, 0, 0,
2733  ref_picture, pix_op,
2734  s->mv[dir][0][0], s->mv[dir][0][1],
2735  2 * block_s, mb_y);
2736  break;
2737  case MV_TYPE_8X8:
2738  mx = 0;
2739  my = 0;
2740  for (i = 0; i < 4; i++) {
2741  hpel_motion_lowres(s, dest_y + ((i & 1) + (i >> 1) *
2742  s->linesize) * block_s,
2743  ref_picture[0], 0, 0,
2744  (2 * mb_x + (i & 1)) * block_s,
2745  (2 * mb_y + (i >> 1)) * block_s,
2746  s->width, s->height, s->linesize,
2747  s->h_edge_pos >> lowres, s->v_edge_pos >> lowres,
2748  block_s, block_s, pix_op,
2749  s->mv[dir][i][0], s->mv[dir][i][1]);
2750 
2751  mx += s->mv[dir][i][0];
2752  my += s->mv[dir][i][1];
2753  }
2754 
2755  if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY))
2756  chroma_4mv_motion_lowres(s, dest_cb, dest_cr, ref_picture,
2757  pix_op, mx, my);
2758  break;
2759  case MV_TYPE_FIELD:
2760  if (s->picture_structure == PICT_FRAME) {
2761  /* top field */
2762  mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2763  1, 0, s->field_select[dir][0],
2764  ref_picture, pix_op,
2765  s->mv[dir][0][0], s->mv[dir][0][1],
2766  block_s, mb_y);
2767  /* bottom field */
2768  mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2769  1, 1, s->field_select[dir][1],
2770  ref_picture, pix_op,
2771  s->mv[dir][1][0], s->mv[dir][1][1],
2772  block_s, mb_y);
2773  } else {
2774  if (s->picture_structure != s->field_select[dir][0] + 1 &&
2775  s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
2776  ref_picture = s->current_picture_ptr->f->data;
2777 
2778  }
2779  mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2780  0, 0, s->field_select[dir][0],
2781  ref_picture, pix_op,
2782  s->mv[dir][0][0],
2783  s->mv[dir][0][1], 2 * block_s, mb_y >> 1);
2784  }
2785  break;
2786  case MV_TYPE_16X8:
2787  for (i = 0; i < 2; i++) {
2788  uint8_t **ref2picture;
2789 
2790  if (s->picture_structure == s->field_select[dir][i] + 1 ||
2791  s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
2792  ref2picture = ref_picture;
2793  } else {
2794  ref2picture = s->current_picture_ptr->f->data;
2795  }
2796 
2797  mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2798  0, 0, s->field_select[dir][i],
2799  ref2picture, pix_op,
2800  s->mv[dir][i][0], s->mv[dir][i][1] +
2801  2 * block_s * i, block_s, mb_y >> 1);
2802 
2803  dest_y += 2 * block_s * s->linesize;
2804  dest_cb += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2805  dest_cr += (2 * block_s >> s->chroma_y_shift) * s->uvlinesize;
2806  }
2807  break;
2808  case MV_TYPE_DMV:
2809  if (s->picture_structure == PICT_FRAME) {
2810  for (i = 0; i < 2; i++) {
2811  int j;
2812  for (j = 0; j < 2; j++) {
2813  mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2814  1, j, j ^ i,
2815  ref_picture, pix_op,
2816  s->mv[dir][2 * i + j][0],
2817  s->mv[dir][2 * i + j][1],
2818  block_s, mb_y);
2819  }
2821  }
2822  } else {
2823  for (i = 0; i < 2; i++) {
2824  mpeg_motion_lowres(s, dest_y, dest_cb, dest_cr,
2825  0, 0, s->picture_structure != i + 1,
2826  ref_picture, pix_op,
2827  s->mv[dir][2 * i][0],s->mv[dir][2 * i][1],
2828  2 * block_s, mb_y >> 1);
2829 
2830  // after put we make avg of the same block
2832 
2833  // opposite parity is always in the same
2834  // frame if this is second field
2835  if (!s->first_field) {
2836  ref_picture = s->current_picture_ptr->f->data;
2837  }
2838  }
2839  }
2840  break;
2841  default:
2842  av_assert2(0);
2843  }
2844 }
2845 
2846 /**
2847  * find the lowest MB row referenced in the MVs
2848  */
2850 {
2851  int my_max = INT_MIN, my_min = INT_MAX, qpel_shift = !s->quarter_sample;
2852  int my, off, i, mvs;
2853 
2854  if (s->picture_structure != PICT_FRAME || s->mcsel)
2855  goto unhandled;
2856 
2857  switch (s->mv_type) {
2858  case MV_TYPE_16X16:
2859  mvs = 1;
2860  break;
2861  case MV_TYPE_16X8:
2862  mvs = 2;
2863  break;
2864  case MV_TYPE_8X8:
2865  mvs = 4;
2866  break;
2867  default:
2868  goto unhandled;
2869  }
2870 
2871  for (i = 0; i < mvs; i++) {
2872  my = s->mv[dir][i][1];
2873  my_max = FFMAX(my_max, my);
2874  my_min = FFMIN(my_min, my);
2875  }
2876 
2877  off = ((FFMAX(-my_min, my_max)<<qpel_shift) + 63) >> 6;
2878 
2879  return av_clip(s->mb_y + off, 0, s->mb_height - 1);
2880 unhandled:
2881  return s->mb_height-1;
2882 }
2883 
2884 /* put block[] to dest[] */
2885 static inline void put_dct(MpegEncContext *s,
2886  int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2887 {
2888  s->dct_unquantize_intra(s, block, i, qscale);
2889  s->idsp.idct_put(dest, line_size, block);
2890 }
2891 
2892 /* add block[] to dest[] */
2893 static inline void add_dct(MpegEncContext *s,
2894  int16_t *block, int i, uint8_t *dest, int line_size)
2895 {
2896  if (s->block_last_index[i] >= 0) {
2897  s->idsp.idct_add(dest, line_size, block);
2898  }
2899 }
2900 
2901 static inline void add_dequant_dct(MpegEncContext *s,
2902  int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
2903 {
2904  if (s->block_last_index[i] >= 0) {
2905  s->dct_unquantize_inter(s, block, i, qscale);
2906 
2907  s->idsp.idct_add(dest, line_size, block);
2908  }
2909 }
2910 
2911 /**
2912  * Clean dc, ac, coded_block for the current non-intra MB.
2913  */
2915 {
2916  int wrap = s->b8_stride;
2917  int xy = s->block_index[0];
2918 
2919  s->dc_val[0][xy ] =
2920  s->dc_val[0][xy + 1 ] =
2921  s->dc_val[0][xy + wrap] =
2922  s->dc_val[0][xy + 1 + wrap] = 1024;
2923  /* ac pred */
2924  memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
2925  memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
2926  if (s->msmpeg4_version>=3) {
2927  s->coded_block[xy ] =
2928  s->coded_block[xy + 1 ] =
2929  s->coded_block[xy + wrap] =
2930  s->coded_block[xy + 1 + wrap] = 0;
2931  }
2932  /* chroma */
2933  wrap = s->mb_stride;
2934  xy = s->mb_x + s->mb_y * wrap;
2935  s->dc_val[1][xy] =
2936  s->dc_val[2][xy] = 1024;
2937  /* ac pred */
2938  memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
2939  memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
2940 
2941  s->mbintra_table[xy]= 0;
2942 }
2943 
2944 /* generic function called after a macroblock has been parsed by the
2945  decoder or after it has been encoded by the encoder.
2946 
2947  Important variables used:
2948  s->mb_intra : true if intra macroblock
2949  s->mv_dir : motion vector direction
2950  s->mv_type : motion vector type
2951  s->mv : motion vector
2952  s->interlaced_dct : true if interlaced dct used (mpeg2)
2953  */
2954 static av_always_inline
2956  int lowres_flag, int is_mpeg12)
2957 {
2958  const int mb_xy = s->mb_y * s->mb_stride + s->mb_x;
2959 
2960  if (CONFIG_XVMC &&
2961  s->avctx->hwaccel && s->avctx->hwaccel->decode_mb) {
2962  s->avctx->hwaccel->decode_mb(s);//xvmc uses pblocks
2963  return;
2964  }
2965 
2966  if(s->avctx->debug&FF_DEBUG_DCT_COEFF) {
2967  /* print DCT coefficients */
2968  int i,j;
2969  av_log(s->avctx, AV_LOG_DEBUG, "DCT coeffs of MB at %dx%d:\n", s->mb_x, s->mb_y);
2970  for(i=0; i<6; i++){
2971  for(j=0; j<64; j++){
2972  av_log(s->avctx, AV_LOG_DEBUG, "%5d",
2973  block[i][s->idsp.idct_permutation[j]]);
2974  }
2975  av_log(s->avctx, AV_LOG_DEBUG, "\n");
2976  }
2977  }
2978 
2979  s->current_picture.qscale_table[mb_xy] = s->qscale;
2980 
2981  /* update DC predictors for P macroblocks */
2982  if (!s->mb_intra) {
2983  if (!is_mpeg12 && (s->h263_pred || s->h263_aic)) {
2984  if(s->mbintra_table[mb_xy])
2986  } else {
2987  s->last_dc[0] =
2988  s->last_dc[1] =
2989  s->last_dc[2] = 128 << s->intra_dc_precision;
2990  }
2991  }
2992  else if (!is_mpeg12 && (s->h263_pred || s->h263_aic))
2993  s->mbintra_table[mb_xy]=1;
2994 
2996  !(s->encoding && (s->intra_only || s->pict_type == AV_PICTURE_TYPE_B) &&
2997  s->avctx->mb_decision != FF_MB_DECISION_RD)) { // FIXME precalc
2998  uint8_t *dest_y, *dest_cb, *dest_cr;
2999  int dct_linesize, dct_offset;
3000  op_pixels_func (*op_pix)[4];
3001  qpel_mc_func (*op_qpix)[16];
3002  const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
3003  const int uvlinesize = s->current_picture.f->linesize[1];
3004  const int readable= s->pict_type != AV_PICTURE_TYPE_B || s->encoding || s->avctx->draw_horiz_band || lowres_flag;
3005  const int block_size= lowres_flag ? 8>>s->avctx->lowres : 8;
3006 
3007  /* avoid copy if macroblock skipped in last frame too */
3008  /* skip only during decoding as we might trash the buffers during encoding a bit */
3009  if(!s->encoding){
3010  uint8_t *mbskip_ptr = &s->mbskip_table[mb_xy];
3011 
3012  if (s->mb_skipped) {
3013  s->mb_skipped= 0;
3015  *mbskip_ptr = 1;
3016  } else if(!s->current_picture.reference) {
3017  *mbskip_ptr = 1;
3018  } else{
3019  *mbskip_ptr = 0; /* not skipped */
3020  }
3021  }
3022 
3023  dct_linesize = linesize << s->interlaced_dct;
3024  dct_offset = s->interlaced_dct ? linesize : linesize * block_size;
3025 
3026  if(readable){
3027  dest_y= s->dest[0];
3028  dest_cb= s->dest[1];
3029  dest_cr= s->dest[2];
3030  }else{
3031  dest_y = s->sc.b_scratchpad;
3032  dest_cb= s->sc.b_scratchpad+16*linesize;
3033  dest_cr= s->sc.b_scratchpad+32*linesize;
3034  }
3035 
3036  if (!s->mb_intra) {
3037  /* motion handling */
3038  /* decoding or more than one mb_type (MC was already done otherwise) */
3039  if(!s->encoding){
3040 
3042  if (s->mv_dir & MV_DIR_FORWARD) {
3045  0);
3046  }
3047  if (s->mv_dir & MV_DIR_BACKWARD) {
3050  0);
3051  }
3052  }
3053 
3054  if(lowres_flag){
3056 
3057  if (s->mv_dir & MV_DIR_FORWARD) {
3058  MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix);
3060  }
3061  if (s->mv_dir & MV_DIR_BACKWARD) {
3062  MPV_motion_lowres(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix);
3063  }
3064  }else{
3065  op_qpix = s->me.qpel_put;
3066  if ((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
3067  op_pix = s->hdsp.put_pixels_tab;
3068  }else{
3069  op_pix = s->hdsp.put_no_rnd_pixels_tab;
3070  }
3071  if (s->mv_dir & MV_DIR_FORWARD) {
3072  ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 0, s->last_picture.f->data, op_pix, op_qpix);
3073  op_pix = s->hdsp.avg_pixels_tab;
3074  op_qpix= s->me.qpel_avg;
3075  }
3076  if (s->mv_dir & MV_DIR_BACKWARD) {
3077  ff_mpv_motion(s, dest_y, dest_cb, dest_cr, 1, s->next_picture.f->data, op_pix, op_qpix);
3078  }
3079  }
3080  }
3081 
3082  /* skip dequant / idct if we are really late ;) */
3083  if(s->avctx->skip_idct){
3086  || s->avctx->skip_idct >= AVDISCARD_ALL)
3087  goto skip_idct;
3088  }
3089 
3090  /* add dct residue */
3092  || (s->codec_id==AV_CODEC_ID_MPEG4 && !s->mpeg_quant))){
3093  add_dequant_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
3094  add_dequant_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
3095  add_dequant_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
3096  add_dequant_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
3097 
3098  if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
3099  if (s->chroma_y_shift){
3100  add_dequant_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
3101  add_dequant_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
3102  }else{
3103  dct_linesize >>= 1;
3104  dct_offset >>=1;
3105  add_dequant_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
3106  add_dequant_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
3107  add_dequant_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
3108  add_dequant_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
3109  }
3110  }
3111  } else if(is_mpeg12 || (s->codec_id != AV_CODEC_ID_WMV2)){
3112  add_dct(s, block[0], 0, dest_y , dct_linesize);
3113  add_dct(s, block[1], 1, dest_y + block_size, dct_linesize);
3114  add_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize);
3115  add_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize);
3116 
3117  if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
3118  if(s->chroma_y_shift){//Chroma420
3119  add_dct(s, block[4], 4, dest_cb, uvlinesize);
3120  add_dct(s, block[5], 5, dest_cr, uvlinesize);
3121  }else{
3122  //chroma422
3123  dct_linesize = uvlinesize << s->interlaced_dct;
3124  dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
3125 
3126  add_dct(s, block[4], 4, dest_cb, dct_linesize);
3127  add_dct(s, block[5], 5, dest_cr, dct_linesize);
3128  add_dct(s, block[6], 6, dest_cb+dct_offset, dct_linesize);
3129  add_dct(s, block[7], 7, dest_cr+dct_offset, dct_linesize);
3130  if(!s->chroma_x_shift){//Chroma444
3131  add_dct(s, block[8], 8, dest_cb+block_size, dct_linesize);
3132  add_dct(s, block[9], 9, dest_cr+block_size, dct_linesize);
3133  add_dct(s, block[10], 10, dest_cb+block_size+dct_offset, dct_linesize);
3134  add_dct(s, block[11], 11, dest_cr+block_size+dct_offset, dct_linesize);
3135  }
3136  }
3137  }//fi gray
3138  }
3140  ff_wmv2_add_mb(s, block, dest_y, dest_cb, dest_cr);
3141  }
3142  } else {
3143  /* dct only in intra block */
3145  put_dct(s, block[0], 0, dest_y , dct_linesize, s->qscale);
3146  put_dct(s, block[1], 1, dest_y + block_size, dct_linesize, s->qscale);
3147  put_dct(s, block[2], 2, dest_y + dct_offset , dct_linesize, s->qscale);
3148  put_dct(s, block[3], 3, dest_y + dct_offset + block_size, dct_linesize, s->qscale);
3149 
3150  if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
3151  if(s->chroma_y_shift){
3152  put_dct(s, block[4], 4, dest_cb, uvlinesize, s->chroma_qscale);
3153  put_dct(s, block[5], 5, dest_cr, uvlinesize, s->chroma_qscale);
3154  }else{
3155  dct_offset >>=1;
3156  dct_linesize >>=1;
3157  put_dct(s, block[4], 4, dest_cb, dct_linesize, s->chroma_qscale);
3158  put_dct(s, block[5], 5, dest_cr, dct_linesize, s->chroma_qscale);
3159  put_dct(s, block[6], 6, dest_cb + dct_offset, dct_linesize, s->chroma_qscale);
3160  put_dct(s, block[7], 7, dest_cr + dct_offset, dct_linesize, s->chroma_qscale);
3161  }
3162  }
3163  }else{
3164  s->idsp.idct_put(dest_y, dct_linesize, block[0]);
3165  s->idsp.idct_put(dest_y + block_size, dct_linesize, block[1]);
3166  s->idsp.idct_put(dest_y + dct_offset, dct_linesize, block[2]);
3167  s->idsp.idct_put(dest_y + dct_offset + block_size, dct_linesize, block[3]);
3168 
3169  if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
3170  if(s->chroma_y_shift){
3171  s->idsp.idct_put(dest_cb, uvlinesize, block[4]);
3172  s->idsp.idct_put(dest_cr, uvlinesize, block[5]);
3173  }else{
3174 
3175  dct_linesize = uvlinesize << s->interlaced_dct;
3176  dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size;
3177 
3178  s->idsp.idct_put(dest_cb, dct_linesize, block[4]);
3179  s->idsp.idct_put(dest_cr, dct_linesize, block[5]);
3180  s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, block[6]);
3181  s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, block[7]);
3182  if(!s->chroma_x_shift){//Chroma444
3183  s->idsp.idct_put(dest_cb + block_size, dct_linesize, block[8]);
3184  s->idsp.idct_put(dest_cr + block_size, dct_linesize, block[9]);
3185  s->idsp.idct_put(dest_cb + block_size + dct_offset, dct_linesize, block[10]);
3186  s->idsp.idct_put(dest_cr + block_size + dct_offset, dct_linesize, block[11]);
3187  }
3188  }
3189  }//gray
3190  }
3191  }
3192 skip_idct:
3193  if(!readable){
3194  s->hdsp.put_pixels_tab[0][0](s->dest[0], dest_y , linesize,16);
3195  if (!CONFIG_GRAY || !(s->avctx->flags & CODEC_FLAG_GRAY)) {
3196  s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[1], dest_cb, uvlinesize,16 >> s->chroma_y_shift);
3197  s->hdsp.put_pixels_tab[s->chroma_x_shift][0](s->dest[2], dest_cr, uvlinesize,16 >> s->chroma_y_shift);
3198  }
3199  }
3200  }
3201 }
3202 
3203 void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64])
3204 {
3205 #if !CONFIG_SMALL
3206  if(s->out_format == FMT_MPEG1) {
3207  if(s->avctx->lowres) mpv_decode_mb_internal(s, block, 1, 1);
3208  else mpv_decode_mb_internal(s, block, 0, 1);
3209  } else
3210 #endif
3211  if(s->avctx->lowres) mpv_decode_mb_internal(s, block, 1, 0);
3212  else mpv_decode_mb_internal(s, block, 0, 0);
3213 }
3214 
3216 {
3219  s->first_field, s->low_delay);
3220 }
3221 
3222 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
3223  const int linesize = s->current_picture.f->linesize[0]; //not s->linesize as this would be wrong for field pics
3224  const int uvlinesize = s->current_picture.f->linesize[1];
3225  const int mb_size= 4 - s->avctx->lowres;
3226 
3227  s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
3228  s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
3229  s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
3230  s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
3231  s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
3232  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;
3233  //block_index is not used by mpeg2, so it is not affected by chroma_format
3234 
3235  s->dest[0] = s->current_picture.f->data[0] + (int)((s->mb_x - 1U) << mb_size);
3236  s->dest[1] = s->current_picture.f->data[1] + (int)((s->mb_x - 1U) << (mb_size - s->chroma_x_shift));
3237  s->dest[2] = s->current_picture.f->data[2] + (int)((s->mb_x - 1U) << (mb_size - s->chroma_x_shift));
3238 
3240  {
3241  if(s->picture_structure==PICT_FRAME){
3242  s->dest[0] += s->mb_y * linesize << mb_size;
3243  s->dest[1] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
3244  s->dest[2] += s->mb_y * uvlinesize << (mb_size - s->chroma_y_shift);
3245  }else{
3246  s->dest[0] += (s->mb_y>>1) * linesize << mb_size;
3247  s->dest[1] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
3248  s->dest[2] += (s->mb_y>>1) * uvlinesize << (mb_size - s->chroma_y_shift);
3250  }
3251  }
3252 }
3253 
3254 /**
3255  * Permute an 8x8 block.
3256  * @param block the block which will be permuted according to the given permutation vector
3257  * @param permutation the permutation vector
3258  * @param last the last non zero coefficient in scantable order, used to speed the permutation up
3259  * @param scantable the used scantable, this is only used to speed the permutation up, the block is not
3260  * (inverse) permutated to scantable order!
3261  */
3262 void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
3263 {
3264  int i;
3265  int16_t temp[64];
3266 
3267  if(last<=0) return;
3268  //if(permutation[1]==1) return; //FIXME it is ok but not clean and might fail for some permutations
3269 
3270  for(i=0; i<=last; i++){
3271  const int j= scantable[i];
3272  temp[j]= block[j];
3273  block[j]=0;
3274  }
3275 
3276  for(i=0; i<=last; i++){
3277  const int j= scantable[i];
3278  const int perm_j= permutation[j];
3279  block[perm_j]= temp[j];
3280  }
3281 }
3282 
3284  int i;
3285  MpegEncContext *s = avctx->priv_data;
3286 
3287  if (!s || !s->picture)
3288  return;
3289 
3290  for (i = 0; i < MAX_PICTURE_COUNT; i++)
3291  ff_mpeg_unref_picture(s->avctx, &s->picture[i]);
3293 
3297 
3298  s->mb_x= s->mb_y= 0;
3299  s->closed_gop= 0;
3300 
3301  s->parse_context.state= -1;
3303  s->parse_context.overread= 0;
3305  s->parse_context.index= 0;
3306  s->parse_context.last_index= 0;
3307  s->bitstream_buffer_size=0;
3308  s->pp_time=0;
3309 }
3310 
3311 /**
3312  * set qscale and update qscale dependent variables.
3313  */
3314 void ff_set_qscale(MpegEncContext * s, int qscale)
3315 {
3316  if (qscale < 1)
3317  qscale = 1;
3318  else if (qscale > 31)
3319  qscale = 31;
3320 
3321  s->qscale = qscale;
3322  s->chroma_qscale= s->chroma_qscale_table[qscale];
3323 
3324  s->y_dc_scale= s->y_dc_scale_table[ qscale ];
3326 }
3327 
3329 {
3332 }
int last_time_base
Definition: mpegvideo.h:451
int bitstream_buffer_size
Definition: mpegvideo.h:479
uint8_t * scratchpad
data area for the ME algo, so that the ME does not need to malloc/free.
Definition: motion_est.h:42
#define FF_DEBUG_DCT_COEFF
Definition: avcodec.h:2577
IDCTDSPContext idsp
Definition: mpegvideo.h:299
#define NULL
Definition: coverity.c:32
static int init_duplicate_context(MpegEncContext *s)
Definition: mpegvideo.c:753
int ff_thread_can_start_frame(AVCodecContext *avctx)
const struct AVCodec * codec
Definition: avcodec.h:1250
int16_t(* b_bidir_back_mv_table_base)[2]
Definition: mpegvideo.h:313
av_cold void ff_mpv_common_init_arm(MpegEncContext *s)
Definition: mpegvideo_arm.c:43
discard all frames except keyframes
Definition: avcodec.h:668
int8_t * ref_index[2]
Definition: mpegvideo.h:106
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:3222
float v
int picture_number
Definition: mpegvideo.h:196
const char * s
Definition: avisynth_c.h:631
#define MAX_PICTURE_COUNT
Definition: mpegvideo.h:64
void ff_wmv2_add_mb(MpegEncContext *s, int16_t block1[6][64], uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr)
Definition: wmv2.c:82
av_cold void ff_mpv_common_init_neon(MpegEncContext *s)
Definition: mpegvideo.c:126
ScanTable intra_v_scantable
Definition: mpegvideo.h:162
AVBufferRef * mb_var_buf
Definition: mpegvideo.h:108
av_cold void ff_mpegvideodsp_init(MpegVideoDSPContext *c)
Definition: mpegvideodsp.c:110
static int shift(int a, int b)
Definition: sonic.c:82
#define CONFIG_WMV2_ENCODER
Definition: config.h:1313
void(* dct_unquantize_inter)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:580
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:124
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
#define FF_ALLOCZ_ARRAY_OR_GOTO(ctx, p, nelem, elsize, label)
Definition: internal.h:156
int16_t(* p_mv_table)[2]
MV table (1MV per MB) p-frame encoding.
Definition: mpegvideo.h:317
#define FF_DEBUG_VIS_QP
only access through AVOptions from outside libavcodec
Definition: avcodec.h:2587
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:222
#define MV_TYPE_FIELD
2 vectors, one per field
Definition: mpegvideo.h:336
#define MAKE_WRITABLE(table)
const uint8_t * y_dc_scale_table
qscale -> y_dc_scale table
Definition: mpegvideo.h:257
uint8_t * mb_mean
Table for MB luminance.
Definition: mpegvideo.h:118
uint8_t * edge_emu_buffer
temporary buffer for if MVs point to out-of-frame data
Definition: mpegvideo.h:141
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1424
#define ARCH_PPC
Definition: config.h:29
static int clip_line(int *sx, int *sy, int *ex, int *ey, int maxx)
Definition: mpegvideo.c:1922
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:182
#define CONFIG_XVMC
Definition: config.h:483
AVFrame * f
Definition: thread.h:36
int16_t src_x
Absolute source position.
Definition: motion_vector.h:38
static void chroma_4mv_motion_lowres(MpegEncContext *s, uint8_t *dest_cb, uint8_t *dest_cr, uint8_t **ref_picture, h264_chroma_mc_func *pix_op, int mx, int my)
Definition: mpegvideo.c:2647
uint8_t * coded_block_base
Definition: mpegvideo.h:260
else temp
Definition: vf_mcdeint.c:257
static int update_picture_tables(Picture *dst, Picture *src)
Definition: mpegvideo.c:670
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:441
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:223
uint16_t * mb_var
Table for MB variances.
Definition: mpegvideo.h:109
void ff_block_permute(int16_t *block, uint8_t *permutation, const uint8_t *scantable, int last)
Permute an 8x8 block.
Definition: mpegvideo.c:3262
int16_t(*[3] ac_val)[16]
used for mpeg4 AC prediction, all 3 arrays must be continuous
Definition: mpegvideo.h:263
MJPEG encoder.
int v_edge_pos
horizontal / vertical position of the right/bottom edge (pixel replication)
Definition: mpegvideo.h:201
h264_chroma_mc_func put_h264_chroma_pixels_tab[4]
Definition: h264chroma.h:27
void * opaque
for some private data of the user
Definition: frame.h:346
#define me
static void gray8(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
Definition: mpegvideo.c:294
static void gray_frame(AVFrame *frame)
Definition: mpegvideo.c:1665
int msmpeg4_version
0=not msmpeg4, 1=mp41, 2=mp42, 3=mp43/divx3 4=wmv1/7 5=wmv2/8
Definition: mpegvideo.h:499
int needs_realloc
Picture needs to be reallocated (eg due to a frame size change)
Definition: mpegvideo.h:132
#define HAVE_INTRINSICS_NEON
Definition: config.h:224
uint8_t * bitstream_buffer
Definition: mpegvideo.h:478
enum AVCodecID codec_id
Definition: mpegvideo.h:181
static int find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
Definition: mpegvideo.c:1618
#define DELAYED_PIC_REF
Value of Picture.reference when Picture is not a reference picture, but is held for delayed output...
Definition: diracdec.c:74
void(* clear_blocks)(int16_t *blocks)
Definition: blockdsp.h:36
int field_picture
whether or not the picture was encoded in separate fields
Definition: mpegvideo.h:126
int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int qp_type)
Definition: frame.c:49
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1444
int16_t(*[2][2] p_field_mv_table)[2]
MV table (2MV per MB) interlaced p-frame encoding.
Definition: mpegvideo.h:323
int16_t(* p_mv_table_base)[2]
Definition: mpegvideo.h:309
static int make_tables_writable(Picture *pic)
Definition: mpegvideo.c:548
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
av_cold void ff_h264chroma_init(H264ChromaContext *c, int bit_depth)
Definition: h264chroma.c:41
uint32_t * score_map
map to store the scores
Definition: motion_est.h:49
mpegvideo header.
#define FF_ARRAY_ELEMS(a)
#define FF_DEBUG_VIS_MV_B_BACK
Definition: avcodec.h:2604
discard all
Definition: avcodec.h:669
uint8_t permutated[64]
Definition: idctdsp.h:31
static void free_duplicate_context(MpegEncContext *s)
Definition: mpegvideo.c:805
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2727
#define FF_DEBUG_VIS_MB_TYPE
only access through AVOptions from outside libavcodec
Definition: avcodec.h:2588
int padding_bug_score
used to detect the VERY common padding bug in MPEG4
Definition: mpegvideo.h:474
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:202
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:273
int h263_aic
Advanded INTRA Coding (AIC)
Definition: mpegvideo.h:156
int16_t(* b_back_mv_table)[2]
MV table (1MV per MB) backward mode b-frame encoding.
Definition: mpegvideo.h:319
int chroma_x_shift
Definition: mpegvideo.h:537
int encoding
true if we are encoding (vs decoding)
Definition: mpegvideo.h:183
void(* dct_unquantize_h263_intra)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:574
int field_select[2][2]
Definition: mpegvideo.h:344
void(* dct_unquantize_intra)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:578
int block_wrap[6]
Definition: mpegvideo.h:361
static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:81
Macro definitions for various function/variable attributes.
#define FFALIGN(x, a)
Definition: common.h:71
int16_t(* b_back_mv_table_base)[2]
Definition: mpegvideo.h:311
#define REBASE_PICTURE(pic, new_ctx, old_ctx)
static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
Definition: mpegvideo.c:825
void ff_clean_intra_table_entries(MpegEncContext *s)
Clean dc, ac, coded_block for the current non-intra MB.
Definition: mpegvideo.c:2914
void(* dct_unquantize_h263_inter)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:576
#define COLOR(theta, r)
#define FF_DEBUG_QP
Definition: avcodec.h:2570
int b_frame_score
Definition: mpegvideo.h:131
int av_codec_is_encoder(const AVCodec *codec)
Definition: utils.c:187
int alloc_mb_width
mb_width used to allocate tables
Definition: mpegvideo.h:114
#define CODEC_FLAG_PSNR
error[?] variables will be set during encoding.
Definition: avcodec.h:746
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2644
#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:3215
void(* emulated_edge_mc)(uint8_t *dst, const uint8_t *src, ptrdiff_t dst_linesize, ptrdiff_t src_linesize, int block_w, int block_h, int src_x, int src_y, int w, int h)
Copy a rectangular area of samples to a temporary buffer and replicate the border samples...
Definition: videodsp.h:63
int context_reinit
Definition: mpegvideo.h:608
static int alloc_frame_buffer(AVCodecContext *avctx, Picture *pic, MotionEstContext *me, ScratchpadContext *sc, int chroma_x_shift, int chroma_y_shift, int linesize, int uvlinesize)
Allocate a frame buffer.
Definition: mpegvideo.c:402
const uint8_t ff_mpeg1_dc_scale_table[128]
Definition: mpegvideodata.c:27
int16_t * dc_val_base
Definition: mpegvideo.h:255
ScratchpadContext sc
Definition: mpegvideo.h:271
if()
Definition: avfilter.c:975
uint8_t
#define av_cold
Definition: attributes.h:74
av_cold void ff_mpv_common_init_axp(MpegEncContext *s)
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:135
#define mb
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:63
enum OutputFormat out_format
output format
Definition: mpegvideo.h:173
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:94
int ff_alloc_picture(AVCodecContext *avctx, Picture *pic, MotionEstContext *me, ScratchpadContext *sc, int shared, int encoding, int chroma_x_shift, int chroma_y_shift, int out_format, int mb_stride, int mb_width, int mb_height, int b8_stride, ptrdiff_t *linesize, ptrdiff_t *uvlinesize)
Allocate a Picture.
Definition: mpegvideo.c:585
int ff_mpv_common_frame_size_change(MpegEncContext *s)
Definition: mpegvideo.c:1469
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:269
#define FF_DEBUG_NOMC
Definition: avcodec.h:2592
Multithreading support functions.
#define CODEC_CAP_HWACCEL_VDPAU
Codec can export data for HW decoding (VDPAU).
Definition: avcodec.h:834
Motion estimation context.
Definition: motion_est.h:37
qpel_mc_func(* qpel_put)[16]
Definition: motion_est.h:81
int16_t dst_x
Absolute destination position.
Definition: motion_vector.h:42
#define emms_c()
Definition: internal.h:50
int no_rounding
apply no rounding to motion compensation (MPEG4, msmpeg4, ...) for b-frames rounding mode is always 0...
Definition: mpegvideo.h:351
int interlaced_dct
Definition: mpegvideo.h:542
void ff_mpv_decode_mb(MpegEncContext *s, int16_t block[12][64])
Definition: mpegvideo.c:3203
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:249
int intra_dc_precision
Definition: mpegvideo.h:523
static AVFrame * frame
quarterpel DSP functions
void ff_mpv_common_init_ppc(MpegEncContext *s)
Structure to hold side data for an AVFrame.
Definition: frame.h:134
#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:320
float * cplx_tab
Definition: mpegvideo.h:604
int32_t source
Where the current macroblock comes from; negative value when it comes from the past, positive value when it comes from the future.
Definition: motion_vector.h:30
int ff_thread_ref_frame(ThreadFrame *dst, ThreadFrame *src)
Definition: utils.c:3685
char av_get_picture_type_char(enum AVPictureType pict_type)
Return a single letter to describe the given picture type pict_type.
Definition: utils.c:84
static int alloc_picture_tables(AVCodecContext *avctx, Picture *pic, int encoding, int out_format, int mb_stride, int mb_width, int mb_height, int b8_stride)
Definition: mpegvideo.c:505
void(* decode_mb)(struct MpegEncContext *s)
Called for every Macroblock in a slice.
Definition: avcodec.h:3396
uint16_t pp_time
time distance between the last 2 p,s,i frames
Definition: mpegvideo.h:455
AVBufferRef * mb_type_buf
Definition: mpegvideo.h:99
static int alloc_picture(MpegEncContext *s, Picture *pic, int shared)
Definition: mpegvideo.c:573
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:259
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:367
#define CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:759
av_cold void ff_mpv_idct_init(MpegEncContext *s)
Definition: mpegvideo.c:346
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:198
int lowres
low resolution decoding, 1-> 1/2 size, 2->1/4 size
Definition: avcodec.h:2737
static av_always_inline void mpv_decode_mb_internal(MpegEncContext *s, int16_t block[12][64], int lowres_flag, int is_mpeg12)
Definition: mpegvideo.c:2955
int codec_tag
internal codec_tag upper case converted from avctx codec_tag
Definition: mpegvideo.h:189
high precision timer, useful to profile code
int16_t(*[2][2] p_field_mv_table_base)[2]
Definition: mpegvideo.h:315
#define av_log(a,...)
static void ff_update_block_index(MpegEncContext *s)
Definition: mpegvideo.h:759
#define ff_sqrt
Definition: mathops.h:215
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
Definition: mpegvideo.c:3314
#define ROUNDED_DIV(a, b)
Definition: common.h:55
AVBufferRef * mb_mean_buf
Definition: mpegvideo.h:117
static void gray16(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
Definition: mpegvideo.c:288
int intra_only
if true, only intra pictures are generated
Definition: mpegvideo.h:171
ThreadFrame tf
Definition: mpegvideo.h:91
#define U(x)
Definition: vp56_arith.h:37
int16_t * dc_val[3]
used for mpeg4 DC prediction, all 3 arrays must be continuous
Definition: mpegvideo.h:256
enum AVCodecID id
Definition: avcodec.h:3195
int h263_plus
h263 plus headers
Definition: mpegvideo.h:178
int slice_context_count
number of used thread_contexts
Definition: mpegvideo.h:225
unsigned int buffer_size
Definition: parser.h:32
int width
width and height of the video frame
Definition: frame.h:220
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
uint8_t * mbskip_table
Definition: mpegvideo.h:103
int last_dc[3]
last DC values for MPEG1
Definition: mpegvideo.h:254
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:2893
#define CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
Definition: avcodec.h:763
int mb_skipped
MUST BE SET only during DECODING.
Definition: mpegvideo.h:264
#define ARCH_X86
Definition: config.h:38
int chroma_y_shift
Definition: mpegvideo.h:538
int partitioned_frame
is current frame partitioned
Definition: mpegvideo.h:468
uint8_t * rd_scratchpad
scratchpad for rate distortion mb decision
Definition: mpegvideo.h:142
#define AVERROR(e)
Definition: error.h:43
int frame_skip_threshold
frame skip threshold
Definition: avcodec.h:2408
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
#define CODEC_FLAG2_EXPORT_MVS
Export motion vectors through frame side data.
Definition: avcodec.h:773
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:2118
ERContext er
Definition: mpegvideo.h:610
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2772
int last_lambda_for[5]
last lambda for a specific pict type
Definition: mpegvideo.h:288
uint8_t w
Width and height of the block.
Definition: motion_vector.h:34
int reference
Definition: mpegvideo.h:134
const char * r
Definition: vf_curves.c:107
#define FF_DEBUG_VIS_MV_B_FOR
Definition: avcodec.h:2603
int capabilities
Codec capabilities.
Definition: avcodec.h:3200
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:137
void(* dct_unquantize_mpeg2_inter)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:572
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:566
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1335
#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:2885
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
GLsizei GLsizei * length
Definition: opengl_enc.c:115
static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:52
#define IS_SKIP(a)
Definition: mpegutils.h:77
#define FF_DEBUG_SKIP
Definition: avcodec.h:2578
int quarter_sample
1->qpel, 0->half pel ME/MC
Definition: mpegvideo.h:464
uint16_t * mb_type
Table for candidate MB types for encoding (defines in mpegutils.h)
Definition: mpegvideo.h:358
static void draw_line(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color)
Draw a line from (ex, ey) -> (sx, sy).
Definition: mpegvideo.c:1951
int low_delay
no reordering needed / has no b-frames
Definition: mpegvideo.h:469
uint8_t *[2][2] b_field_select_table
Definition: mpegvideo.h:326
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
void ff_mpv_common_end(MpegEncContext *s)
Definition: mpegvideo.c:1548
#define FFMAX(a, b)
Definition: common.h:64
Libavcodec external API header.
av_cold void ff_mpv_common_init_x86(MpegEncContext *s)
Definition: mpegvideo.c:447
uint8_t * mbintra_table
int * mb_index2xy
void ff_mpeg_flush(AVCodecContext *avctx)
Definition: mpegvideo.c:3283
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:195
int * lambda_table
Definition: mpegvideo.h:277
uint8_t * error_status_table
const uint8_t ff_alternate_horizontal_scan[64]
Definition: mpegvideodata.c:82
AVBufferRef * hwaccel_priv_buf
Definition: mpegvideo.h:120
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:630
#define MAX_THREADS
static void draw_arrow(uint8_t *buf, int sx, int sy, int ex, int ey, int w, int h, int stride, int color, int tail, int direction)
Draw an arrow from (ex, ey) -> (sx, sy).
Definition: mpegvideo.c:2009
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:38
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:241
AVBufferRef * motion_val_buf[2]
Definition: mpegvideo.h:96
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:1478
int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
Definition: mpegvideo.c:2451
int progressive_frame
Definition: mpegvideo.h:540
#define IS_16X8(a)
Definition: mpegutils.h:83
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:242
#define UPDATE_PICTURE(pic)
int top_field_first
Definition: mpegvideo.h:525
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int ff_mpeg_ref_picture(AVCodecContext *avctx, Picture *dst, Picture *src)
Definition: mpegvideo.c:715
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:66
int last_index
Definition: parser.h:31
float y
#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:426
#define ARCH_ARM
Definition: config.h:19
static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:168
Picture new_picture
copy of the source picture structure for encoding.
Definition: mpegvideo.h:243
void ff_mpeg_unref_picture(AVCodecContext *avctx, Picture *pic)
Deallocate a picture.
Definition: mpegvideo.c:648
ret
Definition: avfilter.c:974
int width
picture width / height.
Definition: avcodec.h:1414
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:265
int16_t(*[2] motion_val)[2]
Definition: mpegvideo.h:97
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:253
Picture.
Definition: mpegvideo.h:89
#define FF_CEIL_RSHIFT(a, b)
Definition: common.h:57
int alternate_scan
Definition: mpegvideo.h:529
unsigned int allocated_bitstream_buffer_size
Definition: mpegvideo.h:480
void * hwaccel_picture_private
hardware accelerator private data
Definition: mpegvideo.h:124
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:262
int32_t
Motion vectors exported by some codecs (on demand through the export_mvs flag set in the libavcodec A...
Definition: frame.h:96
#define FFABS(a)
Definition: common.h:61
int16_t(*[2][2][2] b_field_mv_table_base)[2]
Definition: mpegvideo.h:316
int16_t(* b_forw_mv_table_base)[2]
Definition: mpegvideo.h:310
#define AV_RL32
Definition: intreadwrite.h:146
int16_t(*[12] pblocks)[64]
Definition: mpegvideo.h:556
#define CONFIG_GRAY
Definition: config.h:470
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:155
MotionEstContext me
Definition: mpegvideo.h:349
float u
int n
Definition: avisynth_c.h:547
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:94
int mb_decision
macroblock decision mode
Definition: avcodec.h:1777
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:267
void avcodec_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: imgconvert.c:43
#define ME_MAP_SIZE
Definition: mpegvideo.h:68
#define FF_DEBUG_MB_TYPE
Definition: avcodec.h:2569
int ff_mpeg_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
Definition: mpegvideo.c:880
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:107
void ff_mpv_decode_defaults(MpegEncContext *s)
Set the given MpegEncContext to defaults for decoding.
Definition: mpegvideo.c:1054
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2753
int block_index[6]
index to current MB in block based arrays with edges
Definition: mpegvideo.h:360
#define IS_INTRA16x16(a)
Definition: mpegutils.h:72
int * mb_index2xy
mb_index -> mb_x + mb_y*mb_stride
Definition: mpegvideo.h:364
int first_field
is 1 for the first field of a field picture 0 otherwise
Definition: mpegvideo.h:543
void * av_memdup(const void *p, size_t size)
Duplicate the buffer p.
Definition: mem.c:297
int ff_mpv_lowest_referenced_row(MpegEncContext *s, int dir)
find the lowest MB row referenced in the MVs
Definition: mpegvideo.c:2849
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static const int8_t mv[256][2]
Definition: 4xm.c:77
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:232
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:333
int frame_skip_factor
frame skip factor
Definition: avcodec.h:2415
static void clear_context(MpegEncContext *s)
Definition: mpegvideo.c:1230
uint16_t * mc_mb_var
Table for motion compensated MB variances.
Definition: mpegvideo.h:112
AVBufferRef * qscale_table_buf
Definition: mpegvideo.h:93
#define MV_DIR_BACKWARD
Definition: mpegvideo.h:330
int16_t(* b_bidir_forw_mv_table_base)[2]
Definition: mpegvideo.h:312
int coded_picture_number
picture number in bitstream order
Definition: frame.h:274
uint16_t inter_matrix[64]
Definition: mpegvideo.h:369
#define IS_INTERLACED(a)
Definition: mpegutils.h:79
int alloc_mb_height
mb_height used to allocate tables
Definition: mpegvideo.h:115
uint8_t * buffer
Definition: parser.h:29
struct MpegEncContext * thread_context[MAX_THREADS]
Definition: mpegvideo.h:224
AVS_Value src
Definition: avisynth_c.h:482
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
Definition: utils.c:721
void ff_free_picture_tables(Picture *pic)
Definition: mpegvideo.c:485
#define ff_dlog(ctx,...)
Definition: internal.h:54
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:2765
ptrdiff_t linesize
line size, in bytes, may be different from width
Definition: mpegvideo.h:203
enum AVCodecID codec_id
Definition: avcodec.h:1258
BlockDSPContext bdsp
Definition: mpegvideo.h:295
av_cold void ff_blockdsp_init(BlockDSPContext *c, AVCodecContext *avctx)
Definition: blockdsp.c:58
enum AVDiscard skip_idct
Skip IDCT/dequantization for selected frames.
Definition: avcodec.h:2940
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
int debug
debug
Definition: avcodec.h:2565
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:1241
ScanTable intra_scantable
Definition: mpegvideo.h:160
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:261
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:169
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1273
uint8_t * data
Definition: frame.h:136
static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:110
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition: hpeldsp.h:56
#define MV_TYPE_16X8
2 vectors, one per 16x8 block
Definition: mpegvideo.h:335
void * buf
Definition: avisynth_c.h:553
void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
Definition: mpegvideo.c:2444
void ff_print_debug_info2(AVCodecContext *avctx, AVFrame *pict, uint8_t *mbskip_table, uint32_t *mbtype_table, int8_t *qscale_table, int16_t(*motion_val[2])[2], int *low_delay, int mb_width, int mb_height, int mb_stride, int quarter_sample)
Print debugging info for the given picture.
Definition: mpegvideo.c:2067
GLint GLenum type
Definition: opengl_enc.c:105
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
Picture * picture
main picture buffer
Definition: mpegvideo.h:205
AVBufferRef * av_buffer_allocz(int size)
Same as av_buffer_alloc(), except the returned buffer will be initialized to zero.
Definition: buffer.c:82
int progressive_sequence
Definition: mpegvideo.h:517
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2764
int coded_height
Definition: avcodec.h:1424
#define IS_16X16(a)
Definition: mpegutils.h:82
ScanTable intra_h_scantable
Definition: mpegvideo.h:161
op_pixels_func put_no_rnd_pixels_tab[4][4]
Halfpel motion compensation with no rounding (a+b)>>1.
Definition: hpeldsp.h:80
int16_t(*[2][2][2] b_field_mv_table)[2]
MV table (4MV per MB) interlaced b-frame encoding.
Definition: mpegvideo.h:324
uint8_t * cbp_table
used to store cbp, ac_pred for partitioned decoding
Definition: mpegvideo.h:268
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, int size)
Add a new side data to a frame.
Definition: frame.c:584
int closed_gop
MPEG1/2 GOP is closed.
Definition: mpegvideo.h:280
int64_t mc_mb_var_sum
motion compensated MB variance for current frame
Definition: mpegvideo.h:129
#define UPDATE_TABLE(table)
unsigned int avpriv_toupper4(unsigned int x)
Definition: utils.c:3677
struct AVFrame * f
Definition: mpegvideo.h:90
#define IS_8X16(a)
Definition: mpegutils.h:84
int ff_mpeg_framesize_alloc(AVCodecContext *avctx, MotionEstContext *me, ScratchpadContext *sc, int linesize)
Definition: mpegvideo.c:364
int context_initialized
Definition: mpegvideo.h:193
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:117
ptrdiff_t uvlinesize
line size, for chroma in bytes, may be different from width
Definition: mpegvideo.h:204
#define s1
Definition: regdef.h:38
static int ff_h263_round_chroma(int x)
Definition: motion_est.h:91
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:1685
static int add_mb(AVMotionVector *mb, uint32_t mb_type, int dst_x, int dst_y, int src_x, int src_y, int direction)
Definition: mpegvideo.c:2048
int f_code
forward MV resolution
Definition: mpegvideo.h:307
#define COPY(a)
AVCodecContext * avctx
#define MV_DIR_FORWARD
Definition: mpegvideo.h:329
int max_b_frames
max number of b-frames for encoding
Definition: mpegvideo.h:184
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:281
int size
Size of data in bytes.
Definition: buffer.h:93
int h263_pred
use mpeg4/h263 ac/dc predictions
Definition: mpegvideo.h:174
int16_t(* b_bidir_back_mv_table)[2]
MV table (1MV per MB) bidir mode b-frame encoding.
Definition: mpegvideo.h:321
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:462
#define EDGE_WIDTH
Definition: mpegvideo.h:74
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:505
static int init_context_frame(MpegEncContext *s)
Initialize and allocates MpegEncContext fields dependent on the resolution.
Definition: mpegvideo.c:1110
#define IS_PCM(a)
Definition: mpegutils.h:73
static int pic_is_unused(Picture *pic)
Definition: mpegvideo.c:1609
uint8_t *[2] p_field_select_table
Definition: mpegvideo.h:325
int16_t(* b_direct_mv_table)[2]
MV table (1MV per MB) direct mode b-frame encoding.
Definition: mpegvideo.h:322
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
const uint8_t * c_dc_scale_table
qscale -> c_dc_scale table
Definition: mpegvideo.h:258
uint8_t level
Definition: svq3.c:150
qpel_mc_func(* qpel_avg)[16]
Definition: motion_est.h:82
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
Definition: mpegvideo.h:343
int16_t(* b_forw_mv_table)[2]
MV table (1MV per MB) forward mode b-frame encoding.
Definition: mpegvideo.h:318
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
Definition: mpegvideo.h:200
int noise_reduction
noise reduction strength
Definition: avcodec.h:1809
void(* h264_chroma_mc_func)(uint8_t *dst, uint8_t *src, int srcStride, int h, int x, int y)
Definition: h264chroma.h:24
static void dct_unquantize_h263_intra_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:199
MpegEncContext.
Definition: mpegvideo.h:150
Picture * next_picture_ptr
pointer to the next picture (for bidir pred)
Definition: mpegvideo.h:252
int8_t * qscale_table
Definition: mpegvideo.h:94
struct AVCodecContext * avctx
Definition: mpegvideo.h:167
A reference to a data buffer.
Definition: buffer.h:81
discard all non reference
Definition: avcodec.h:665
int ff_find_unused_picture(AVCodecContext *avctx, Picture *picture, int shared)
Definition: mpegvideo.c:1651
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
MpegVideoDSPContext mdsp
Definition: mpegvideo.h:301
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:513
int(* dct_error_sum)[64]
Definition: mpegvideo.h:398
uint64_t flags
Extra flag information.
Definition: motion_vector.h:47
#define FF_MB_DECISION_RD
rate distortion
Definition: avcodec.h:1780
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:199
void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
Definition: mpegvideo.c:1059
AVBufferRef * mbskip_table_buf
Definition: mpegvideo.h:102
const uint8_t ff_default_chroma_qscale_table[32]
Definition: mpegvideodata.c:21
#define CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:738
#define ARCH_ALPHA
Definition: config.h:18
uint8_t * dest[3]
Definition: mpegvideo.h:362
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
Definition: internal.h:129
int shared
Definition: mpegvideo.h:135
static av_cold int dct_init(MpegEncContext *s)
Definition: mpegvideo.c:301
static double c[64]
int last_pict_type
Definition: mpegvideo.h:283
static void dct_unquantize_h263_inter_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:233
int16_t * dc_val[3]
Picture last_picture
copy of the previous picture structure.
Definition: mpegvideo.h:231
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
Picture * last_picture_ptr
pointer to the previous picture.
Definition: mpegvideo.h:251
Bi-dir predicted.
Definition: avutil.h:269
int index
Definition: parser.h:30
int workaround_bugs
Work around bugs in encoders which sometimes cannot be detected automatically.
Definition: avcodec.h:2509
uint8_t * b_scratchpad
scratchpad used for writing into write only buffers
Definition: mpegvideo.h:144
const uint8_t * chroma_qscale_table
qscale -> chroma_qscale (h263)
Definition: mpegvideo.h:259
const uint8_t ff_alternate_vertical_scan[64]
Definition: mpegvideodata.c:93
static void release_unused_pictures(AVCodecContext *avctx, Picture *picture)
Definition: mpegvideo.c:1598
uint32_t * map
map to avoid duplicate evaluations
Definition: motion_est.h:48
int ff_update_duplicate_context(MpegEncContext *dst, MpegEncContext *src)
Definition: mpegvideo.c:852
void(* dct_unquantize_mpeg1_inter)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:568
static int lowres
Definition: ffplay.c:324
H264ChromaContext h264chroma
Definition: mpegvideo.h:297
int16_t(* blocks)[12][64]
Definition: mpegvideo.h:559
#define IS_INTRA(x, y)
h264_chroma_mc_func avg_h264_chroma_pixels_tab[4]
Definition: h264chroma.h:28
int slices
Number of slices.
Definition: avcodec.h:1976
void * priv_data
Definition: avcodec.h:1283
#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:1310
#define IS_INTRA4x4(a)
Definition: mpegutils.h:71
int picture_structure
Definition: mpegvideo.h:521
av_cold void ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: idctdsp.c:29
VideoDSPContext vdsp
Definition: mpegvideo.h:305
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: idctdsp.c:241
#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:372
void ff_mpv_frame_end(MpegEncContext *s)
Definition: mpegvideo.c:1912
#define MV_TYPE_DMV
2 vectors, special mpeg2 Dual Prime Vectors
Definition: mpegvideo.h:337
uint8_t * obmc_scratchpad
Definition: mpegvideo.h:143
int frame_priv_data_size
Size of per-frame hardware accelerator private data.
Definition: avcodec.h:3385
int16_t(* block)[64]
points to one of the following blocks
Definition: mpegvideo.h:558
ParseContext parse_context
Definition: mpegvideo.h:428
static void add_dequant_dct(MpegEncContext *s, int16_t *block, int i, uint8_t *dest, int line_size, int qscale)
Definition: mpegvideo.c:2901
Picture next_picture
copy of the next picture structure.
Definition: mpegvideo.h:237
AVBufferRef * mc_mb_var_buf
Definition: mpegvideo.h:111
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:237
#define IS_ACPRED(a)
Definition: mpegutils.h:90
#define CONFIG_WMV2_DECODER
Definition: config.h:796
static av_always_inline void mpeg_motion_lowres(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int field_based, int bottom_field, int field_select, uint8_t **ref_picture, h264_chroma_mc_func *pix_op, int motion_x, int motion_y, int h, int mb_y)
Definition: mpegvideo.c:2510
int flags2
CODEC_FLAG2_*.
Definition: avcodec.h:1342
#define HAVE_THREADS
Definition: config.h:353
static int init_er(MpegEncContext *s)
Definition: mpegvideo.c:1071
int chroma_qscale
chroma QP
Definition: mpegvideo.h:274
void(* dct_unquantize_mpeg2_intra)(struct MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.h:570
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:2016
void ff_mpv_common_defaults(MpegEncContext *s)
Set the given MpegEncContext to common defaults (same for encoding and decoding). ...
Definition: mpegvideo.c:1031
static void free_context_frame(MpegEncContext *s)
Frees and resets MpegEncContext fields depending on the resolution.
Definition: mpegvideo.c:1420
static int hpel_motion_lowres(MpegEncContext *s, uint8_t *dest, uint8_t *src, int field_based, int field_select, int src_x, int src_y, int width, int height, ptrdiff_t stride, int h_edge_pos, int v_edge_pos, int w, int h, h264_chroma_mc_func *pix_op, int motion_x, int motion_y)
Definition: mpegvideo.c:2463
int height
Definition: frame.h:220
uint16_t intra_matrix[64]
matrix transmitted in the bitstream
Definition: mpegvideo.h:367
uint32_t * mb_type
types and macros are defined in mpegutils.h
Definition: mpegvideo.h:100
#define av_freep(p)
int workaround_bugs
workaround bugs in encoders which cannot be detected automatically
Definition: mpegvideo.h:188
ScanTable inter_scantable
if inter == intra then intra should be used to reduce tha cache usage
Definition: mpegvideo.h:159
#define av_always_inline
Definition: attributes.h:37
uint8_t * temp
Definition: motion_est.h:46
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:170
#define av_malloc_array(a, b)
#define FFSWAP(type, a, b)
Definition: common.h:69
#define stride
int debug_mv
debug Code outside libavcodec should access this field using AVOptions
Definition: avcodec.h:2601
#define MV_TYPE_8X8
4 vectors (h263, mpeg4 4MV)
Definition: mpegvideo.h:334
#define FF_DEBUG_VIS_MV_P_FOR
Definition: avcodec.h:2602
int16_t(* b_direct_mv_table_base)[2]
Definition: mpegvideo.h:314
int b_code
backward MV resolution for B Frames (mpeg4)
Definition: mpegvideo.h:308
float * bits_tab
Definition: mpegvideo.h:604
uint8_t * mbskip_table
int64_t mb_var_sum
sum of MB variance for current frame
Definition: mpegvideo.h:128
void ff_mpv_report_decode_progress(MpegEncContext *s)
Definition: mpegvideo.c:3328
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:969
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:250
static void MPV_motion_lowres(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, int dir, uint8_t **ref_picture, h264_chroma_mc_func *pix_op)
motion compensation of a single macroblock
Definition: mpegvideo.c:2715
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)
Definition: internal.h:138
Predicted.
Definition: avutil.h:268
AVBufferRef * ref_index_buf[2]
Definition: mpegvideo.h:105
HpelDSPContext hdsp
Definition: mpegvideo.h:298
static int width
static int16_t block[64]
Definition: dct-test.c:110