FFmpeg
mv30.c
Go to the documentation of this file.
1 /*
2  * MidiVid MV30 decoder
3  *
4  * Copyright (c) 2020 Paul B Mahol
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <stddef.h>
24 #include <string.h>
25 
26 #include "libavutil/thread.h"
27 
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "codec_internal.h"
31 #include "copy_block.h"
32 #include "decode.h"
33 #include "mathops.h"
34 #include "blockdsp.h"
35 #include "get_bits.h"
36 #include "aandcttab.h"
37 
38 #define CBP_VLC_BITS 9
39 
40 typedef struct MV30Context {
42 
45  int is_inter;
46  int mode_size;
48 
49  int block[6][64];
50  int16_t *mvectors;
51  unsigned int mvectors_size;
52  int16_t *coeffs;
53  unsigned int coeffs_size;
54 
55  int16_t intraq_tab[2][64];
56  int16_t interq_tab[2][64];
57 
60 } MV30Context;
61 
62 static VLC cbp_tab;
63 
64 static const uint8_t luma_tab[] = {
65  12, 12, 15, 19, 25, 34, 40, 48,
66  12, 12, 18, 22, 27, 44, 47, 46,
67  17, 18, 21, 26, 35, 46, 52, 47,
68  18, 20, 24, 28, 40, 61, 59, 51,
69  20, 24, 32, 43, 50, 72, 72, 63,
70  25, 31, 42, 48, 58, 72, 81, 75,
71  38, 46, 54, 61, 71, 84, 88, 85,
72  50, 61, 65, 68, 79, 78, 86, 91,
73 };
74 
75 static const uint8_t chroma_tab[] = {
76  12, 16, 24, 47, 99, 99, 99, 99,
77  16, 21, 26, 66, 99, 99, 99, 99,
78  24, 26, 56, 99, 99, 99, 99, 99,
79  47, 66, 99, 99, 99, 99, 99, 99,
80  99, 99, 99, 99, 99, 99, 99, 99,
81  99, 99, 99, 99, 99, 99, 99, 99,
82  99, 99, 99, 99, 99, 99, 99, 99,
83  99, 99, 99, 99, 99, 99, 99, 99,
84 };
85 
86 static const uint8_t zigzag[] = {
87  0, 1, 8, 9, 16, 2, 3, 10,
88  17, 24, 32, 25, 18, 11, 4, 5,
89  12, 19, 26, 33, 40, 48, 41, 34,
90  27, 20, 13, 6, 7, 14, 21, 28,
91  35, 42, 49, 56, 57, 50, 43, 36,
92  29, 22, 15, 23, 30, 37, 44, 51,
93  58, 59, 52, 45, 38, 31, 39, 46,
94  53, 60, 61, 54, 47, 55, 62, 63,
95 };
96 
97 static void get_qtable(int16_t *table, int quant, const uint8_t *quant_tab)
98 {
99  int factor = quant < 50 ? 5000 / FFMAX(quant, 1) : 200 - FFMIN(quant, 100) * 2;
100 
101  for (int i = 0; i < 64; i++) {
102  table[i] = av_clip((quant_tab[i] * factor + 0x32) / 100, 1, 0x7fff);
103  table[i] = ((int)ff_aanscales[i] * (int)table[i] + 0x800) >> 12;
104  }
105 }
106 
107 static inline void idct_1d(unsigned *blk, int step)
108 {
109  const unsigned t0 = blk[0 * step] + blk[4 * step];
110  const unsigned t1 = blk[0 * step] - blk[4 * step];
111  const unsigned t2 = blk[2 * step] + blk[6 * step];
112  const unsigned t3 = ((int)((blk[2 * step] - blk[6 * step]) * 362U) >> 8) - t2;
113  const unsigned t4 = t0 + t2;
114  const unsigned t5 = t0 - t2;
115  const unsigned t6 = t1 + t3;
116  const unsigned t7 = t1 - t3;
117  const unsigned t8 = blk[5 * step] + blk[3 * step];
118  const unsigned t9 = blk[5 * step] - blk[3 * step];
119  const unsigned tA = blk[1 * step] + blk[7 * step];
120  const unsigned tB = blk[1 * step] - blk[7 * step];
121  const unsigned tC = t8 + tA;
122  const unsigned tD = (int)((tB + t9) * 473U) >> 8;
123  const unsigned tE = (((int)(t9 * -669U) >> 8) - tC) + tD;
124  const unsigned tF = ((int)((tA - t8) * 362U) >> 8) - tE;
125  const unsigned t10 = (((int)(tB * 277U) >> 8) - tD) + tF;
126 
127  blk[0 * step] = t4 + tC;
128  blk[1 * step] = t6 + tE;
129  blk[2 * step] = t7 + tF;
130  blk[3 * step] = t5 - t10;
131  blk[4 * step] = t5 + t10;
132  blk[5 * step] = t7 - tF;
133  blk[6 * step] = t6 - tE;
134  blk[7 * step] = t4 - tC;
135 }
136 
137 static void idct_put(uint8_t *dst, int stride, int *block)
138 {
139  for (int i = 0; i < 8; i++) {
140  if ((block[0x08 + i] |
141  block[0x10 + i] |
142  block[0x18 + i] |
143  block[0x20 + i] |
144  block[0x28 + i] |
145  block[0x30 + i] |
146  block[0x38 + i]) == 0) {
147  block[0x08 + i] = block[i];
148  block[0x10 + i] = block[i];
149  block[0x18 + i] = block[i];
150  block[0x20 + i] = block[i];
151  block[0x28 + i] = block[i];
152  block[0x30 + i] = block[i];
153  block[0x38 + i] = block[i];
154  } else {
155  idct_1d(block + i, 8);
156  }
157  }
158 
159  for (int i = 0; i < 8; i++) {
160  idct_1d(block, 1);
161  for (int j = 0; j < 8; j++)
162  dst[j] = av_clip_uint8((block[j] >> 5) + 128);
163  block += 8;
164  dst += stride;
165  }
166 }
167 
168 static void idct_add(uint8_t *dst, int stride,
169  const uint8_t *src, int in_linesize, int *block)
170 {
171  for (int i = 0; i < 8; i++) {
172  if ((block[0x08 + i] |
173  block[0x10 + i] |
174  block[0x18 + i] |
175  block[0x20 + i] |
176  block[0x28 + i] |
177  block[0x30 + i] |
178  block[0x38 + i]) == 0) {
179  block[0x08 + i] = block[i];
180  block[0x10 + i] = block[i];
181  block[0x18 + i] = block[i];
182  block[0x20 + i] = block[i];
183  block[0x28 + i] = block[i];
184  block[0x30 + i] = block[i];
185  block[0x38 + i] = block[i];
186  } else {
187  idct_1d(block + i, 8);
188  }
189  }
190 
191  for (int i = 0; i < 8; i++) {
192  idct_1d(block, 1);
193  for (int j = 0; j < 8; j++)
194  dst[j] = av_clip_uint8((block[j] >> 5) + src[j]);
195  block += 8;
196  dst += stride;
197  src += in_linesize;
198  }
199 }
200 
201 static inline void idct2_1d(int *blk, int step)
202 {
203  const unsigned int t0 = blk[0 * step];
204  const unsigned int t1 = blk[1 * step];
205  const unsigned int t2 = (int)(t1 * 473U) >> 8;
206  const unsigned int t3 = t2 - t1;
207  const unsigned int t4 = ((int)(t1 * 362U) >> 8) - t3;
208  const unsigned int t5 = (((int)(t1 * 277U) >> 8) - t2) + t4;
209 
210  blk[0 * step] = t1 + t0;
211  blk[1 * step] = t0 + t3;
212  blk[2 * step] = t4 + t0;
213  blk[3 * step] = t0 - t5;
214  blk[4 * step] = t5 + t0;
215  blk[5 * step] = t0 - t4;
216  blk[6 * step] = t0 - t3;
217  blk[7 * step] = t0 - t1;
218 }
219 
220 static void idct2_put(uint8_t *dst, int stride, int *block)
221 {
222  for (int i = 0; i < 2; i++) {
223  if ((block[0x08 + i]) == 0) {
224  block[0x08 + i] = block[i];
225  block[0x10 + i] = block[i];
226  block[0x18 + i] = block[i];
227  block[0x20 + i] = block[i];
228  block[0x28 + i] = block[i];
229  block[0x30 + i] = block[i];
230  block[0x38 + i] = block[i];
231  } else {
232  idct2_1d(block + i, 8);
233  }
234  }
235 
236  for (int i = 0; i < 8; i++) {
237  if (block[1] == 0) {
238  for (int j = 0; j < 8; j++)
239  dst[j] = av_clip_uint8((block[0] >> 5) + 128);
240  } else {
241  idct2_1d(block, 1);
242  for (int j = 0; j < 8; j++)
243  dst[j] = av_clip_uint8((block[j] >> 5) + 128);
244  }
245  block += 8;
246  dst += stride;
247  }
248 }
249 
250 static void idct2_add(uint8_t *dst, int stride,
251  const uint8_t *src, int in_linesize,
252  int *block)
253 {
254  for (int i = 0; i < 2; i++) {
255  if ((block[0x08 + i]) == 0) {
256  block[0x08 + i] = block[i];
257  block[0x10 + i] = block[i];
258  block[0x18 + i] = block[i];
259  block[0x20 + i] = block[i];
260  block[0x28 + i] = block[i];
261  block[0x30 + i] = block[i];
262  block[0x38 + i] = block[i];
263  } else {
264  idct2_1d(block + i, 8);
265  }
266  }
267 
268  for (int i = 0; i < 8; i++) {
269  if (block[1] == 0) {
270  for (int j = 0; j < 8; j++)
271  dst[j] = av_clip_uint8((block[0] >> 5) + src[j]);
272  } else {
273  idct2_1d(block, 1);
274  for (int j = 0; j < 8; j++)
275  dst[j] = av_clip_uint8((block[j] >> 5) + src[j]);
276  }
277  block += 8;
278  dst += stride;
279  src += in_linesize;
280  }
281 }
282 
283 static void update_inter_block(uint8_t *dst, int stride,
284  const uint8_t *src, int in_linesize,
285  int block)
286 {
287  for (int i = 0; i < 8; i++) {
288  for (int j = 0; j < 8; j++)
289  dst[j] = av_clip_uint8(block + src[j]);
290  dst += stride;
291  src += in_linesize;
292  }
293 }
294 
295 static int decode_intra_block(AVCodecContext *avctx, int mode,
296  GetByteContext *gbyte, int16_t *qtab,
297  int *block, int *pfill,
298  uint8_t *dst, int linesize)
299 {
300  MV30Context *s = avctx->priv_data;
301  int fill;
302 
303  switch (mode) {
304  case 0:
305  s->bdsp.fill_block_tab[1](dst, 128, linesize, 8);
306  break;
307  case 1:
308  fill = sign_extend(bytestream2_get_ne16(gbyte), 16);
309  pfill[0] += fill;
310  block[0] = ((int)((unsigned)pfill[0] * qtab[0]) >> 5) + 128;
311  s->bdsp.fill_block_tab[1](dst, block[0], linesize, 8);
312  break;
313  case 2:
314  memset(block, 0, sizeof(*block) * 64);
315  fill = sign_extend(bytestream2_get_ne16(gbyte), 16);
316  pfill[0] += fill;
317  block[0] = (unsigned)pfill[0] * qtab[0];
318  block[1] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[1];
319  block[8] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[8];
320  block[9] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[9];
321  idct2_put(dst, linesize, block);
322  break;
323  case 3:
324  fill = sign_extend(bytestream2_get_ne16(gbyte), 16);
325  pfill[0] += fill;
326  block[0] = (unsigned)pfill[0] * qtab[0];
327  for (int i = 1; i < 64; i++)
328  block[zigzag[i]] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[zigzag[i]];
329  idct_put(dst, linesize, block);
330  break;
331  }
332 
333  return 0;
334 }
335 
336 static int decode_inter_block(AVCodecContext *avctx, int mode,
337  GetByteContext *gbyte, int16_t *qtab,
338  int *block, int *pfill,
339  uint8_t *dst, int linesize,
340  const uint8_t *src, int in_linesize)
341 {
342  int fill;
343 
344  switch (mode) {
345  case 0:
346  copy_block8(dst, src, linesize, in_linesize, 8);
347  break;
348  case 1:
349  fill = sign_extend(bytestream2_get_ne16(gbyte), 16);
350  pfill[0] += fill;
351  block[0] = (int)((unsigned)pfill[0] * qtab[0]) >> 5;
352  update_inter_block(dst, linesize, src, in_linesize, block[0]);
353  break;
354  case 2:
355  memset(block, 0, sizeof(*block) * 64);
356  fill = sign_extend(bytestream2_get_ne16(gbyte), 16);
357  pfill[0] += fill;
358  block[0] = (unsigned)pfill[0] * qtab[0];
359  block[1] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[1];
360  block[8] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[8];
361  block[9] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[9];
362  idct2_add(dst, linesize, src, in_linesize, block);
363  break;
364  case 3:
365  fill = sign_extend(bytestream2_get_ne16(gbyte), 16);
366  pfill[0] += fill;
367  block[0] = (unsigned)pfill[0] * qtab[0];
368  for (int i = 1; i < 64; i++)
369  block[zigzag[i]] = sign_extend(bytestream2_get_ne16(gbyte), 16) * qtab[zigzag[i]];
370  idct_add(dst, linesize, src, in_linesize, block);
371  break;
372  }
373 
374  return 0;
375 }
376 
377 static int decode_coeffs(GetBitContext *gb, int16_t *coeffs, int nb_codes)
378 {
379  memset(coeffs, 0, nb_codes * sizeof(*coeffs));
380 
381  for (int i = 0; i < nb_codes;) {
382  int value = get_vlc2(gb, cbp_tab.table, CBP_VLC_BITS, 1);
383 
384  if (value > 0) {
385  int x = get_bits(gb, value);
386 
387  if (x < (1 << value) / 2) {
388  x = (1 << (value - 1)) + (x & ((1 << value) - 1 >> 1));
389  } else {
390  x = -(1 << (value - 1)) - (x & ((1 << value) - 1 >> 1));
391  }
392  coeffs[i++] = x;
393  } else {
394  int flag = get_bits1(gb);
395 
396  i += get_bits(gb, 3 + flag * 3) + 1 + flag * 8;
397  }
398  }
399 
400  return 0;
401 }
402 
404 {
405  MV30Context *s = avctx->priv_data;
406  GetBitContext mgb;
407  uint8_t *dst[6];
408  int linesize[6];
409  int ret;
410 
411  mgb = *gb;
412  if (get_bits_left(gb) < s->mode_size * 8)
413  return AVERROR_INVALIDDATA;
414 
415  skip_bits_long(gb, s->mode_size * 8);
416 
417  linesize[0] = frame->linesize[0];
418  linesize[1] = frame->linesize[0];
419  linesize[2] = frame->linesize[0];
420  linesize[3] = frame->linesize[0];
421  linesize[4] = frame->linesize[1];
422  linesize[5] = frame->linesize[2];
423 
424  for (int y = 0; y < avctx->height; y += 16) {
425  GetByteContext gbyte;
426  int pfill[3][1] = { {0} };
427  int nb_codes = get_bits(gb, 16);
428 
429  av_fast_padded_malloc(&s->coeffs, &s->coeffs_size, nb_codes * sizeof(*s->coeffs));
430  if (!s->coeffs)
431  return AVERROR(ENOMEM);
432  ret = decode_coeffs(gb, s->coeffs, nb_codes);
433  if (ret < 0)
434  return ret;
435 
436  bytestream2_init(&gbyte, (uint8_t *)s->coeffs, nb_codes * sizeof(*s->coeffs));
437 
438  for (int x = 0; x < avctx->width; x += 16) {
439  dst[0] = frame->data[0] + linesize[0] * y + x;
440  dst[1] = frame->data[0] + linesize[0] * y + x + 8;
441  dst[2] = frame->data[0] + linesize[0] * (y + 8) + x;
442  dst[3] = frame->data[0] + linesize[0] * (y + 8) + x + 8;
443  dst[4] = frame->data[1] + linesize[4] * (y >> 1) + (x >> 1);
444  dst[5] = frame->data[2] + linesize[5] * (y >> 1) + (x >> 1);
445 
446  for (int b = 0; b < 6; b++) {
447  int mode = get_bits_le(&mgb, 2);
448 
449  ret = decode_intra_block(avctx, mode, &gbyte, s->intraq_tab[b >= 4],
450  s->block[b],
451  pfill[(b >= 4) + (b >= 5)],
452  dst[b], linesize[b]);
453  if (ret < 0)
454  return ret;
455  }
456  }
457  }
458 
459  return 0;
460 }
461 
463  AVFrame *frame, AVFrame *prev)
464 {
465  MV30Context *s = avctx->priv_data;
467  GetBitContext mgb;
469  const int mask_size = ((avctx->height >> 4) * (avctx->width >> 4) * 2 + 7) / 8;
470  uint8_t *dst[6], *src[6];
471  int in_linesize[6];
472  int linesize[6];
473  int ret, cnt = 0;
474  int flags = 0;
475 
476  in_linesize[0] = prev->linesize[0];
477  in_linesize[1] = prev->linesize[0];
478  in_linesize[2] = prev->linesize[0];
479  in_linesize[3] = prev->linesize[0];
480  in_linesize[4] = prev->linesize[1];
481  in_linesize[5] = prev->linesize[2];
482 
483  linesize[0] = frame->linesize[0];
484  linesize[1] = frame->linesize[0];
485  linesize[2] = frame->linesize[0];
486  linesize[3] = frame->linesize[0];
487  linesize[4] = frame->linesize[1];
488  linesize[5] = frame->linesize[2];
489 
490  av_fast_padded_malloc(&s->mvectors, &s->mvectors_size, 2 * s->nb_mvectors * sizeof(*s->mvectors));
491  if (!s->mvectors) {
492  ret = AVERROR(ENOMEM);
493  goto fail;
494  }
495 
496  mask = *gb;
497  skip_bits_long(gb, mask_size * 8);
498  mgb = *gb;
499  skip_bits_long(gb, s->mode_size * 8);
500 
501  ret = decode_coeffs(gb, s->mvectors, 2 * s->nb_mvectors);
502  if (ret < 0)
503  goto fail;
504 
505  bytestream2_init(&mv, (uint8_t *)s->mvectors, 2 * s->nb_mvectors * sizeof(*s->mvectors));
506 
507  for (int y = 0; y < avctx->height; y += 16) {
508  GetByteContext gbyte;
509  int pfill[3][1] = { {0} };
510  int nb_codes = get_bits(gb, 16);
511 
512  skip_bits(gb, 8);
513  if (get_bits_left(gb) < 0) {
515  goto fail;
516  }
517 
518  av_fast_padded_malloc(&s->coeffs, &s->coeffs_size, nb_codes * sizeof(*s->coeffs));
519  if (!s->coeffs) {
520  ret = AVERROR(ENOMEM);
521  goto fail;
522  }
523 
524  ret = decode_coeffs(gb, s->coeffs, nb_codes);
525  if (ret < 0)
526  goto fail;
527 
528  bytestream2_init(&gbyte, (uint8_t *)s->coeffs, nb_codes * sizeof(*s->coeffs));
529 
530  for (int x = 0; x < avctx->width; x += 16) {
531  if (cnt >= 4)
532  cnt = 0;
533  if (cnt == 0) {
534  if (get_bits_left(&mask) < 8) {
536  goto fail;
537  }
538  flags = get_bits(&mask, 8);
539  }
540 
541  dst[0] = frame->data[0] + linesize[0] * y + x;
542  dst[1] = frame->data[0] + linesize[0] * y + x + 8;
543  dst[2] = frame->data[0] + linesize[0] * (y + 8) + x;
544  dst[3] = frame->data[0] + linesize[0] * (y + 8) + x + 8;
545  dst[4] = frame->data[1] + linesize[4] * (y >> 1) + (x >> 1);
546  dst[5] = frame->data[2] + linesize[5] * (y >> 1) + (x >> 1);
547 
548  if ((flags >> (cnt)) & 1) {
549  int mv_x = sign_extend(bytestream2_get_ne16(&mv), 16);
550  int mv_y = sign_extend(bytestream2_get_ne16(&mv), 16);
551 
552  int px = x + mv_x;
553  int py = y + mv_y;
554 
555  if (px < 0 || px > FFALIGN(avctx->width , 16) - 16 ||
556  py < 0 || py > FFALIGN(avctx->height, 16) - 16)
557  return AVERROR_INVALIDDATA;
558 
559  src[0] = prev->data[0] + in_linesize[0] * py + px;
560  src[1] = prev->data[0] + in_linesize[0] * py + px + 8;
561  src[2] = prev->data[0] + in_linesize[0] * (py + 8) + px;
562  src[3] = prev->data[0] + in_linesize[0] * (py + 8) + px + 8;
563  src[4] = prev->data[1] + in_linesize[4] * (py >> 1) + (px >> 1);
564  src[5] = prev->data[2] + in_linesize[5] * (py >> 1) + (px >> 1);
565 
566  if ((flags >> (cnt + 4)) & 1) {
567  for (int b = 0; b < 6; b++)
568  copy_block8(dst[b], src[b], linesize[b], in_linesize[b], 8);
569  } else {
570  for (int b = 0; b < 6; b++) {
571  int mode = get_bits_le(&mgb, 2);
572 
573  ret = decode_inter_block(avctx, mode, &gbyte, s->interq_tab[b >= 4],
574  s->block[b],
575  pfill[(b >= 4) + (b >= 5)],
576  dst[b], linesize[b],
577  src[b], in_linesize[b]);
578  if (ret < 0)
579  goto fail;
580  }
581  }
582  } else {
583  for (int b = 0; b < 6; b++) {
584  int mode = get_bits_le(&mgb, 2);
585 
586  ret = decode_intra_block(avctx, mode, &gbyte, s->intraq_tab[b >= 4],
587  s->block[b],
588  pfill[(b >= 4) + (b >= 5)],
589  dst[b], linesize[b]);
590  if (ret < 0)
591  goto fail;
592  }
593  }
594 
595  cnt++;
596  }
597  }
598 
599 fail:
600  return ret;
601 }
602 
604  int *got_frame, AVPacket *avpkt)
605 {
606  MV30Context *s = avctx->priv_data;
607  GetBitContext *gb = &s->gb;
608  int ret;
609 
610  if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
611  return ret;
612 
613  if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
614  return ret;
615 
616  s->intra_quant = get_bits(gb, 8);
617  s->inter_quant = s->intra_quant + get_sbits(gb, 8);
618  s->is_inter = get_bits_le(gb, 16);
619  s->mode_size = get_bits_le(gb, 16);
620  if (s->is_inter)
621  s->nb_mvectors = get_bits_le(gb, 16);
622 
623  get_qtable(s->intraq_tab[0], s->intra_quant, luma_tab);
624  get_qtable(s->intraq_tab[1], s->intra_quant, chroma_tab);
625 
626  if (s->is_inter == 0) {
628  ret = decode_intra(avctx, gb, frame);
629  if (ret < 0)
630  return ret;
631  } else {
632  get_qtable(s->interq_tab[0], s->inter_quant, luma_tab);
633  get_qtable(s->interq_tab[1], s->inter_quant, chroma_tab);
634 
635  if (!s->prev_frame->data[0]) {
636  av_log(avctx, AV_LOG_ERROR, "Missing reference frame.\n");
637  return AVERROR_INVALIDDATA;
638  }
639 
641  ret = decode_inter(avctx, gb, frame, s->prev_frame);
642  if (ret < 0)
643  return ret;
644  }
645 
646  if ((ret = av_frame_replace(s->prev_frame, frame)) < 0)
647  return ret;
648 
649  *got_frame = 1;
650 
651  return avpkt->size;
652 }
653 
654 static const uint8_t cbp_bits[] = {
655  2, 2, 3, 3, 3, 4, 5, 6, 7, 8, 9, 9,
656 };
657 
658 static av_cold void init_static_data(void)
659 {
661  cbp_bits, 1, NULL, 0, 0, 0, 0, 1 << CBP_VLC_BITS);
662 }
663 
665 {
666  MV30Context *s = avctx->priv_data;
667  static AVOnce init_static_once = AV_ONCE_INIT;
668 
669  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
670  avctx->color_range = AVCOL_RANGE_JPEG;
671 
672  ff_blockdsp_init(&s->bdsp);
673 
674  s->prev_frame = av_frame_alloc();
675  if (!s->prev_frame)
676  return AVERROR(ENOMEM);
677 
678  ff_thread_once(&init_static_once, init_static_data);
679 
680  return 0;
681 }
682 
683 static void decode_flush(AVCodecContext *avctx)
684 {
685  MV30Context *s = avctx->priv_data;
686 
687  av_frame_unref(s->prev_frame);
688 }
689 
691 {
692  MV30Context *s = avctx->priv_data;
693 
694  av_frame_free(&s->prev_frame);
695  av_freep(&s->coeffs);
696  s->coeffs_size = 0;
697  av_freep(&s->mvectors);
698  s->mvectors_size = 0;
699 
700  return 0;
701 }
702 
704  .p.name = "mv30",
705  CODEC_LONG_NAME("MidiVid 3.0"),
706  .p.type = AVMEDIA_TYPE_VIDEO,
707  .p.id = AV_CODEC_ID_MV30,
708  .priv_data_size = sizeof(MV30Context),
709  .init = decode_init,
710  .close = decode_close,
712  .flush = decode_flush,
713  .p.capabilities = AV_CODEC_CAP_DR1,
714  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
715 };
MV30Context::mvectors_size
unsigned int mvectors_size
Definition: mv30.c:51
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:278
idct2_add
static void idct2_add(uint8_t *dst, int stride, const uint8_t *src, int in_linesize, int *block)
Definition: mv30.c:250
av_clip
#define av_clip
Definition: common.h:96
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
blockdsp.h
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:694
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
GetByteContext
Definition: bytestream.h:33
thread.h
ff_mv30_decoder
const FFCodec ff_mv30_decoder
Definition: mv30.c:703
mv
static const int8_t mv[256][2]
Definition: 4xm.c:80
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
CBP_VLC_BITS
#define CBP_VLC_BITS
Definition: mv30.c:38
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:673
AVPacket::data
uint8_t * data
Definition: packet.h:491
luma_tab
static const uint8_t luma_tab[]
Definition: mv30.c:64
t0
#define t0
Definition: regdef.h:28
b
#define b
Definition: input.c:41
table
static const uint16_t table[]
Definition: prosumer.c:205
FFCodec
Definition: codec_internal.h:127
copy_block8
static void copy_block8(uint8_t *dst, const uint8_t *src, ptrdiff_t dstStride, ptrdiff_t srcStride, int h)
Definition: copy_block.h:47
t1
#define t1
Definition: regdef.h:29
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:649
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
BlockDSPContext
Definition: blockdsp.h:32
MV30Context::inter_quant
int inter_quant
Definition: mv30.c:44
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
t10
#define t10
Definition: regdef.h:55
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
decode_intra
static int decode_intra(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame)
Definition: mv30.c:403
init_static_data
static av_cold void init_static_data(void)
Definition: mv30.c:658
fail
#define fail()
Definition: checkasm.h:138
MV30Context::bdsp
BlockDSPContext bdsp
Definition: mv30.c:58
AV_CODEC_ID_MV30
@ AV_CODEC_ID_MV30
Definition: codec_id.h:304
GetBitContext
Definition: get_bits.h:108
chroma_tab
static const uint8_t chroma_tab[]
Definition: mv30.c:75
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:88
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: mv30.c:603
quant
static const uint8_t quant[64]
Definition: vmixdec.c:71
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:203
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:628
mask
static const uint16_t mask[17]
Definition: lzw.c:38
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
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
AV_GET_BUFFER_FLAG_REF
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:421
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
t7
#define t7
Definition: regdef.h:35
get_bits_le
static unsigned int get_bits_le(GetBitContext *s, int n)
Definition: get_bits.h:356
get_sbits
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:320
decode.h
get_bits.h
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
blk
#define blk(i)
Definition: sha.c:186
decode_flush
static void decode_flush(AVCodecContext *avctx)
Definition: mv30.c:683
MV30Context::prev_frame
AVFrame * prev_frame
Definition: mv30.c:59
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
cbp_bits
static const uint8_t cbp_bits[]
Definition: mv30.c:654
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:201
MV30Context
Definition: mv30.c:40
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1039
decode_intra_block
static int decode_intra_block(AVCodecContext *avctx, int mode, GetByteContext *gbyte, int16_t *qtab, int *block, int *pfill, uint8_t *dst, int linesize)
Definition: mv30.c:295
t5
#define t5
Definition: regdef.h:33
aandcttab.h
t6
#define t6
Definition: regdef.h:34
MV30Context::mode_size
int mode_size
Definition: mv30.c:46
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
mathops.h
idct2_put
static void idct2_put(uint8_t *dst, int stride, int *block)
Definition: mv30.c:220
zigzag
static const uint8_t zigzag[]
Definition: mv30.c:86
MV30Context::is_inter
int is_inter
Definition: mv30.c:45
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:652
AVOnce
#define AVOnce
Definition: thread.h:200
decode_close
static av_cold int decode_close(AVCodecContext *avctx)
Definition: mv30.c:690
MV30Context::nb_mvectors
int nb_mvectors
Definition: mv30.c:47
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1617
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:492
idct_put
static void idct_put(uint8_t *dst, int stride, int *block)
Definition: mv30.c:137
codec_internal.h
bytestream2_get_ne16
#define bytestream2_get_ne16
Definition: bytestream.h:119
MV30Context::gb
GetBitContext gb
Definition: mv30.c:41
t8
#define t8
Definition: regdef.h:53
flag
#define flag(name)
Definition: cbs_av1.c:466
MV30Context::interq_tab
int16_t interq_tab[2][64]
Definition: mv30.c:56
idct_1d
static void idct_1d(unsigned *blk, int step)
Definition: mv30.c:107
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
copy_block.h
MV30Context::coeffs
int16_t * coeffs
Definition: mv30.c:52
t4
#define t4
Definition: regdef.h:32
t3
#define t3
Definition: regdef.h:31
MV30Context::intra_quant
int intra_quant
Definition: mv30.c:43
av_fast_padded_malloc
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size)
Same behaviour av_fast_malloc but the buffer has additional AV_INPUT_BUFFER_PADDING_SIZE at the end w...
Definition: utils.c:52
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:622
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
MV30Context::coeffs_size
unsigned int coeffs_size
Definition: mv30.c:53
AVCodecContext::height
int height
Definition: avcodec.h:621
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:658
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
get_qtable
static void get_qtable(int16_t *table, int quant, const uint8_t *quant_tab)
Definition: mv30.c:97
MV30Context::block
int block[6][64]
Definition: mv30.c:49
ret
ret
Definition: filter_design.txt:187
MV30Context::intraq_tab
int16_t intraq_tab[2][64]
Definition: mv30.c:55
U
#define U(x)
Definition: vpx_arith.h:37
av_frame_replace
int av_frame_replace(AVFrame *dst, const AVFrame *src)
Ensure the destination frame refers to the same data described by the source frame,...
Definition: frame.c:482
AVCodecContext
main external API structure.
Definition: avcodec.h:441
t2
#define t2
Definition: regdef.h:30
mode
mode
Definition: ebur128.h:83
VLC
Definition: vlc.h:33
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:133
VLC::table
VLCElem * table
Definition: vlc.h:35
decode_coeffs
static int decode_coeffs(GetBitContext *gb, int16_t *coeffs, int nb_codes)
Definition: mv30.c:377
VLC_INIT_STATIC_FROM_LENGTHS
#define VLC_INIT_STATIC_FROM_LENGTHS(vlc, bits, nb_codes, lens, len_wrap, symbols, symbols_wrap, symbols_size, offset, flags, static_size)
Definition: vlc.h:213
av_clip_uint8
#define av_clip_uint8
Definition: common.h:102
factor
static const int factor[16]
Definition: vf_pp7.c:78
MV30Context::mvectors
int16_t * mvectors
Definition: mv30.c:50
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
t9
#define t9
Definition: regdef.h:54
decode_inter
static int decode_inter(AVCodecContext *avctx, GetBitContext *gb, AVFrame *frame, AVFrame *prev)
Definition: mv30.c:462
decode_init
static av_cold int decode_init(AVCodecContext *avctx)
Definition: mv30.c:664
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVPacket
This structure stores compressed data.
Definition: packet.h:468
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:468
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:621
bytestream.h
idct_add
static void idct_add(uint8_t *dst, int stride, const uint8_t *src, int in_linesize, int *block)
Definition: mv30.c:168
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
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
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
int
int
Definition: ffmpeg_filter.c:368
cbp_tab
static VLC cbp_tab
Definition: mv30.c:62
idct2_1d
static void idct2_1d(int *blk, int step)
Definition: mv30.c:201
ff_aanscales
const uint16_t ff_aanscales[64]
Definition: aandcttab.c:26
update_inter_block
static void update_inter_block(uint8_t *dst, int stride, const uint8_t *src, int in_linesize, int block)
Definition: mv30.c:283
decode_inter_block
static int decode_inter_block(AVCodecContext *avctx, int mode, GetByteContext *gbyte, int16_t *qtab, int *block, int *pfill, uint8_t *dst, int linesize, const uint8_t *src, int in_linesize)
Definition: mv30.c:336