* providing MPEG codecs with a generic fields in AVFrame to use.
[libav.git] / libavcodec / dv.c
CommitLineData
5d2231ee
FB
1/*
2 * DV decoder
3 * Copyright (c) 2002 Fabrice Bellard.
4 *
4fa1c4fa
RS
5 * DV encoder
6 * Copyright (c) 2003 Roman Shaposhnik.
7 *
8 * Many thanks to Dan Dennedy <dan@dennedy.org> for providing wealth
9 * of DV technical info.
10 *
5d2231ee
FB
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
983e3246
MN
25
26/**
27 * @file dv.c
4fa1c4fa 28 * DV codec.
983e3246 29 */
5d2231ee
FB
30#include "avcodec.h"
31#include "dsputil.h"
32#include "mpegvideo.h"
33#include "simple_idct.h"
7458ccbb 34#include "dvdata.h"
5d2231ee
FB
35
36typedef struct DVVideoDecodeContext {
7458ccbb 37 const DVprofile* sys;
5d2231ee 38 GetBitContext gb;
492cd3a9 39 AVFrame picture;
5d2231ee 40 DCTELEM block[5*6][64] __align8;
7458ccbb
RS
41
42 /* FIXME: the following is extracted from DSP */
0c1a9eda
ZK
43 uint8_t dv_zigzag[2][64];
44 uint8_t idct_permutation[64];
7458ccbb
RS
45 void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
46 void (*fdct)(DCTELEM *block);
47
5d2231ee 48 /* XXX: move it to static storage ? */
0c1a9eda
ZK
49 uint8_t dv_shift[2][22][64];
50 void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
5d2231ee
FB
51} DVVideoDecodeContext;
52
7458ccbb 53#define TEX_VLC_BITS 9
5d2231ee
FB
54/* XXX: also include quantization */
55static RL_VLC_ELEM *dv_rl_vlc[1];
7458ccbb 56static VLC_TYPE dv_vlc_codes[15][23];
5d2231ee
FB
57
58static void dv_build_unquantize_tables(DVVideoDecodeContext *s)
59{
7be166e4 60 int i, q, j;
5d2231ee
FB
61
62 /* NOTE: max left shift is 6 */
7be166e4
FB
63 for(q = 0; q < 22; q++) {
64 /* 88 unquant */
65 for(i = 1; i < 64; i++) {
66 /* 88 table */
67 j = s->idct_permutation[i];
68 s->dv_shift[0][q][j] =
69 dv_quant_shifts[q][dv_88_areas[i]] + 1;
70 }
71
72 /* 248 unquant */
73 for(i = 1; i < 64; i++) {
74 /* 248 table */
75 s->dv_shift[1][q][i] =
76 dv_quant_shifts[q][dv_248_areas[i]] + 1;
5d2231ee
FB
77 }
78 }
79}
80
4fa1c4fa 81static int dvvideo_init(AVCodecContext *avctx)
5d2231ee
FB
82{
83 DVVideoDecodeContext *s = avctx->priv_data;
7be166e4 84 MpegEncContext s2;
7604246d 85 static int done=0;
5d2231ee
FB
86
87 if (!done) {
88 int i;
7458ccbb 89 VLC dv_vlc;
5d2231ee
FB
90
91 done = 1;
92
93 /* NOTE: as a trick, we use the fact the no codes are unused
94 to accelerate the parsing of partial codes */
95 init_vlc(&dv_vlc, TEX_VLC_BITS, NB_DV_VLC,
96 dv_vlc_len, 1, 1, dv_vlc_bits, 2, 2);
97
98 dv_rl_vlc[0] = av_malloc(dv_vlc.table_size * sizeof(RL_VLC_ELEM));
99 for(i = 0; i < dv_vlc.table_size; i++){
100 int code= dv_vlc.table[i][0];
101 int len = dv_vlc.table[i][1];
102 int level, run;
103
104 if(len<0){ //more bits needed
105 run= 0;
106 level= code;
107 } else if (code == (NB_DV_VLC - 1)) {
108 /* EOB */
109 run = 0;
110 level = 256;
111 } else {
112 run= dv_vlc_run[code] + 1;
113 level= dv_vlc_level[code];
114 }
115 dv_rl_vlc[0][i].len = len;
116 dv_rl_vlc[0][i].level = level;
117 dv_rl_vlc[0][i].run = run;
118 }
7458ccbb
RS
119
120 memset(dv_vlc_codes, 0xff, sizeof(dv_vlc_codes));
121 for (i = 0; i < NB_DV_VLC - 1; i++) {
122 if (dv_vlc_run[i] < 15 && dv_vlc_level[i] < 23 && dv_vlc_len[i] < 15)
123 dv_vlc_codes[dv_vlc_run[i]][dv_vlc_level[i]] = i;
124 }
5d2231ee 125 }
7be166e4
FB
126
127 /* ugly way to get the idct & scantable */
128 /* XXX: fix it */
129 memset(&s2, 0, sizeof(MpegEncContext));
7be166e4 130 s2.avctx = avctx;
b0368839 131 dsputil_init(&s2.dsp, avctx);
defdfc9a 132 if (DCT_common_init(&s2) < 0)
7be166e4
FB
133 return -1;
134
7458ccbb
RS
135 s->get_pixels = s2.dsp.get_pixels;
136 s->fdct = s2.dsp.fdct;
137
b0368839
MN
138 s->idct_put[0] = s2.dsp.idct_put;
139 memcpy(s->idct_permutation, s2.dsp.idct_permutation, 64);
7be166e4
FB
140 memcpy(s->dv_zigzag[0], s2.intra_scantable.permutated, 64);
141
142 /* XXX: use MMX also for idct248 */
143 s->idct_put[1] = simple_idct248_put;
144 memcpy(s->dv_zigzag[1], dv_248_zigzag, 64);
145
5d2231ee
FB
146 /* XXX: do it only for constant case */
147 dv_build_unquantize_tables(s);
7458ccbb 148
4fa1c4fa
RS
149 /* FIXME: I really don't think this should be here */
150 if (dv_codec_profile(avctx))
151 avctx->pix_fmt = dv_codec_profile(avctx)->pix_fmt;
2744ca9a 152 avctx->coded_frame = &s->picture;
4fa1c4fa 153
5d2231ee
FB
154 return 0;
155}
156
7458ccbb 157// #define VLC_DEBUG
5d2231ee 158
7be166e4 159typedef struct BlockInfo {
0c1a9eda
ZK
160 const uint8_t *shift_table;
161 const uint8_t *scan_table;
162 uint8_t pos; /* position in block */
163 uint8_t eob_reached; /* true if EOB has been reached */
164 uint8_t dct_mode;
165 uint8_t partial_bit_count;
166 uint16_t partial_bit_buffer;
7be166e4
FB
167 int shift_offset;
168} BlockInfo;
5d2231ee
FB
169
170/* block size in bits */
0c1a9eda 171static const uint16_t block_sizes[6] = {
5d2231ee
FB
172 112, 112, 112, 112, 80, 80
173};
174
175#ifndef ALT_BITSTREAM_READER
924311cd 176#warning only works with ALT_BITSTREAM_READER
5d2231ee
FB
177#endif
178
179/* decode ac coefs */
180static void dv_decode_ac(DVVideoDecodeContext *s,
0e15384d 181 BlockInfo *mb, DCTELEM *block, int last_index)
5d2231ee
FB
182{
183 int last_re_index;
7be166e4 184 int shift_offset = mb->shift_offset;
0c1a9eda
ZK
185 const uint8_t *scan_table = mb->scan_table;
186 const uint8_t *shift_table = mb->shift_table;
7be166e4 187 int pos = mb->pos;
5d2231ee
FB
188 int level, pos1, sign, run;
189 int partial_bit_count;
924311cd
MN
190#ifndef ALT_BITSTREAM_READER //FIXME
191 int re_index=0;
192 int re1_index=0;
193#endif
5d2231ee
FB
194 OPEN_READER(re, &s->gb);
195
196#ifdef VLC_DEBUG
7be166e4 197 printf("start\n");
5d2231ee
FB
198#endif
199
200 /* if we must parse a partial vlc, we do it here */
7be166e4 201 partial_bit_count = mb->partial_bit_count;
5d2231ee 202 if (partial_bit_count > 0) {
0c1a9eda
ZK
203 uint8_t buf[4];
204 uint32_t v;
5d2231ee
FB
205 int l, l1;
206 GetBitContext gb1;
207
208 /* build the dummy bit buffer */
209 l = 16 - partial_bit_count;
210 UPDATE_CACHE(re, &s->gb);
211#ifdef VLC_DEBUG
212 printf("show=%04x\n", SHOW_UBITS(re, &s->gb, 16));
213#endif
7be166e4 214 v = (mb->partial_bit_buffer << l) | SHOW_UBITS(re, &s->gb, l);
5d2231ee
FB
215 buf[0] = v >> 8;
216 buf[1] = v;
217#ifdef VLC_DEBUG
218 printf("v=%04x cnt=%d %04x\n",
7be166e4 219 v, partial_bit_count, (mb->partial_bit_buffer << l));
5d2231ee
FB
220#endif
221 /* try to read the codeword */
68f593b4 222 init_get_bits(&gb1, buf, 4*8);
5d2231ee
FB
223 {
224 OPEN_READER(re1, &gb1);
225 UPDATE_CACHE(re1, &gb1);
226 GET_RL_VLC(level, run, re1, &gb1, dv_rl_vlc[0],
227 TEX_VLC_BITS, 2);
228 l = re1_index;
229 CLOSE_READER(re1, &gb1);
230 }
231#ifdef VLC_DEBUG
232 printf("****run=%d level=%d size=%d\n", run, level, l);
233#endif
234 /* compute codeword length */
235 l1 = (level != 256 && level != 0);
236 /* if too long, we cannot parse */
237 l -= partial_bit_count;
238 if ((re_index + l + l1) > last_index)
239 return;
240 /* skip read bits */
241 last_re_index = 0; /* avoid warning */
242 re_index += l;
7be166e4 243 /* by definition, if we can read the vlc, all partial bits
5d2231ee 244 will be read (otherwise we could have read the vlc before) */
7be166e4 245 mb->partial_bit_count = 0;
5d2231ee
FB
246 UPDATE_CACHE(re, &s->gb);
247 goto handle_vlc;
248 }
249
250 /* get the AC coefficients until last_index is reached */
251 for(;;) {
252 UPDATE_CACHE(re, &s->gb);
253#ifdef VLC_DEBUG
254 printf("%2d: bits=%04x index=%d\n",
255 pos, SHOW_UBITS(re, &s->gb, 16), re_index);
256#endif
257 last_re_index = re_index;
258 GET_RL_VLC(level, run, re, &s->gb, dv_rl_vlc[0],
259 TEX_VLC_BITS, 2);
260 handle_vlc:
261#ifdef VLC_DEBUG
262 printf("run=%d level=%d\n", run, level);
263#endif
264 if (level == 256) {
265 if (re_index > last_index) {
266 cannot_read:
267 /* put position before read code */
268 re_index = last_re_index;
7be166e4 269 mb->eob_reached = 0;
5d2231ee
FB
270 break;
271 }
272 /* EOB */
7be166e4 273 mb->eob_reached = 1;
5d2231ee
FB
274 break;
275 } else if (level != 0) {
276 if ((re_index + 1) > last_index)
277 goto cannot_read;
278 sign = SHOW_SBITS(re, &s->gb, 1);
279 level = (level ^ sign) - sign;
280 LAST_SKIP_BITS(re, &s->gb, 1);
281 pos += run;
282 /* error */
283 if (pos >= 64) {
284 goto read_error;
285 }
286 pos1 = scan_table[pos];
7be166e4 287 level = level << (shift_table[pos1] + shift_offset);
5d2231ee
FB
288 block[pos1] = level;
289 // printf("run=%d level=%d shift=%d\n", run, level, shift_table[pos1]);
290 } else {
291 if (re_index > last_index)
292 goto cannot_read;
293 /* level is zero: means run without coding. No
294 sign is coded */
295 pos += run;
296 /* error */
297 if (pos >= 64) {
298 read_error:
299#if defined(VLC_DEBUG) || 1
ed543377 300 fprintf(stderr, "error pos=%d\n", pos);
5d2231ee
FB
301#endif
302 /* for errors, we consider the eob is reached */
7be166e4 303 mb->eob_reached = 1;
5d2231ee
FB
304 break;
305 }
306 }
307 }
308 CLOSE_READER(re, &s->gb);
7be166e4 309 mb->pos = pos;
5d2231ee
FB
310}
311
312static inline void bit_copy(PutBitContext *pb, GetBitContext *gb, int bits_left)
313{
314 while (bits_left >= 16) {
315 put_bits(pb, 16, get_bits(gb, 16));
316 bits_left -= 16;
317 }
318 if (bits_left > 0) {
319 put_bits(pb, bits_left, get_bits(gb, bits_left));
320 }
321}
322
323/* mb_x and mb_y are in units of 8 pixels */
324static inline void dv_decode_video_segment(DVVideoDecodeContext *s,
0c1a9eda
ZK
325 uint8_t *buf_ptr1,
326 const uint16_t *mb_pos_ptr)
5d2231ee
FB
327{
328 int quant, dc, dct_mode, class1, j;
329 int mb_index, mb_x, mb_y, v, last_index;
330 DCTELEM *block, *block1;
331 int c_offset, bits_left;
0c1a9eda 332 uint8_t *y_ptr;
7be166e4 333 BlockInfo mb_data[5 * 6], *mb, *mb1;
0c1a9eda
ZK
334 void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
335 uint8_t *buf_ptr;
5d2231ee 336 PutBitContext pb, vs_pb;
0c1a9eda 337 uint8_t mb_bit_buffer[80 + 4]; /* allow some slack */
7be166e4 338 int mb_bit_count;
0c1a9eda 339 uint8_t vs_bit_buffer[5 * 80 + 4]; /* allow some slack */
5d2231ee
FB
340 int vs_bit_count;
341
342 memset(s->block, 0, sizeof(s->block));
343
344 /* pass 1 : read DC and AC coefficients in blocks */
345 buf_ptr = buf_ptr1;
346 block1 = &s->block[0][0];
7be166e4 347 mb1 = mb_data;
ed7debda 348 init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
5d2231ee
FB
349 vs_bit_count = 0;
350 for(mb_index = 0; mb_index < 5; mb_index++) {
351 /* skip header */
352 quant = buf_ptr[3] & 0x0f;
353 buf_ptr += 4;
ed7debda 354 init_put_bits(&pb, mb_bit_buffer, 80);
7be166e4
FB
355 mb_bit_count = 0;
356 mb = mb1;
5d2231ee
FB
357 block = block1;
358 for(j = 0;j < 6; j++) {
359 /* NOTE: size is not important here */
68f593b4 360 init_get_bits(&s->gb, buf_ptr, 14*8);
5d2231ee
FB
361
362 /* get the dc */
363 dc = get_bits(&s->gb, 9);
364 dc = (dc << (32 - 9)) >> (32 - 9);
365 dct_mode = get_bits1(&s->gb);
7be166e4
FB
366 mb->dct_mode = dct_mode;
367 mb->scan_table = s->dv_zigzag[dct_mode];
5d2231ee 368 class1 = get_bits(&s->gb, 2);
7be166e4
FB
369 mb->shift_offset = (class1 == 3);
370 mb->shift_table = s->dv_shift[dct_mode]
5d2231ee
FB
371 [quant + dv_quant_offset[class1]];
372 dc = dc << 2;
373 /* convert to unsigned because 128 is not added in the
374 standard IDCT */
375 dc += 1024;
376 block[0] = dc;
377 last_index = block_sizes[j];
378 buf_ptr += last_index >> 3;
7be166e4
FB
379 mb->pos = 0;
380 mb->partial_bit_count = 0;
5d2231ee 381
4fa1c4fa
RS
382#ifdef VLC_DEBUG
383 printf("MB block: %d, %d ", mb_index, j);
384#endif
7be166e4 385 dv_decode_ac(s, mb, block, last_index);
5d2231ee
FB
386
387 /* write the remaining bits in a new buffer only if the
388 block is finished */
924311cd 389 bits_left = last_index - get_bits_count(&s->gb);
7be166e4
FB
390 if (mb->eob_reached) {
391 mb->partial_bit_count = 0;
392 mb_bit_count += bits_left;
5d2231ee
FB
393 bit_copy(&pb, &s->gb, bits_left);
394 } else {
395 /* should be < 16 bits otherwise a codeword could have
396 been parsed */
7be166e4
FB
397 mb->partial_bit_count = bits_left;
398 mb->partial_bit_buffer = get_bits(&s->gb, bits_left);
5d2231ee
FB
399 }
400 block += 64;
7be166e4 401 mb++;
5d2231ee
FB
402 }
403
404 flush_put_bits(&pb);
405
406 /* pass 2 : we can do it just after */
407#ifdef VLC_DEBUG
4fa1c4fa 408 printf("***pass 2 size=%d MB#=%d\n", mb_bit_count, mb_index);
5d2231ee
FB
409#endif
410 block = block1;
7be166e4 411 mb = mb1;
68f593b4 412 init_get_bits(&s->gb, mb_bit_buffer, 80*8);
5d2231ee 413 for(j = 0;j < 6; j++) {
924311cd 414 if (!mb->eob_reached && get_bits_count(&s->gb) < mb_bit_count) {
7be166e4 415 dv_decode_ac(s, mb, block, mb_bit_count);
5d2231ee 416 /* if still not finished, no need to parse other blocks */
7be166e4 417 if (!mb->eob_reached) {
5d2231ee
FB
418 /* we could not parse the current AC coefficient,
419 so we add the remaining bytes */
924311cd 420 bits_left = mb_bit_count - get_bits_count(&s->gb);
5d2231ee 421 if (bits_left > 0) {
7be166e4
FB
422 mb->partial_bit_count += bits_left;
423 mb->partial_bit_buffer =
424 (mb->partial_bit_buffer << bits_left) |
5d2231ee
FB
425 get_bits(&s->gb, bits_left);
426 }
427 goto next_mb;
428 }
429 }
430 block += 64;
7be166e4 431 mb++;
5d2231ee
FB
432 }
433 /* all blocks are finished, so the extra bytes can be used at
434 the video segment level */
924311cd 435 bits_left = mb_bit_count - get_bits_count(&s->gb);
5d2231ee
FB
436 vs_bit_count += bits_left;
437 bit_copy(&vs_pb, &s->gb, bits_left);
438 next_mb:
7be166e4 439 mb1 += 6;
5d2231ee
FB
440 block1 += 6 * 64;
441 }
442
443 /* we need a pass other the whole video segment */
444 flush_put_bits(&vs_pb);
445
446#ifdef VLC_DEBUG
447 printf("***pass 3 size=%d\n", vs_bit_count);
448#endif
449 block = &s->block[0][0];
450 mb = mb_data;
68f593b4 451 init_get_bits(&s->gb, vs_bit_buffer, 5 * 80*8);
5d2231ee
FB
452 for(mb_index = 0; mb_index < 5; mb_index++) {
453 for(j = 0;j < 6; j++) {
7be166e4 454 if (!mb->eob_reached) {
5d2231ee
FB
455#ifdef VLC_DEBUG
456 printf("start %d:%d\n", mb_index, j);
457#endif
7be166e4 458 dv_decode_ac(s, mb, block, vs_bit_count);
5d2231ee
FB
459 }
460 block += 64;
7be166e4 461 mb++;
5d2231ee 462 }
5d2231ee
FB
463 }
464
465 /* compute idct and place blocks */
466 block = &s->block[0][0];
467 mb = mb_data;
468 for(mb_index = 0; mb_index < 5; mb_index++) {
469 v = *mb_pos_ptr++;
470 mb_x = v & 0xff;
471 mb_y = v >> 8;
7458ccbb
RS
472 y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
473 if (s->sys->pix_fmt == PIX_FMT_YUV411P)
474 c_offset = (mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8);
5d2231ee 475 else
7458ccbb 476 c_offset = ((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8);
5d2231ee 477 for(j = 0;j < 6; j++) {
7be166e4 478 idct_put = s->idct_put[mb->dct_mode];
5d2231ee 479 if (j < 4) {
7458ccbb 480 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
1c05e11d 481 /* NOTE: at end of line, the macroblock is handled as 420 */
7458ccbb 482 idct_put(y_ptr + (j * 8), s->picture.linesize[0], block);
5d2231ee 483 } else {
7458ccbb
RS
484 idct_put(y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]),
485 s->picture.linesize[0], block);
5d2231ee
FB
486 }
487 } else {
7458ccbb 488 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
00c28046 489 uint64_t aligned_pixels[64/8];
f539eb52 490 uint8_t *pixels= (uint8_t*)aligned_pixels;
00c28046 491 uint8_t *c_ptr, *c_ptr1, *ptr;
1c05e11d
FB
492 int y, linesize;
493 /* NOTE: at end of line, the macroblock is handled as 420 */
494 idct_put(pixels, 8, block);
7458ccbb
RS
495 linesize = s->picture.linesize[6 - j];
496 c_ptr = s->picture.data[6 - j] + c_offset;
1c05e11d
FB
497 ptr = pixels;
498 for(y = 0;y < 8; y++) {
499 /* convert to 411P */
62cf114b
MN
500 c_ptr1 = c_ptr + 8*linesize;
501 c_ptr[0]= ptr[0]; c_ptr1[0]= ptr[4];
502 c_ptr[1]= ptr[1]; c_ptr1[1]= ptr[5];
503 c_ptr[2]= ptr[2]; c_ptr1[2]= ptr[6];
504 c_ptr[3]= ptr[3]; c_ptr1[3]= ptr[7];
505 c_ptr += linesize;
1c05e11d
FB
506 ptr += 8;
507 }
508 } else {
509 /* don't ask me why they inverted Cb and Cr ! */
7458ccbb
RS
510 idct_put(s->picture.data[6 - j] + c_offset,
511 s->picture.linesize[6 - j], block);
1c05e11d 512 }
5d2231ee
FB
513 }
514 block += 64;
7be166e4 515 mb++;
5d2231ee 516 }
5d2231ee
FB
517 }
518}
519
4fa1c4fa
RS
520/* Converts run and level (where level != 0) pair into vlc, returning bit size */
521static inline int dv_rl2vlc(int run, int l, uint32_t* vlc)
522{
523 int sign = l >> 8;
524 int level = (l ^ sign) - sign;
525 int size;
526
527 sign = (sign & 1);
528
529 if (run < 15 && level < 23 && dv_vlc_codes[run][level] != -1) {
530 *vlc = (dv_vlc_bits[dv_vlc_codes[run][level]] << 1) | sign;
531 size = dv_vlc_len[dv_vlc_codes[run][level]] + 1;
532 }
533 else {
534 if (level < 23) {
535 *vlc = (dv_vlc_bits[dv_vlc_codes[0][level]] << 1) | sign;
536 size = dv_vlc_len[dv_vlc_codes[0][level]] + 1;
537 } else {
538 *vlc = 0xfe00 | (level << 1) | sign;
539 size = 16;
540 }
541
542 switch(run) {
543 case 0:
544 break;
545 case 1:
546 case 2:
547 *vlc |= ((0x7ce | (run - 1)) << size);
548 size += 11;
549 break;
550 case 3:
551 case 4:
552 case 5:
553 case 6:
554 *vlc |= ((0xfac | (run - 3)) << size);
555 size += 12;
556 break;
557 default:
558 *vlc |= ((0x1f80 | (run - 1)) << size);
559 size += 13;
560 break;
561 }
562 }
563
564 return size;
565}
566
567typedef struct EncBlockInfo {
568 int qno;
569 int cno;
570 int dct_mode;
571 int block_size;
572 DCTELEM *mb;
573 PutBitContext pb;
574} EncBlockInfo;
575
576static inline int dv_bits_left(EncBlockInfo* bi)
577{
578 return (bi->block_size - get_bit_count(&bi->pb));
579}
580
581static inline void dv_encode_ac(EncBlockInfo* bi, PutBitContext* heap)
582{
583 int i, level, size, run = 0;
584 uint32_t vlc;
585 PutBitContext* cpb = &bi->pb;
586
587 for (i=1; i<64; i++) {
588 level = bi->mb[ff_zigzag_direct[i]] /
589 (1<<(dv_quant_shifts[bi->qno + dv_quant_offset[bi->cno]]
590 [dv_88_areas[ff_zigzag_direct[i]]] + 4 + (bi->cno == 3)));
591 if (level != 0) {
592 size = dv_rl2vlc(run, level, &vlc);
593put_vlc:
594
595#ifdef VLC_DEBUG
596 printf(" %3d:%3d", run, level);
597#endif
598 if (cpb == &bi->pb && size > dv_bits_left(bi)) {
599 size -= dv_bits_left(bi);
600 put_bits(cpb, dv_bits_left(bi), vlc >> size);
601 vlc = vlc & ((1<<size)-1);
602 cpb = heap;
603 }
604 put_bits(cpb, size, vlc);
605 run = 0;
606 } else
607 run++;
608 }
609
610 if (i == 64) {
611 size = 4; vlc = 6; /* End Of Block stamp */
612 goto put_vlc;
613 }
614}
615
616static inline void dv_redistr_bits(EncBlockInfo* bi, int count, uint8_t* extra_data, int extra_bits, PutBitContext* heap)
617{
618 int i;
619 GetBitContext gb;
620
621 init_get_bits(&gb, extra_data, extra_bits);
622
623 for (i=0; i<count; i++) {
624 int bits_left = dv_bits_left(bi);
625#ifdef VLC_DEBUG
626 if (bits_left)
627 printf("------------> inserting %d bytes in %d:%d\n", bits_left, i/6, i%6);
628#endif
629 if (bits_left > extra_bits) {
630 bit_copy(&bi->pb, &gb, extra_bits);
631 extra_bits = 0;
632 break;
633 } else
634 bit_copy(&bi->pb, &gb, bits_left);
635
636 extra_bits -= bits_left;
637 bi++;
638 }
639
640 if (extra_bits > 0 && heap)
641 bit_copy(heap, &gb, extra_bits);
642}
643
644static inline void dv_set_class_number(EncBlockInfo* bi, int j)
645{
646 int i, max_ac = 0;
647
648 for (i=1; i<64; i++) {
649 int ac = abs(bi->mb[ff_zigzag_direct[i]]) / 4;
650 if (max_ac < ac)
651 max_ac = ac;
652 }
653 if (max_ac < 12)
654 bi->cno = j;
655 else if (max_ac < 24)
656 bi->cno = j + 1;
657 else if (max_ac < 36)
658 bi->cno = j + 2;
659 else
660 bi->cno = j + 3;
661
662 if (bi->cno > 3)
663 bi->cno = 3;
664}
665
666/*
667 * This is a very rough initial implementaion. The performance is
668 * horrible and some features are missing, mainly 2-4-8 DCT encoding.
669 * The weighting is missing as well, but it's missing from the decoding
670 * step also -- so at least we're on the same page with decoder ;-)
671 */
672static inline void dv_encode_video_segment(DVVideoDecodeContext *s,
673 uint8_t *dif,
674 const uint16_t *mb_pos_ptr)
675{
676 int mb_index, i, j, v;
677 int mb_x, mb_y, c_offset, linesize;
678 uint8_t* y_ptr;
679 uint8_t* data;
680 int do_edge_wrap;
681 DCTELEM *block;
682 EncBlockInfo enc_blks[5*6];
683 EncBlockInfo* enc_blk;
684 int free_vs_bits;
685 int extra_bits;
686 PutBitContext extra_vs;
687 uint8_t extra_vs_data[5*6*128];
688 uint8_t extra_mb_data[6*128];
689
690 int QNO = 15;
691
692 /* Stage 1 -- doing DCT on 5 MBs */
693 block = &s->block[0][0];
694 for(mb_index = 0; mb_index < 5; mb_index++) {
695 v = *mb_pos_ptr++;
696 mb_x = v & 0xff;
697 mb_y = v >> 8;
698 y_ptr = s->picture.data[0] + (mb_y * s->picture.linesize[0] * 8) + (mb_x * 8);
699 c_offset = (s->sys->pix_fmt == PIX_FMT_YUV411P) ?
700 ((mb_y * s->picture.linesize[1] * 8) + ((mb_x >> 2) * 8)) :
701 (((mb_y >> 1) * s->picture.linesize[1] * 8) + ((mb_x >> 1) * 8));
702 do_edge_wrap = 0;
703 for(j = 0;j < 6; j++) {
704 if (j < 4) { /* Four Y blocks */
705 /* NOTE: at end of line, the macroblock is handled as 420 */
706 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x < (704 / 8)) {
707 data = y_ptr + (j * 8);
708 } else {
709 data = y_ptr + ((j & 1) * 8) + ((j >> 1) * 8 * s->picture.linesize[0]);
710 }
711 linesize = s->picture.linesize[0];
712 } else { /* Cr and Cb blocks */
713 /* don't ask Fabrice why they inverted Cb and Cr ! */
714 data = s->picture.data[6 - j] + c_offset;
715 linesize = s->picture.linesize[6 - j];
716 if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8))
717 do_edge_wrap = 1;
718 }
719
720 /* Everything is set up -- now just copy data -> DCT block */
721 if (do_edge_wrap) { /* Edge wrap copy: 4x16 -> 8x8 */
722 uint8_t* d;
723 DCTELEM *b = block;
724 for (i=0;i<8;i++) {
725 d = data + 8 * linesize;
726 b[0] = data[0]; b[1] = data[1]; b[2] = data[2]; b[3] = data[3];
727 b[4] = d[0]; b[5] = d[1]; b[6] = d[2]; b[7] = d[3];
728 data += linesize;
729 b += 8;
730 }
731 } else { /* Simple copy: 8x8 -> 8x8 */
732 s->get_pixels(block, data, linesize);
733 }
734
735 s->fdct(block);
736
737 block += 64;
738 }
739 }
740
741 /* Stage 2 -- setup for encoding phase */
742 enc_blk = &enc_blks[0];
743 block = &s->block[0][0];
744 for (i=0; i<5; i++) {
745 for (j=0; j<6; j++) {
746 enc_blk->mb = block;
747 enc_blk->dct_mode = 0;
748 enc_blk->block_size = block_sizes[j];
749
750 dv_set_class_number(enc_blk, j/4*(j%2));
751
752 block += 64;
753 enc_blk++;
754 }
755 }
756
757 /* Stage 3 -- encoding by trial-and-error */
758encode_vs:
759 enc_blk = &enc_blks[0];
760 for (i=0; i<5; i++) {
761 uint8_t* p = dif + i*80 + 4;
762 for (j=0; j<6; j++) {
763 enc_blk->qno = QNO;
ed7debda 764 init_put_bits(&enc_blk->pb, p, block_sizes[j]/8);
4fa1c4fa
RS
765 enc_blk++;
766 p += block_sizes[j]/8;
767 }
768 }
769
ed7debda 770 init_put_bits(&extra_vs, extra_vs_data, sizeof(extra_vs_data));
4fa1c4fa
RS
771 free_vs_bits = 0;
772 enc_blk = &enc_blks[0];
773 for (i=0; i<5; i++) {
774 PutBitContext extra_mb;
775 EncBlockInfo* enc_blk2 = enc_blk;
776 int free_mb_bits = 0;
777
ed7debda 778 init_put_bits(&extra_mb, extra_mb_data, sizeof(extra_mb_data));
4fa1c4fa
RS
779 dif[i*80 + 3] = enc_blk->qno;
780
781 for (j=0; j<6; j++) {
782 uint16_t dc = ((enc_blk->mb[0] >> 3) - 1024) >> 2;
783
784 put_bits(&enc_blk->pb, 9, dc);
785 put_bits(&enc_blk->pb, 1, enc_blk->dct_mode);
786 put_bits(&enc_blk->pb, 2, enc_blk->cno);
787
788#ifdef VLC_DEBUG
789 printf("[%d, %d]: ", i, j);
790#endif
791 dv_encode_ac(enc_blk, &extra_mb);
792#ifdef VLC_DEBUG
793 printf("\n");
794#endif
795
796 free_mb_bits += dv_bits_left(enc_blk);
797 enc_blk++;
798 }
799
800 /* We can't flush extra_mb just yet -- since it'll round up bit number */
801 extra_bits = get_bit_count(&extra_mb);
802 if (free_mb_bits > extra_bits)
803 free_vs_bits += free_mb_bits - extra_bits;
804
805 if (extra_bits) { /* FIXME: speed up things when free_mb_bits == 0 */
806 flush_put_bits(&extra_mb);
807 dv_redistr_bits(enc_blk2, 6, extra_mb_data, extra_bits, &extra_vs);
808 }
809 }
810
811 /* We can't flush extra_mb just yet -- since it'll round up bit number */
812 extra_bits = get_bit_count(&extra_vs);
813 if (extra_bits > free_vs_bits && QNO) { /* FIXME: very crude trial-and-error */
814 QNO--;
815 goto encode_vs;
816 }
817
818 if (extra_bits) {
819 flush_put_bits(&extra_vs);
820 dv_redistr_bits(&enc_blks[0], 5*6, extra_vs_data, extra_bits, NULL);
821 }
822
823 for (i=0; i<6*5; i++) {
824 flush_put_bits(&enc_blks[i].pb);
825#ifdef VLC_DEBUG
826 printf("[%d:%d] qno=%d cno=%d\n", i/6, i%6, enc_blks[i].qno, enc_blks[i].cno);
827#endif
828 }
829}
830
5d2231ee
FB
831/* NOTE: exactly one frame must be given (120000 bytes for NTSC,
832 144000 bytes for PAL) */
833static int dvvideo_decode_frame(AVCodecContext *avctx,
834 void *data, int *data_size,
0c1a9eda 835 uint8_t *buf, int buf_size)
5d2231ee
FB
836{
837 DVVideoDecodeContext *s = avctx->priv_data;
7458ccbb 838 int ds, vs;
0c1a9eda 839 const uint16_t *mb_pos_ptr;
5d2231ee 840
7458ccbb
RS
841 s->sys = dv_frame_profile(buf);
842 if (!s->sys || buf_size < s->sys->frame_size)
843 return -1; /* NOTE: we only accept several full frames */
e9feea59 844
7458ccbb 845
e20c4069
MN
846 if(s->picture.data[0])
847 avctx->release_buffer(avctx, &s->picture);
848
7458ccbb
RS
849 s->picture.reference = 0;
850 avctx->pix_fmt = s->sys->pix_fmt;
86a7e115
RS
851 avctx->width = s->sys->width;
852 avctx->height = s->sys->height;
1e491e29
MN
853 if(avctx->get_buffer(avctx, &s->picture) < 0) {
854 fprintf(stderr, "get_buffer() failed\n");
855 return -1;
b0397e0e 856 }
2744ca9a
RS
857 s->picture.interlaced_frame = 1;
858 s->picture.bottom_field_first = 1;
b0397e0e 859
5d2231ee 860 /* for each DIF segment */
7458ccbb
RS
861 mb_pos_ptr = s->sys->video_place;
862 for (ds = 0; ds < s->sys->difseg_size; ds++) {
863 buf += 6 * 80; /* skip DIF segment header */
5d2231ee
FB
864
865 for(vs = 0; vs < 27; vs++) {
7458ccbb
RS
866 if ((vs % 3) == 0)
867 buf += 80; /* skip audio block */
868
4fa1c4fa
RS
869#ifdef VLC_DEBUG
870 printf("********************* %d, %d **********************\n", ds, vs);
871#endif
7458ccbb
RS
872 dv_decode_video_segment(s, buf, mb_pos_ptr);
873 buf += 5 * 80;
5d2231ee
FB
874 mb_pos_ptr += 5;
875 }
876 }
877
9e398782
A
878 emms_c();
879
5d2231ee 880 /* return image */
492cd3a9
MN
881 *data_size = sizeof(AVFrame);
882 *(AVFrame*)data= s->picture;
1e491e29 883
7458ccbb 884 return s->sys->frame_size;
5d2231ee
FB
885}
886
4fa1c4fa
RS
887static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
888 void *data)
5d2231ee 889{
4fa1c4fa
RS
890 DVVideoDecodeContext *s = c->priv_data;
891 const uint16_t *mb_pos_ptr;
892 int ds, vs;
893
894 s->sys = dv_codec_profile(c);
895 if (!s->sys)
896 return -1;
897
898 c->pix_fmt = s->sys->pix_fmt;
899 s->picture = *((AVFrame *)data);
900
901 /* for each DIF segment */
902 mb_pos_ptr = s->sys->video_place;
903 for (ds = 0; ds < s->sys->difseg_size; ds++) {
904 buf += 6 * 80; /* skip DIF segment header */
905
906 for(vs = 0; vs < 27; vs++) {
907 if ((vs % 3) == 0)
908 buf += 80; /* skip audio block */
909
910#ifdef VLC_DEBUG
911 printf("********************* %d, %d **********************\n", ds, vs);
912#endif
913 dv_encode_video_segment(s, buf, mb_pos_ptr);
914 buf += 5 * 80;
915 mb_pos_ptr += 5;
916 }
917 }
5d2231ee 918
4fa1c4fa
RS
919 emms_c();
920 return s->sys->frame_size;
921}
922
923static int dvvideo_end(AVCodecContext *avctx)
924{
925 avcodec_default_free_buffers(avctx);
5d2231ee
FB
926 return 0;
927}
928
929AVCodec dvvideo_decoder = {
930 "dvvideo",
931 CODEC_TYPE_VIDEO,
932 CODEC_ID_DVVIDEO,
933 sizeof(DVVideoDecodeContext),
4fa1c4fa
RS
934 dvvideo_init,
935 dvvideo_encode_frame,
936 dvvideo_end,
5d2231ee 937 dvvideo_decode_frame,
b0397e0e 938 CODEC_CAP_DR1,
5d2231ee
FB
939 NULL
940};