FFmpeg
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"
34 #include "libavutil/mem.h"
35 
36 #include "avcodec.h"
37 #include "blockdsp.h"
38 #include "idctdsp.h"
39 #include "mathops.h"
40 #include "mpeg_er.h"
41 #include "mpegutils.h"
42 #include "mpegvideo.h"
43 #include "mpegvideodata.h"
44 #include "refstruct.h"
45 
47  int16_t *block, int n, int qscale)
48 {
49  int i, level, nCoeffs;
50  const uint16_t *quant_matrix;
51 
52  nCoeffs= s->block_last_index[n];
53 
54  block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
55  /* XXX: only MPEG-1 */
56  quant_matrix = s->intra_matrix;
57  for(i=1;i<=nCoeffs;i++) {
58  int j= s->intra_scantable.permutated[i];
59  level = block[j];
60  if (level) {
61  if (level < 0) {
62  level = -level;
63  level = (int)(level * qscale * quant_matrix[j]) >> 3;
64  level = (level - 1) | 1;
65  level = -level;
66  } else {
67  level = (int)(level * qscale * quant_matrix[j]) >> 3;
68  level = (level - 1) | 1;
69  }
70  block[j] = level;
71  }
72  }
73 }
74 
76  int16_t *block, int n, int qscale)
77 {
78  int i, level, nCoeffs;
79  const uint16_t *quant_matrix;
80 
81  nCoeffs= s->block_last_index[n];
82 
83  quant_matrix = s->inter_matrix;
84  for(i=0; i<=nCoeffs; i++) {
85  int j= s->intra_scantable.permutated[i];
86  level = block[j];
87  if (level) {
88  if (level < 0) {
89  level = -level;
90  level = (((level << 1) + 1) * qscale *
91  ((int) (quant_matrix[j]))) >> 4;
92  level = (level - 1) | 1;
93  level = -level;
94  } else {
95  level = (((level << 1) + 1) * qscale *
96  ((int) (quant_matrix[j]))) >> 4;
97  level = (level - 1) | 1;
98  }
99  block[j] = level;
100  }
101  }
102 }
103 
105  int16_t *block, int n, int qscale)
106 {
107  int i, level, nCoeffs;
108  const uint16_t *quant_matrix;
109 
110  if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
111  else qscale <<= 1;
112 
113  if(s->alternate_scan) nCoeffs= 63;
114  else nCoeffs= s->block_last_index[n];
115 
116  block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
117  quant_matrix = s->intra_matrix;
118  for(i=1;i<=nCoeffs;i++) {
119  int j= s->intra_scantable.permutated[i];
120  level = block[j];
121  if (level) {
122  if (level < 0) {
123  level = -level;
124  level = (int)(level * qscale * quant_matrix[j]) >> 4;
125  level = -level;
126  } else {
127  level = (int)(level * qscale * quant_matrix[j]) >> 4;
128  }
129  block[j] = level;
130  }
131  }
132 }
133 
135  int16_t *block, int n, int qscale)
136 {
137  int i, level, nCoeffs;
138  const uint16_t *quant_matrix;
139  int sum=-1;
140 
141  if (s->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
142  else qscale <<= 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]) >> 4;
157  level = -level;
158  } else {
159  level = (int)(level * qscale * quant_matrix[j]) >> 4;
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->q_scale_type) qscale = ff_mpeg2_non_linear_qscale[qscale];
176  else qscale <<= 1;
177 
178  if(s->alternate_scan) nCoeffs= 63;
179  else nCoeffs= s->block_last_index[n];
180 
181  quant_matrix = s->inter_matrix;
182  for(i=0; i<=nCoeffs; i++) {
183  int j= s->intra_scantable.permutated[i];
184  level = block[j];
185  if (level) {
186  if (level < 0) {
187  level = -level;
188  level = (((level << 1) + 1) * qscale *
189  ((int) (quant_matrix[j]))) >> 5;
190  level = -level;
191  } else {
192  level = (((level << 1) + 1) * qscale *
193  ((int) (quant_matrix[j]))) >> 5;
194  }
195  block[j] = level;
196  sum+=level;
197  }
198  }
199  block[63]^=sum&1;
200 }
201 
203  int16_t *block, int n, int qscale)
204 {
205  int i, level, qmul, qadd;
206  int nCoeffs;
207 
208  av_assert2(s->block_last_index[n]>=0 || s->h263_aic);
209 
210  qmul = qscale << 1;
211 
212  if (!s->h263_aic) {
213  block[0] *= n < 4 ? s->y_dc_scale : s->c_dc_scale;
214  qadd = (qscale - 1) | 1;
215  }else{
216  qadd = 0;
217  }
218  if(s->ac_pred)
219  nCoeffs=63;
220  else
221  nCoeffs= s->intra_scantable.raster_end[ s->block_last_index[n] ];
222 
223  for(i=1; i<=nCoeffs; i++) {
224  level = block[i];
225  if (level) {
226  if (level < 0) {
227  level = level * qmul - qadd;
228  } else {
229  level = level * qmul + qadd;
230  }
231  block[i] = level;
232  }
233  }
234 }
235 
237  int16_t *block, int n, int qscale)
238 {
239  int i, level, qmul, qadd;
240  int nCoeffs;
241 
242  av_assert2(s->block_last_index[n]>=0);
243 
244  qadd = (qscale - 1) | 1;
245  qmul = qscale << 1;
246 
247  nCoeffs= s->inter_scantable.raster_end[ s->block_last_index[n] ];
248 
249  for(i=0; i<=nCoeffs; i++) {
250  level = block[i];
251  if (level) {
252  if (level < 0) {
253  level = level * qmul - qadd;
254  } else {
255  level = level * qmul + qadd;
256  }
257  block[i] = level;
258  }
259  }
260 }
261 
262 
263 static void gray16(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
264 {
265  while(h--)
266  memset(dst + h*linesize, 128, 16);
267 }
268 
269 static void gray8(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
270 {
271  while(h--)
272  memset(dst + h*linesize, 128, 8);
273 }
274 
275 /* init common dct for both encoder and decoder */
277 {
278  ff_blockdsp_init(&s->bdsp);
279  ff_hpeldsp_init(&s->hdsp, s->avctx->flags);
280  ff_videodsp_init(&s->vdsp, s->avctx->bits_per_raw_sample);
281 
282  if (s->avctx->debug & FF_DEBUG_NOMC) {
283  int i;
284  for (i=0; i<4; i++) {
285  s->hdsp.avg_pixels_tab[0][i] = gray16;
286  s->hdsp.put_pixels_tab[0][i] = gray16;
287  s->hdsp.put_no_rnd_pixels_tab[0][i] = gray16;
288 
289  s->hdsp.avg_pixels_tab[1][i] = gray8;
290  s->hdsp.put_pixels_tab[1][i] = gray8;
291  s->hdsp.put_no_rnd_pixels_tab[1][i] = gray8;
292  }
293  }
294 
295  s->dct_unquantize_h263_intra = dct_unquantize_h263_intra_c;
296  s->dct_unquantize_h263_inter = dct_unquantize_h263_inter_c;
297  s->dct_unquantize_mpeg1_intra = dct_unquantize_mpeg1_intra_c;
298  s->dct_unquantize_mpeg1_inter = dct_unquantize_mpeg1_inter_c;
299  s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_c;
300  if (s->avctx->flags & AV_CODEC_FLAG_BITEXACT)
301  s->dct_unquantize_mpeg2_intra = dct_unquantize_mpeg2_intra_bitexact;
302  s->dct_unquantize_mpeg2_inter = dct_unquantize_mpeg2_inter_c;
303 
304 #if HAVE_INTRINSICS_NEON
306 #endif
307 
308 #if ARCH_ARM
310 #elif ARCH_PPC
312 #elif ARCH_X86
314 #elif ARCH_MIPS
316 #endif
317 
318  return 0;
319 }
320 
321 av_cold void ff_init_scantable(const uint8_t *permutation, ScanTable *st,
322  const uint8_t *src_scantable)
323 {
324  int end;
325 
326  st->scantable = src_scantable;
327 
328  for (int i = 0; i < 64; i++) {
329  int j = src_scantable[i];
330  st->permutated[i] = permutation[j];
331  }
332 
333  end = -1;
334  for (int i = 0; i < 64; i++) {
335  int j = st->permutated[i];
336  if (j > end)
337  end = j;
338  st->raster_end[i] = end;
339  }
340 }
341 
343 {
344  if (s->codec_id == AV_CODEC_ID_MPEG4)
345  s->idsp.mpeg4_studio_profile = s->studio_profile;
346  ff_idctdsp_init(&s->idsp, s->avctx);
347 
348  /* load & permutate scantables
349  * note: only wmv uses different ones
350  */
351  if (s->alternate_scan) {
352  ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_alternate_vertical_scan);
353  ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_alternate_vertical_scan);
354  } else {
355  ff_init_scantable(s->idsp.idct_permutation, &s->inter_scantable, ff_zigzag_direct);
356  ff_init_scantable(s->idsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
357  }
358  ff_permute_scantable(s->permutated_intra_h_scantable, ff_alternate_horizontal_scan,
359  s->idsp.idct_permutation);
360  ff_permute_scantable(s->permutated_intra_v_scantable, ff_alternate_vertical_scan,
361  s->idsp.idct_permutation);
362 }
363 
365 {
366  if (s->encoding) {
367  s->me.map = av_mallocz(2 * ME_MAP_SIZE * sizeof(*s->me.map));
368  if (!s->me.map)
369  return AVERROR(ENOMEM);
370  s->me.score_map = s->me.map + ME_MAP_SIZE;
371 
372  if (s->noise_reduction) {
373  if (!FF_ALLOCZ_TYPED_ARRAY(s->dct_error_sum, 2))
374  return AVERROR(ENOMEM);
375  }
376  }
377  if (!FF_ALLOCZ_TYPED_ARRAY(s->blocks, 1 + s->encoding))
378  return AVERROR(ENOMEM);
379  s->block = s->blocks[0];
380 
381  if (s->out_format == FMT_H263) {
382  int mb_height = s->msmpeg4_version == MSMP4_VC1 ?
383  FFALIGN(s->mb_height, 2) : s->mb_height;
384  int y_size = s->b8_stride * (2 * mb_height + 1);
385  int c_size = s->mb_stride * (mb_height + 1);
386  int yc_size = y_size + 2 * c_size;
387  /* ac values */
388  if (!FF_ALLOCZ_TYPED_ARRAY(s->ac_val_base, yc_size))
389  return AVERROR(ENOMEM);
390  s->ac_val[0] = s->ac_val_base + s->b8_stride + 1;
391  s->ac_val[1] = s->ac_val_base + y_size + s->mb_stride + 1;
392  s->ac_val[2] = s->ac_val[1] + c_size;
393  }
394 
395  return 0;
396 }
397 
399 {
400  int nb_slices = s->slice_context_count, ret;
401 
402  /* We initialize the copies before the original so that
403  * fields allocated in init_duplicate_context are NULL after
404  * copying. This prevents double-frees upon allocation error. */
405  for (int i = 1; i < nb_slices; i++) {
406  s->thread_context[i] = av_memdup(s, sizeof(MpegEncContext));
407  if (!s->thread_context[i])
408  return AVERROR(ENOMEM);
409  if ((ret = init_duplicate_context(s->thread_context[i])) < 0)
410  return ret;
411  s->thread_context[i]->start_mb_y =
412  (s->mb_height * (i ) + nb_slices / 2) / nb_slices;
413  s->thread_context[i]->end_mb_y =
414  (s->mb_height * (i + 1) + nb_slices / 2) / nb_slices;
415  }
416  s->start_mb_y = 0;
417  s->end_mb_y = nb_slices > 1 ? (s->mb_height + nb_slices / 2) / nb_slices
418  : s->mb_height;
419  return init_duplicate_context(s);
420 }
421 
423 {
424  if (!s)
425  return;
426 
427  av_freep(&s->sc.edge_emu_buffer);
428  av_freep(&s->sc.scratchpad_buf);
429  s->me.temp = s->me.scratchpad =
430  s->sc.obmc_scratchpad = NULL;
431  s->sc.linesize = 0;
432 
433  av_freep(&s->dct_error_sum);
434  av_freep(&s->me.map);
435  s->me.score_map = NULL;
436  av_freep(&s->blocks);
437  av_freep(&s->ac_val_base);
438  s->block = NULL;
439 }
440 
442 {
443  for (int i = 1; i < s->slice_context_count; i++) {
444  free_duplicate_context(s->thread_context[i]);
445  av_freep(&s->thread_context[i]);
446  }
448 }
449 
451 {
452 #define COPY(a) bak->a = src->a
453  COPY(sc);
454  COPY(me.map);
455  COPY(me.score_map);
456  COPY(blocks);
457  COPY(block);
458  COPY(start_mb_y);
459  COPY(end_mb_y);
460  COPY(me.map_generation);
461  COPY(dct_error_sum);
462  COPY(dct_count[0]);
463  COPY(dct_count[1]);
464  COPY(ac_val_base);
465  COPY(ac_val[0]);
466  COPY(ac_val[1]);
467  COPY(ac_val[2]);
468 #undef COPY
469 }
470 
472 {
473  MpegEncContext bak;
474  int ret;
475  // FIXME copy only needed parts
476  backup_duplicate_context(&bak, dst);
477  memcpy(dst, src, sizeof(MpegEncContext));
478  backup_duplicate_context(dst, &bak);
479 
480  ret = ff_mpv_framesize_alloc(dst->avctx, &dst->sc, dst->linesize);
481  if (ret < 0) {
482  av_log(dst->avctx, AV_LOG_ERROR, "failed to allocate context "
483  "scratch buffers.\n");
484  return ret;
485  }
486  return 0;
487 }
488 
489 /**
490  * Set the given MpegEncContext to common defaults
491  * (same for encoding and decoding).
492  * The changed fields will not depend upon the
493  * prior state of the MpegEncContext.
494  */
496 {
497  s->y_dc_scale_table =
498  s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
499  s->chroma_qscale_table = ff_default_chroma_qscale_table;
500  s->progressive_frame = 1;
501  s->progressive_sequence = 1;
502  s->picture_structure = PICT_FRAME;
503 
504  s->picture_number = 0;
505 
506  s->f_code = 1;
507  s->b_code = 1;
508 
509  s->slice_context_count = 1;
510 }
511 
513 {
519  pools->alloc_mb_height = pools->alloc_mb_width = pools->alloc_mb_stride = 0;
520 }
521 
523 {
524  BufferPoolContext *const pools = &s->buffer_pools;
525  int y_size, c_size, yc_size, i, mb_array_size, mv_table_size, x, y;
526  int mb_height;
527 
528  if (s->codec_id == AV_CODEC_ID_MPEG2VIDEO && !s->progressive_sequence)
529  s->mb_height = (s->height + 31) / 32 * 2;
530  else
531  s->mb_height = (s->height + 15) / 16;
532 
533  /* VC-1 can change from being progressive to interlaced on a per-frame
534  * basis. We therefore allocate certain buffers so big that they work
535  * in both instances. */
536  mb_height = s->msmpeg4_version == MSMP4_VC1 ?
537  FFALIGN(s->mb_height, 2) : s->mb_height;
538 
539  s->mb_width = (s->width + 15) / 16;
540  s->mb_stride = s->mb_width + 1;
541  s->b8_stride = s->mb_width * 2 + 1;
542  mb_array_size = mb_height * s->mb_stride;
543  mv_table_size = (mb_height + 2) * s->mb_stride + 1;
544 
545  /* set default edge pos, will be overridden
546  * in decode_header if needed */
547  s->h_edge_pos = s->mb_width * 16;
548  s->v_edge_pos = s->mb_height * 16;
549 
550  s->mb_num = s->mb_width * s->mb_height;
551 
552  s->block_wrap[0] =
553  s->block_wrap[1] =
554  s->block_wrap[2] =
555  s->block_wrap[3] = s->b8_stride;
556  s->block_wrap[4] =
557  s->block_wrap[5] = s->mb_stride;
558 
559  y_size = s->b8_stride * (2 * mb_height + 1);
560  c_size = s->mb_stride * (mb_height + 1);
561  yc_size = y_size + 2 * c_size;
562 
563  if (!FF_ALLOCZ_TYPED_ARRAY(s->mb_index2xy, s->mb_num + 1))
564  return AVERROR(ENOMEM);
565  for (y = 0; y < s->mb_height; y++)
566  for (x = 0; x < s->mb_width; x++)
567  s->mb_index2xy[x + y * s->mb_width] = x + y * s->mb_stride;
568 
569  s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed?
570 
571 #define ALLOC_POOL(name, size, flags) do { \
572  pools->name ##_pool = ff_refstruct_pool_alloc((size), (flags)); \
573  if (!pools->name ##_pool) \
574  return AVERROR(ENOMEM); \
575 } while (0)
576 
577  if (s->codec_id == AV_CODEC_ID_MPEG4 ||
578  (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) {
579  /* interlaced direct mode decoding tables */
580  int16_t (*tmp)[2] = av_calloc(mv_table_size, 4 * sizeof(*tmp));
581  if (!tmp)
582  return AVERROR(ENOMEM);
583  s->p_field_mv_table_base = tmp;
584  tmp += s->mb_stride + 1;
585  for (int i = 0; i < 2; i++) {
586  for (int j = 0; j < 2; j++) {
587  s->p_field_mv_table[i][j] = tmp;
588  tmp += mv_table_size;
589  }
590  }
591  if (s->codec_id == AV_CODEC_ID_MPEG4) {
592  ALLOC_POOL(mbskip_table, mb_array_size + 2,
593  !s->encoding ? FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME : 0);
594  if (!s->encoding) {
595  /* cbp, pred_dir */
596  if (!(s->cbp_table = av_mallocz(mb_array_size)) ||
597  !(s->pred_dir_table = av_mallocz(mb_array_size)))
598  return AVERROR(ENOMEM);
599  }
600  }
601  }
602 
603  if (s->msmpeg4_version >= MSMP4_V3) {
604  s->coded_block_base = av_mallocz(y_size);
605  if (!s->coded_block_base)
606  return AVERROR(ENOMEM);
607  s->coded_block = s->coded_block_base + s->b8_stride + 1;
608  }
609 
610  if (s->h263_pred || s->h263_plus || !s->encoding) {
611  /* dc values */
612  // MN: we need these for error resilience of intra-frames
613  if (!FF_ALLOCZ_TYPED_ARRAY(s->dc_val_base, yc_size))
614  return AVERROR(ENOMEM);
615  s->dc_val[0] = s->dc_val_base + s->b8_stride + 1;
616  s->dc_val[1] = s->dc_val_base + y_size + s->mb_stride + 1;
617  s->dc_val[2] = s->dc_val[1] + c_size;
618  for (i = 0; i < yc_size; i++)
619  s->dc_val_base[i] = 1024;
620  }
621 
622  // Note the + 1 is for a quicker MPEG-4 slice_end detection
623  if (!(s->mbskip_table = av_mallocz(mb_array_size + 2)) ||
624  /* which mb is an intra block, init macroblock skip table */
625  !(s->mbintra_table = av_malloc(mb_array_size)))
626  return AVERROR(ENOMEM);
627  memset(s->mbintra_table, 1, mb_array_size);
628 
629  ALLOC_POOL(qscale_table, mv_table_size, 0);
630  ALLOC_POOL(mb_type, mv_table_size * sizeof(uint32_t), 0);
631 
632  if (s->out_format == FMT_H263 || s->encoding ||
633  (s->avctx->export_side_data & AV_CODEC_EXPORT_DATA_MVS)) {
634  const int b8_array_size = s->b8_stride * mb_height * 2;
635  int mv_size = 2 * (b8_array_size + 4) * sizeof(int16_t);
636  int ref_index_size = 4 * mb_array_size;
637 
638  /* FIXME: The output of H.263 with OBMC depends upon
639  * the earlier content of the buffer; therefore we set
640  * the flags to always reset returned buffers here. */
642  ALLOC_POOL(ref_index, ref_index_size, 0);
643  }
644 #undef ALLOC_POOL
645  pools->alloc_mb_width = s->mb_width;
646  pools->alloc_mb_height = mb_height;
647  pools->alloc_mb_stride = s->mb_stride;
648 
649  return !CONFIG_MPEGVIDEODEC || s->encoding ? 0 : ff_mpeg_er_init(s);
650 }
651 
653 {
654  memset(&s->buffer_pools, 0, sizeof(s->buffer_pools));
655  memset(&s->next_pic, 0, sizeof(s->next_pic));
656  memset(&s->last_pic, 0, sizeof(s->last_pic));
657  memset(&s->cur_pic, 0, sizeof(s->cur_pic));
658 
659  memset(s->thread_context, 0, sizeof(s->thread_context));
660 
661  s->me.map = NULL;
662  s->me.score_map = NULL;
663  s->dct_error_sum = NULL;
664  s->block = NULL;
665  s->blocks = NULL;
666  s->ac_val_base = NULL;
667  s->ac_val[0] =
668  s->ac_val[1] =
669  s->ac_val[2] =NULL;
670  s->me.scratchpad = NULL;
671  s->me.temp = NULL;
672  memset(&s->sc, 0, sizeof(s->sc));
673 
674 
675  s->bitstream_buffer = NULL;
676  s->allocated_bitstream_buffer_size = 0;
677  s->p_field_mv_table_base = NULL;
678  for (int i = 0; i < 2; i++)
679  for (int j = 0; j < 2; j++)
680  s->p_field_mv_table[i][j] = NULL;
681 
682  s->dc_val_base = NULL;
683  s->coded_block_base = NULL;
684  s->mbintra_table = NULL;
685  s->cbp_table = NULL;
686  s->pred_dir_table = NULL;
687 
688  s->mbskip_table = NULL;
689 
690  s->er.error_status_table = NULL;
691  s->er.er_temp_buffer = NULL;
692  s->mb_index2xy = NULL;
693 }
694 
695 /**
696  * init common structure for both encoder and decoder.
697  * this assumes that some variables like width/height are already set
698  */
700 {
701  int nb_slices = (HAVE_THREADS &&
702  s->avctx->active_thread_type & FF_THREAD_SLICE) ?
703  s->avctx->thread_count : 1;
704  int ret;
705 
706  clear_context(s);
707 
708  if (s->encoding && s->avctx->slices)
709  nb_slices = s->avctx->slices;
710 
711  if (s->avctx->pix_fmt == AV_PIX_FMT_NONE) {
712  av_log(s->avctx, AV_LOG_ERROR,
713  "decoding to AV_PIX_FMT_NONE is not supported.\n");
714  return AVERROR(EINVAL);
715  }
716 
717  if ((s->width || s->height) &&
718  av_image_check_size(s->width, s->height, 0, s->avctx))
719  return AVERROR(EINVAL);
720 
721  dct_init(s);
722 
723  /* set chroma shifts */
724  ret = av_pix_fmt_get_chroma_sub_sample(s->avctx->pix_fmt,
725  &s->chroma_x_shift,
726  &s->chroma_y_shift);
727  if (ret)
728  return ret;
729 
731  goto fail;
732 
733  if (nb_slices > MAX_THREADS || (nb_slices > s->mb_height && s->mb_height)) {
734  int max_slices;
735  if (s->mb_height)
736  max_slices = FFMIN(MAX_THREADS, s->mb_height);
737  else
738  max_slices = MAX_THREADS;
739  av_log(s->avctx, AV_LOG_WARNING, "too many threads/slices (%d),"
740  " reducing to %d\n", nb_slices, max_slices);
741  nb_slices = max_slices;
742  }
743 
744  s->context_initialized = 1;
745  memset(s->thread_context, 0, sizeof(s->thread_context));
746  s->thread_context[0] = s;
747  s->slice_context_count = nb_slices;
748 
749 // if (s->width && s->height) {
751  if (ret < 0)
752  goto fail;
753 // }
754 
755  return 0;
756  fail:
758  return ret;
759 }
760 
762 {
764 
765  free_buffer_pools(&s->buffer_pools);
766  av_freep(&s->p_field_mv_table_base);
767  for (int i = 0; i < 2; i++)
768  for (int j = 0; j < 2; j++)
769  s->p_field_mv_table[i][j] = NULL;
770 
771  av_freep(&s->dc_val_base);
772  av_freep(&s->coded_block_base);
773  av_freep(&s->mbintra_table);
774  av_freep(&s->cbp_table);
775  av_freep(&s->pred_dir_table);
776 
777  av_freep(&s->mbskip_table);
778 
779  av_freep(&s->er.error_status_table);
780  av_freep(&s->er.er_temp_buffer);
781  av_freep(&s->mb_index2xy);
782 
783  s->linesize = s->uvlinesize = 0;
784 }
785 
787 {
789  if (s->slice_context_count > 1)
790  s->slice_context_count = 1;
791 
792  av_freep(&s->bitstream_buffer);
793  s->allocated_bitstream_buffer_size = 0;
794 
795  ff_mpv_unref_picture(&s->last_pic);
796  ff_mpv_unref_picture(&s->cur_pic);
797  ff_mpv_unref_picture(&s->next_pic);
798 
799  s->context_initialized = 0;
800  s->context_reinit = 0;
801  s->linesize = s->uvlinesize = 0;
802 }
803 
804 
805 /**
806  * Clean dc, ac for the current non-intra MB.
807  */
809 {
810  int wrap = s->b8_stride;
811  int xy = s->block_index[0];
812 
813  s->dc_val[0][xy ] =
814  s->dc_val[0][xy + 1 ] =
815  s->dc_val[0][xy + wrap] =
816  s->dc_val[0][xy + 1 + wrap] = 1024;
817  /* ac pred */
818  memset(s->ac_val[0][xy ], 0, 32 * sizeof(int16_t));
819  memset(s->ac_val[0][xy + wrap], 0, 32 * sizeof(int16_t));
820  /* chroma */
821  wrap = s->mb_stride;
822  xy = s->mb_x + s->mb_y * wrap;
823  s->dc_val[1][xy] =
824  s->dc_val[2][xy] = 1024;
825  /* ac pred */
826  memset(s->ac_val[1][xy], 0, 16 * sizeof(int16_t));
827  memset(s->ac_val[2][xy], 0, 16 * sizeof(int16_t));
828 
829  s->mbintra_table[xy]= 0;
830 }
831 
832 void ff_init_block_index(MpegEncContext *s){ //FIXME maybe rename
833  const int linesize = s->cur_pic.linesize[0]; //not s->linesize as this would be wrong for field pics
834  const int uvlinesize = s->cur_pic.linesize[1];
835  const int width_of_mb = (4 + (s->avctx->bits_per_raw_sample > 8)) - s->avctx->lowres;
836  const int height_of_mb = 4 - s->avctx->lowres;
837 
838  s->block_index[0]= s->b8_stride*(s->mb_y*2 ) - 2 + s->mb_x*2;
839  s->block_index[1]= s->b8_stride*(s->mb_y*2 ) - 1 + s->mb_x*2;
840  s->block_index[2]= s->b8_stride*(s->mb_y*2 + 1) - 2 + s->mb_x*2;
841  s->block_index[3]= s->b8_stride*(s->mb_y*2 + 1) - 1 + s->mb_x*2;
842  s->block_index[4]= s->mb_stride*(s->mb_y + 1) + s->b8_stride*s->mb_height*2 + s->mb_x - 1;
843  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;
844  //block_index is not used by mpeg2, so it is not affected by chroma_format
845 
846  s->dest[0] = s->cur_pic.data[0] + (int)((s->mb_x - 1U) << width_of_mb);
847  s->dest[1] = s->cur_pic.data[1] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
848  s->dest[2] = s->cur_pic.data[2] + (int)((s->mb_x - 1U) << (width_of_mb - s->chroma_x_shift));
849 
850  if (s->picture_structure == PICT_FRAME) {
851  s->dest[0] += s->mb_y * linesize << height_of_mb;
852  s->dest[1] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
853  s->dest[2] += s->mb_y * uvlinesize << (height_of_mb - s->chroma_y_shift);
854  } else {
855  s->dest[0] += (s->mb_y>>1) * linesize << height_of_mb;
856  s->dest[1] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
857  s->dest[2] += (s->mb_y>>1) * uvlinesize << (height_of_mb - s->chroma_y_shift);
858  av_assert1((s->mb_y&1) == (s->picture_structure == PICT_BOTTOM_FIELD));
859  }
860 }
861 
862 /**
863  * set qscale and update qscale dependent variables.
864  */
865 void ff_set_qscale(MpegEncContext * s, int qscale)
866 {
867  if (qscale < 1)
868  qscale = 1;
869  else if (qscale > 31)
870  qscale = 31;
871 
872  s->qscale = qscale;
873  s->chroma_qscale= s->chroma_qscale_table[qscale];
874 
875  s->y_dc_scale= s->y_dc_scale_table[ qscale ];
876  s->c_dc_scale= s->c_dc_scale_table[ s->chroma_qscale ];
877 }
FF_ALLOCZ_TYPED_ARRAY
#define FF_ALLOCZ_TYPED_ARRAY(p, nelem)
Definition: internal.h:78
PICT_FRAME
#define PICT_FRAME
Definition: mpegutils.h:33
ff_mpv_common_init
av_cold int ff_mpv_common_init(MpegEncContext *s)
init common structure for both encoder and decoder.
Definition: mpegvideo.c:699
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
free_duplicate_contexts
static void free_duplicate_contexts(MpegEncContext *s)
Definition: mpegvideo.c:441
level
uint8_t level
Definition: svq3.c:205
blockdsp.h
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ff_mpv_init_context_frame
int ff_mpv_init_context_frame(MpegEncContext *s)
Initialize and allocates MpegEncContext fields dependent on the resolution.
Definition: mpegvideo.c:522
backup_duplicate_context
static void backup_duplicate_context(MpegEncContext *bak, MpegEncContext *src)
Definition: mpegvideo.c:450
ff_mpv_common_defaults
void ff_mpv_common_defaults(MpegEncContext *s)
Set the given MpegEncContext to common defaults (same for encoding and decoding).
Definition: mpegvideo.c:495
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
ff_update_duplicate_context
int ff_update_duplicate_context(MpegEncContext *dst, const MpegEncContext *src)
Definition: mpegvideo.c:471
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
ff_mpeg2_non_linear_qscale
const uint8_t ff_mpeg2_non_linear_qscale[32]
Definition: mpegvideodata.c:26
ff_clean_intra_table_entries
void ff_clean_intra_table_entries(MpegEncContext *s)
Clean dc, ac for the current non-intra MB.
Definition: mpegvideo.c:808
PICT_BOTTOM_FIELD
#define PICT_BOTTOM_FIELD
Definition: mpegutils.h:32
init_duplicate_context
static int init_duplicate_context(MpegEncContext *s)
Definition: mpegvideo.c:364
ff_mpv_common_init_arm
av_cold void ff_mpv_common_init_arm(MpegEncContext *s)
Definition: mpegvideo_arm.c:48
ff_init_block_index
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:832
mpegvideo.h
MpegEncContext::avctx
struct AVCodecContext * avctx
Definition: mpegvideo.h:91
AV_CODEC_FLAG_INTERLACED_ME
#define AV_CODEC_FLAG_INTERLACED_ME
interlaced motion estimation
Definition: avcodec.h:351
mpegutils.h
ff_idctdsp_init
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: idctdsp.c:228
free_duplicate_context
static void free_duplicate_context(MpegEncContext *s)
Definition: mpegvideo.c:422
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:304
ff_permute_scantable
av_cold void ff_permute_scantable(uint8_t dst[64], const uint8_t src[64], const uint8_t permutation[64])
Definition: idctdsp.c:30
dct_unquantize_mpeg1_inter_c
static void dct_unquantize_mpeg1_inter_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:75
fail
#define fail()
Definition: checkasm.h:185
ff_refstruct_pool_uninit
static void ff_refstruct_pool_uninit(FFRefStructPool **poolp)
Mark the pool as being available for freeing.
Definition: refstruct.h:292
wrap
#define wrap(func)
Definition: neontest.h:65
MpegEncContext::linesize
ptrdiff_t linesize
line size, in bytes, may be different from width
Definition: mpegvideo.h:129
MAX_THREADS
#define MAX_THREADS
Definition: frame_thread_encoder.c:37
av_pix_fmt_get_chroma_sub_sample
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:2993
ff_videodsp_init
av_cold void ff_videodsp_init(VideoDSPContext *ctx, int bpc)
Definition: videodsp.c:39
BufferPoolContext::mb_type_pool
struct FFRefStructPool * mb_type_pool
Definition: mpegpicture.h:47
refstruct.h
dct_unquantize_mpeg1_intra_c
static void dct_unquantize_mpeg1_intra_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:46
BufferPoolContext::alloc_mb_stride
int alloc_mb_stride
mb_stride used to allocate tables
Definition: mpegpicture.h:52
ff_mpv_common_end
void ff_mpv_common_end(MpegEncContext *s)
Definition: mpegvideo.c:786
avassert.h
gray16
static void gray16(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
Definition: mpegvideo.c:263
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
ff_blockdsp_init
av_cold void ff_blockdsp_init(BlockDSPContext *c)
Definition: blockdsp.c:58
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_mpv_framesize_alloc
int ff_mpv_framesize_alloc(AVCodecContext *avctx, ScratchpadContext *sc, int linesize)
Definition: mpegpicture.c:138
ff_mpeg1_dc_scale_table
static const uint8_t *const ff_mpeg1_dc_scale_table
Definition: mpegvideodata.h:32
dct_unquantize_mpeg2_intra_bitexact
static void dct_unquantize_mpeg2_intra_bitexact(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:134
ALLOC_POOL
#define ALLOC_POOL(name, size, flags)
ScanTable::scantable
const uint8_t * scantable
Definition: mpegvideo.h:57
BufferPoolContext::motion_val_pool
struct FFRefStructPool * motion_val_pool
Definition: mpegpicture.h:48
BufferPoolContext::ref_index_pool
struct FFRefStructPool * ref_index_pool
Definition: mpegpicture.h:49
BufferPoolContext::mbskip_table_pool
struct FFRefStructPool * mbskip_table_pool
Definition: mpegpicture.h:45
BufferPoolContext::alloc_mb_height
int alloc_mb_height
mb_height used to allocate tables
Definition: mpegpicture.h:51
ff_hpeldsp_init
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition: hpeldsp.c:338
FMT_H263
@ FMT_H263
Definition: mpegvideo.h:65
ff_mpv_unref_picture
void ff_mpv_unref_picture(MPVWorkPicture *pic)
Definition: mpegpicture.c:98
NULL
#define NULL
Definition: coverity.c:32
ff_mpv_idct_init
av_cold void ff_mpv_idct_init(MpegEncContext *s)
Definition: mpegvideo.c:342
me
#define me
Definition: vf_colormatrix.c:102
ff_set_qscale
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
Definition: mpegvideo.c:865
mathops.h
ff_alternate_horizontal_scan
const uint8_t ff_alternate_horizontal_scan[64]
Definition: mpegvideodata.c:52
ME_MAP_SIZE
#define ME_MAP_SIZE
Definition: motion_est.h:38
free_buffer_pools
static void free_buffer_pools(BufferPoolContext *pools)
Definition: mpegvideo.c:512
dct_unquantize_mpeg2_intra_c
static void dct_unquantize_mpeg2_intra_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:104
ff_mpeg_er_init
int ff_mpeg_er_init(MpegEncContext *s)
Definition: mpeg_er.c:103
FF_THREAD_SLICE
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:1595
mpegvideodata.h
attributes.h
dct_unquantize_mpeg2_inter_c
static void dct_unquantize_mpeg2_inter_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:168
clear_context
static void clear_context(MpegEncContext *s)
Definition: mpegvideo.c:652
ff_init_scantable
av_cold void ff_init_scantable(const uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable)
Definition: mpegvideo.c:321
FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME
#define FF_REFSTRUCT_POOL_FLAG_ZERO_EVERY_TIME
If this flag is set, the entries will be zeroed before being returned to the user (after the init or ...
Definition: refstruct.h:221
BufferPoolContext::qscale_table_pool
struct FFRefStructPool * qscale_table_pool
Definition: mpegpicture.h:46
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:67
dct_unquantize_h263_inter_c
static void dct_unquantize_h263_inter_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:236
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ff_alternate_vertical_scan
const uint8_t ff_alternate_vertical_scan[64]
Definition: mpegvideodata.c:63
internal.h
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
ff_mpv_common_init_ppc
void ff_mpv_common_init_ppc(MpegEncContext *s)
Definition: mpegvideo_altivec.c:119
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
MpegEncContext::sc
ScratchpadContext sc
Definition: mpegvideo.h:197
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
FF_DEBUG_NOMC
#define FF_DEBUG_NOMC
Definition: avcodec.h:1411
idctdsp.h
avcodec.h
ff_zigzag_direct
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
ret
ret
Definition: filter_design.txt:187
dct_init
static av_cold int dct_init(MpegEncContext *s)
Definition: mpegvideo.c:276
U
#define U(x)
Definition: vpx_arith.h:37
ff_mpv_free_context_frame
void ff_mpv_free_context_frame(MpegEncContext *s)
Frees and resets MpegEncContext fields depending on the resolution as well as the slice thread contex...
Definition: mpegvideo.c:761
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
dct_unquantize_h263_intra_c
static void dct_unquantize_h263_intra_c(MpegEncContext *s, int16_t *block, int n, int qscale)
Definition: mpegvideo.c:202
ff_mpv_common_init_x86
av_cold void ff_mpv_common_init_x86(MpegEncContext *s)
Definition: mpegvideo.c:454
ff_mpv_common_init_mips
av_cold void ff_mpv_common_init_mips(MpegEncContext *s)
Definition: mpegvideo_init_mips.c:26
ff_default_chroma_qscale_table
const uint8_t ff_default_chroma_qscale_table[32]
Definition: mpegvideodata.c:21
mem.h
AV_CODEC_EXPORT_DATA_MVS
#define AV_CODEC_EXPORT_DATA_MVS
Export motion vectors through frame side data.
Definition: avcodec.h:406
ff_mpv_init_duplicate_contexts
int ff_mpv_init_duplicate_contexts(MpegEncContext *s)
Initialize an MpegEncContext's thread contexts.
Definition: mpegvideo.c:398
AV_CODEC_FLAG_BITEXACT
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
Definition: avcodec.h:342
COPY
#define COPY(a)
ScanTable
Scantable.
Definition: mpegvideo.h:56
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
ScanTable::permutated
uint8_t permutated[64]
Definition: mpegvideo.h:58
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
mpeg_er.h
imgutils.h
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
BufferPoolContext
Definition: mpegpicture.h:44
h
h
Definition: vp9dsp_template.c:2038
av_image_check_size
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:318
BufferPoolContext::alloc_mb_width
int alloc_mb_width
mb_width used to allocate tables
Definition: mpegpicture.h:50
int
int
Definition: ffmpeg_filter.c:424
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:54
ff_mpv_common_init_neon
av_cold void ff_mpv_common_init_neon(MpegEncContext *s)
Definition: mpegvideo.c:127
MpegEncContext
MpegEncContext.
Definition: mpegvideo.h:73
gray8
static void gray8(uint8_t *dst, const uint8_t *src, ptrdiff_t linesize, int h)
Definition: mpegvideo.c:269
ScanTable::raster_end
uint8_t raster_end[64]
Definition: mpegvideo.h:59