~ubuntu-branches/ubuntu/vivid/libav/vivid

« back to all changes in this revision

Viewing changes to libavcodec/dv.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2013-10-22 23:24:08 UTC
  • mfrom: (1.3.36 sid)
  • Revision ID: package-import@ubuntu.com-20131022232408-b8tvvn4pyzri9mi3
Tags: 6:9.10-1ubuntu1
* Build all -extra flavors from this source package, as libav got demoted
  from main to universe, cf LP: #1243235
* Simplify debian/rules to follow exactly the code that debian executes
* New upstream (LP: #1180288) fixes lots of security issues (LP: #1242802)
* Merge from unstable, remaining changes:
  - build-depend on libtiff5-dev rather than libtiff4-dev,
    avoids FTBFS caused by imlib
  - follow the regular debian codepaths

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
#include "avcodec.h"
43
43
#include "dsputil.h"
44
44
#include "get_bits.h"
 
45
#include "internal.h"
45
46
#include "put_bits.h"
46
47
#include "simple_idct.h"
47
48
#include "dvdata.h"
48
49
#include "dv_tablegen.h"
49
50
 
50
 
//#undef NDEBUG
51
 
//#include <assert.h>
52
 
 
53
 
typedef struct DVVideoContext {
54
 
    const DVprofile *sys;
55
 
    AVFrame          picture;
56
 
    AVCodecContext  *avctx;
57
 
    uint8_t         *buf;
58
 
 
59
 
    uint8_t  dv_zigzag[2][64];
60
 
 
61
 
    void (*get_pixels)(DCTELEM *block, const uint8_t *pixels, int line_size);
62
 
    void (*fdct[2])(DCTELEM *block);
63
 
    void (*idct_put[2])(uint8_t *dest, int line_size, DCTELEM *block);
64
 
    me_cmp_func ildct_cmp;
65
 
} DVVideoContext;
66
 
 
67
 
#define TEX_VLC_BITS 9
68
 
 
69
51
/* XXX: also include quantization */
70
 
static RL_VLC_ELEM dv_rl_vlc[1184];
71
 
 
72
 
static inline int dv_work_pool_size(const DVprofile *d)
73
 
{
74
 
    int size = d->n_difchan*d->difseg_size*27;
75
 
    if (DV_PROFILE_IS_1080i50(d))
76
 
        size -= 3*27;
77
 
    if (DV_PROFILE_IS_720p50(d))
78
 
        size -= 4*27;
79
 
    return size;
80
 
}
 
52
RL_VLC_ELEM ff_dv_rl_vlc[1184];
81
53
 
82
54
static inline void dv_calc_mb_coordinates(const DVprofile *d, int chan, int seq, int slot,
83
55
                                          uint16_t *tbl)
165
137
              break;
166
138
        case 720:
167
139
              switch (d->pix_fmt) {
168
 
              case PIX_FMT_YUV422P:
 
140
              case AV_PIX_FMT_YUV422P:
169
141
                   x = shuf3[m] + slot/3;
170
142
                   y = serpent1[slot] +
171
143
                       ((((seq + off[m]) % d->difseg_size)<<1) + chan)*3;
172
144
                   tbl[m] = (x<<1)|(y<<8);
173
145
                   break;
174
 
              case PIX_FMT_YUV420P:
 
146
              case AV_PIX_FMT_YUV420P:
175
147
                   x = shuf3[m] + slot/3;
176
148
                   y = serpent1[slot] +
177
149
                       ((seq + off[m]) % d->difseg_size)*3;
178
150
                   tbl[m] = (x<<1)|(y<<9);
179
151
                   break;
180
 
              case PIX_FMT_YUV411P:
 
152
              case AV_PIX_FMT_YUV411P:
181
153
                   i = (seq + off[m]) % d->difseg_size;
182
154
                   k = slot + ((m==1||m==2)?3:0);
183
155
 
194
166
    }
195
167
}
196
168
 
197
 
static int dv_init_dynamic_tables(const DVprofile *d)
 
169
/* quantization quanta by QNO for DV100 */
 
170
static const uint8_t dv100_qstep[16] = {
 
171
    1, /* QNO = 0 and 1 both have no quantization */
 
172
    1,
 
173
    2, 3, 4, 5, 6, 7, 8, 16, 18, 20, 22, 24, 28, 52
 
174
};
 
175
 
 
176
static const uint8_t dv_quant_areas[4]  = { 6, 21, 43, 64 };
 
177
 
 
178
int ff_dv_init_dynamic_tables(const DVprofile *d)
198
179
{
199
180
    int j,i,c,s,p;
200
181
    uint32_t *factor1, *factor2;
222
203
        factor1 = &d->idct_factor[0];
223
204
        factor2 = &d->idct_factor[DV_PROFILE_IS_HD(d)?4096:2816];
224
205
        if (d->height == 720) {
225
 
            iweight1 = &dv_iweight_720_y[0];
226
 
            iweight2 = &dv_iweight_720_c[0];
 
206
            iweight1 = &ff_dv_iweight_720_y[0];
 
207
            iweight2 = &ff_dv_iweight_720_c[0];
227
208
        } else {
228
 
            iweight1 = &dv_iweight_1080_y[0];
229
 
            iweight2 = &dv_iweight_1080_c[0];
 
209
            iweight1 = &ff_dv_iweight_1080_y[0];
 
210
            iweight2 = &ff_dv_iweight_1080_c[0];
230
211
        }
231
212
        if (DV_PROFILE_IS_HD(d)) {
232
213
            for (c = 0; c < 4; c++) {
238
219
                }
239
220
            }
240
221
        } else {
241
 
            iweight1 = &dv_iweight_88[0];
242
 
            for (j = 0; j < 2; j++, iweight1 = &dv_iweight_248[0]) {
 
222
            iweight1 = &ff_dv_iweight_88[0];
 
223
            for (j = 0; j < 2; j++, iweight1 = &ff_dv_iweight_248[0]) {
243
224
                for (s = 0; s < 22; s++) {
244
225
                    for (i = c = 0; c < 4; c++) {
245
226
                        for (; i < dv_quant_areas[c]; i++) {
246
 
                            *factor1   = iweight1[i] << (dv_quant_shifts[s][c] + 1);
 
227
                            *factor1   = iweight1[i] << (ff_dv_quant_shifts[s][c] + 1);
247
228
                            *factor2++ = (*factor1++) << 1;
248
229
                        }
249
230
                    }
255
236
    return 0;
256
237
}
257
238
 
258
 
static av_cold int dvvideo_init(AVCodecContext *avctx)
 
239
av_cold int ff_dvvideo_init(AVCodecContext *avctx)
259
240
{
260
241
    DVVideoContext *s = avctx->priv_data;
261
242
    DSPContext dsp;
308
289
                run   = new_dv_vlc_run  [code] + 1;
309
290
                level = new_dv_vlc_level[code];
310
291
            }
311
 
            dv_rl_vlc[i].len   = len;
312
 
            dv_rl_vlc[i].level = level;
313
 
            dv_rl_vlc[i].run   = run;
 
292
            ff_dv_rl_vlc[i].len   = len;
 
293
            ff_dv_rl_vlc[i].level = level;
 
294
            ff_dv_rl_vlc[i].run   = run;
314
295
        }
315
296
        ff_free_vlc(&dv_vlc);
316
 
 
317
 
        dv_vlc_map_tableinit();
318
297
    }
319
298
 
320
299
    /* Generic DSP setup */
321
 
    dsputil_init(&dsp, avctx);
 
300
    ff_dsputil_init(&dsp, avctx);
322
301
    ff_set_cmp(&dsp, dsp.ildct_cmp, avctx->ildct_cmp);
323
302
    s->get_pixels = dsp.get_pixels;
324
303
    s->ildct_cmp = dsp.ildct_cmp[5];
332
311
    /* 248DCT setup */
333
312
    s->fdct[1]     = dsp.fdct248;
334
313
    s->idct_put[1] = ff_simple_idct248_put;  // FIXME: need to add it to DSP
335
 
    if (avctx->lowres){
336
 
        for (i = 0; i < 64; i++){
337
 
            int j = ff_zigzag248_direct[i];
338
 
            s->dv_zigzag[1][i] = dsp.idct_permutation[(j & 7) + (j & 8) * 4 + (j & 48) / 2];
339
 
        }
340
 
    }else
341
 
        memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
 
314
    memcpy(s->dv_zigzag[1], ff_zigzag248_direct, 64);
342
315
 
343
316
    avctx->coded_frame = &s->picture;
344
317
    s->avctx = avctx;
350
323
static av_cold int dvvideo_init_encoder(AVCodecContext *avctx)
351
324
{
352
325
    if (!avpriv_dv_codec_profile(avctx)) {
353
 
        av_log(avctx, AV_LOG_ERROR, "Found no DV profile for %ix%i %s video\n",
 
326
        av_log(avctx, AV_LOG_ERROR, "Found no DV profile for %ix%i %s video. "
 
327
               "Valid DV profiles are:\n",
354
328
               avctx->width, avctx->height, av_get_pix_fmt_name(avctx->pix_fmt));
355
 
        return -1;
 
329
        ff_dv_print_profiles(avctx, AV_LOG_ERROR);
 
330
        return AVERROR(EINVAL);
356
331
    }
357
332
 
358
 
    return dvvideo_init(avctx);
 
333
    dv_vlc_map_tableinit();
 
334
 
 
335
    return ff_dvvideo_init(avctx);
359
336
}
360
337
 
361
 
typedef struct BlockInfo {
362
 
    const uint32_t *factor_table;
363
 
    const uint8_t *scan_table;
364
 
    uint8_t pos; /* position in block */
365
 
    void (*idct_put)(uint8_t *dest, int line_size, DCTELEM *block);
366
 
    uint8_t partial_bit_count;
367
 
    uint32_t partial_bit_buffer;
368
 
    int shift_offset;
369
 
} BlockInfo;
370
 
 
371
338
/* bit budget for AC only in 5 MBs */
372
339
static const int vs_total_ac_bits = (100 * 4 + 68*2) * 5;
373
340
static const int mb_area_start[5] = { 1, 6, 21, 43, 64 };
374
341
 
375
 
static inline int put_bits_left(PutBitContext* s)
376
 
{
377
 
    return (s->buf_end - s->buf) * 8 - put_bits_count(s);
378
 
}
379
 
 
380
 
/* decode AC coefficients */
381
 
static void dv_decode_ac(GetBitContext *gb, BlockInfo *mb, DCTELEM *block)
382
 
{
383
 
    int last_index = gb->size_in_bits;
384
 
    const uint8_t  *scan_table   = mb->scan_table;
385
 
    const uint32_t *factor_table = mb->factor_table;
386
 
    int pos               = mb->pos;
387
 
    int partial_bit_count = mb->partial_bit_count;
388
 
    int level, run, vlc_len, index;
389
 
 
390
 
    OPEN_READER(re, gb);
391
 
    UPDATE_CACHE(re, gb);
392
 
 
393
 
    /* if we must parse a partial VLC, we do it here */
394
 
    if (partial_bit_count > 0) {
395
 
        re_cache = re_cache >> partial_bit_count | mb->partial_bit_buffer;
396
 
        re_index -= partial_bit_count;
397
 
        mb->partial_bit_count = 0;
398
 
    }
399
 
 
400
 
    /* get the AC coefficients until last_index is reached */
401
 
    for (;;) {
402
 
        av_dlog(NULL, "%2d: bits=%04x index=%d\n", pos, SHOW_UBITS(re, gb, 16),
403
 
                re_index);
404
 
        /* our own optimized GET_RL_VLC */
405
 
        index   = NEG_USR32(re_cache, TEX_VLC_BITS);
406
 
        vlc_len = dv_rl_vlc[index].len;
407
 
        if (vlc_len < 0) {
408
 
            index = NEG_USR32((unsigned)re_cache << TEX_VLC_BITS, -vlc_len) + dv_rl_vlc[index].level;
409
 
            vlc_len = TEX_VLC_BITS - vlc_len;
410
 
        }
411
 
        level = dv_rl_vlc[index].level;
412
 
        run   = dv_rl_vlc[index].run;
413
 
 
414
 
        /* gotta check if we're still within gb boundaries */
415
 
        if (re_index + vlc_len > last_index) {
416
 
            /* should be < 16 bits otherwise a codeword could have been parsed */
417
 
            mb->partial_bit_count = last_index - re_index;
418
 
            mb->partial_bit_buffer = re_cache & ~(-1u >> mb->partial_bit_count);
419
 
            re_index = last_index;
420
 
            break;
421
 
        }
422
 
        re_index += vlc_len;
423
 
 
424
 
        av_dlog(NULL, "run=%d level=%d\n", run, level);
425
 
        pos += run;
426
 
        if (pos >= 64)
427
 
            break;
428
 
 
429
 
        level = (level * factor_table[pos] + (1 << (dv_iweight_bits - 1))) >> dv_iweight_bits;
430
 
        block[scan_table[pos]] = level;
431
 
 
432
 
        UPDATE_CACHE(re, gb);
433
 
    }
434
 
    CLOSE_READER(re, gb);
435
 
    mb->pos = pos;
436
 
}
437
 
 
438
 
static inline void bit_copy(PutBitContext *pb, GetBitContext *gb)
439
 
{
440
 
    int bits_left = get_bits_left(gb);
441
 
    while (bits_left >= MIN_CACHE_BITS) {
442
 
        put_bits(pb, MIN_CACHE_BITS, get_bits(gb, MIN_CACHE_BITS));
443
 
        bits_left -= MIN_CACHE_BITS;
444
 
    }
445
 
    if (bits_left > 0) {
446
 
        put_bits(pb, bits_left, get_bits(gb, bits_left));
447
 
    }
448
 
}
449
 
 
450
 
static inline void dv_calculate_mb_xy(DVVideoContext *s, DVwork_chunk *work_chunk, int m, int *mb_x, int *mb_y)
451
 
{
452
 
     *mb_x = work_chunk->mb_coordinates[m] & 0xff;
453
 
     *mb_y = work_chunk->mb_coordinates[m] >> 8;
454
 
 
455
 
     /* We work with 720p frames split in half. The odd half-frame (chan==2,3) is displaced :-( */
456
 
     if (s->sys->height == 720 && !(s->buf[1]&0x0C)) {
457
 
         *mb_y -= (*mb_y>17)?18:-72; /* shifting the Y coordinate down by 72/2 macro blocks */
458
 
     }
459
 
}
460
 
 
461
 
/* mb_x and mb_y are in units of 8 pixels */
462
 
static int dv_decode_video_segment(AVCodecContext *avctx, void *arg)
463
 
{
464
 
    DVVideoContext *s = avctx->priv_data;
465
 
    DVwork_chunk *work_chunk = arg;
466
 
    int quant, dc, dct_mode, class1, j;
467
 
    int mb_index, mb_x, mb_y, last_index;
468
 
    int y_stride, linesize;
469
 
    DCTELEM *block, *block1;
470
 
    int c_offset;
471
 
    uint8_t *y_ptr;
472
 
    const uint8_t *buf_ptr;
473
 
    PutBitContext pb, vs_pb;
474
 
    GetBitContext gb;
475
 
    BlockInfo mb_data[5 * DV_MAX_BPM], *mb, *mb1;
476
 
    LOCAL_ALIGNED_16(DCTELEM, sblock, [5*DV_MAX_BPM], [64]);
477
 
    LOCAL_ALIGNED_16(uint8_t, mb_bit_buffer, [  80 + FF_INPUT_BUFFER_PADDING_SIZE]); /* allow some slack */
478
 
    LOCAL_ALIGNED_16(uint8_t, vs_bit_buffer, [5*80 + FF_INPUT_BUFFER_PADDING_SIZE]); /* allow some slack */
479
 
    const int log2_blocksize = 3-s->avctx->lowres;
480
 
    int is_field_mode[5];
481
 
 
482
 
    assert((((int)mb_bit_buffer) & 7) == 0);
483
 
    assert((((int)vs_bit_buffer) & 7) == 0);
484
 
 
485
 
    memset(sblock, 0, 5*DV_MAX_BPM*sizeof(*sblock));
486
 
 
487
 
    /* pass 1: read DC and AC coefficients in blocks */
488
 
    buf_ptr = &s->buf[work_chunk->buf_offset*80];
489
 
    block1  = &sblock[0][0];
490
 
    mb1     = mb_data;
491
 
    init_put_bits(&vs_pb, vs_bit_buffer, 5 * 80);
492
 
    for (mb_index = 0; mb_index < 5; mb_index++, mb1 += s->sys->bpm, block1 += s->sys->bpm * 64) {
493
 
        /* skip header */
494
 
        quant = buf_ptr[3] & 0x0f;
495
 
        buf_ptr += 4;
496
 
        init_put_bits(&pb, mb_bit_buffer, 80);
497
 
        mb    = mb1;
498
 
        block = block1;
499
 
        is_field_mode[mb_index] = 0;
500
 
        for (j = 0; j < s->sys->bpm; j++) {
501
 
            last_index = s->sys->block_sizes[j];
502
 
            init_get_bits(&gb, buf_ptr, last_index);
503
 
 
504
 
            /* get the DC */
505
 
            dc       = get_sbits(&gb, 9);
506
 
            dct_mode = get_bits1(&gb);
507
 
            class1   = get_bits(&gb, 2);
508
 
            if (DV_PROFILE_IS_HD(s->sys)) {
509
 
                mb->idct_put     = s->idct_put[0];
510
 
                mb->scan_table   = s->dv_zigzag[0];
511
 
                mb->factor_table = &s->sys->idct_factor[(j >= 4)*4*16*64 + class1*16*64 + quant*64];
512
 
                is_field_mode[mb_index] |= !j && dct_mode;
513
 
            } else {
514
 
                mb->idct_put     = s->idct_put[dct_mode && log2_blocksize == 3];
515
 
                mb->scan_table   = s->dv_zigzag[dct_mode];
516
 
                mb->factor_table = &s->sys->idct_factor[(class1 == 3)*2*22*64 + dct_mode*22*64 +
517
 
                                                        (quant + dv_quant_offset[class1])*64];
518
 
            }
519
 
            dc = dc << 2;
520
 
            /* convert to unsigned because 128 is not added in the
521
 
               standard IDCT */
522
 
            dc += 1024;
523
 
            block[0] = dc;
524
 
            buf_ptr += last_index >> 3;
525
 
            mb->pos               = 0;
526
 
            mb->partial_bit_count = 0;
527
 
 
528
 
            av_dlog(avctx, "MB block: %d, %d ", mb_index, j);
529
 
            dv_decode_ac(&gb, mb, block);
530
 
 
531
 
            /* write the remaining bits in a new buffer only if the
532
 
               block is finished */
533
 
            if (mb->pos >= 64)
534
 
                bit_copy(&pb, &gb);
535
 
 
536
 
            block += 64;
537
 
            mb++;
538
 
        }
539
 
 
540
 
        /* pass 2: we can do it just after */
541
 
        av_dlog(avctx, "***pass 2 size=%d MB#=%d\n", put_bits_count(&pb), mb_index);
542
 
        block = block1;
543
 
        mb    = mb1;
544
 
        init_get_bits(&gb, mb_bit_buffer, put_bits_count(&pb));
545
 
        put_bits32(&pb, 0); // padding must be zeroed
546
 
        flush_put_bits(&pb);
547
 
        for (j = 0; j < s->sys->bpm; j++, block += 64, mb++) {
548
 
            if (mb->pos < 64 && get_bits_left(&gb) > 0) {
549
 
                dv_decode_ac(&gb, mb, block);
550
 
                /* if still not finished, no need to parse other blocks */
551
 
                if (mb->pos < 64)
552
 
                    break;
553
 
            }
554
 
        }
555
 
        /* all blocks are finished, so the extra bytes can be used at
556
 
           the video segment level */
557
 
        if (j >= s->sys->bpm)
558
 
            bit_copy(&vs_pb, &gb);
559
 
    }
560
 
 
561
 
    /* we need a pass over the whole video segment */
562
 
    av_dlog(avctx, "***pass 3 size=%d\n", put_bits_count(&vs_pb));
563
 
    block = &sblock[0][0];
564
 
    mb    = mb_data;
565
 
    init_get_bits(&gb, vs_bit_buffer, put_bits_count(&vs_pb));
566
 
    put_bits32(&vs_pb, 0); // padding must be zeroed
567
 
    flush_put_bits(&vs_pb);
568
 
    for (mb_index = 0; mb_index < 5; mb_index++) {
569
 
        for (j = 0; j < s->sys->bpm; j++) {
570
 
            if (mb->pos < 64) {
571
 
                av_dlog(avctx, "start %d:%d\n", mb_index, j);
572
 
                dv_decode_ac(&gb, mb, block);
573
 
            }
574
 
            if (mb->pos >= 64 && mb->pos < 127)
575
 
                av_log(avctx, AV_LOG_ERROR, "AC EOB marker is absent pos=%d\n", mb->pos);
576
 
            block += 64;
577
 
            mb++;
578
 
        }
579
 
    }
580
 
 
581
 
    /* compute idct and place blocks */
582
 
    block = &sblock[0][0];
583
 
    mb    = mb_data;
584
 
    for (mb_index = 0; mb_index < 5; mb_index++) {
585
 
        dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
586
 
 
587
 
        /* idct_put'ting luminance */
588
 
        if ((s->sys->pix_fmt == PIX_FMT_YUV420P) ||
589
 
            (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
590
 
            (s->sys->height >= 720 && mb_y != 134)) {
591
 
            y_stride = (s->picture.linesize[0] << ((!is_field_mode[mb_index]) * log2_blocksize));
592
 
        } else {
593
 
            y_stride = (2 << log2_blocksize);
594
 
        }
595
 
        y_ptr = s->picture.data[0] + ((mb_y * s->picture.linesize[0] + mb_x) << log2_blocksize);
596
 
        linesize = s->picture.linesize[0] << is_field_mode[mb_index];
597
 
        mb[0]    .idct_put(y_ptr                                   , linesize, block + 0*64);
598
 
        if (s->sys->video_stype == 4) { /* SD 422 */
599
 
            mb[2].idct_put(y_ptr + (1 << log2_blocksize)           , linesize, block + 2*64);
600
 
        } else {
601
 
            mb[1].idct_put(y_ptr + (1 << log2_blocksize)           , linesize, block + 1*64);
602
 
            mb[2].idct_put(y_ptr                         + y_stride, linesize, block + 2*64);
603
 
            mb[3].idct_put(y_ptr + (1 << log2_blocksize) + y_stride, linesize, block + 3*64);
604
 
        }
605
 
        mb += 4;
606
 
        block += 4*64;
607
 
 
608
 
        /* idct_put'ting chrominance */
609
 
        c_offset = (((mb_y >>  (s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] +
610
 
                     (mb_x >> ((s->sys->pix_fmt == PIX_FMT_YUV411P) ? 2 : 1))) << log2_blocksize);
611
 
        for (j = 2; j; j--) {
612
 
            uint8_t *c_ptr = s->picture.data[j] + c_offset;
613
 
            if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
614
 
                  uint64_t aligned_pixels[64/8];
615
 
                  uint8_t *pixels = (uint8_t*)aligned_pixels;
616
 
                  uint8_t *c_ptr1, *ptr1;
617
 
                  int x, y;
618
 
                  mb->idct_put(pixels, 8, block);
619
 
                  for (y = 0; y < (1 << log2_blocksize); y++, c_ptr += s->picture.linesize[j], pixels += 8) {
620
 
                      ptr1   = pixels + (1 << (log2_blocksize - 1));
621
 
                      c_ptr1 = c_ptr + (s->picture.linesize[j] << log2_blocksize);
622
 
                      for (x = 0; x < (1 << (log2_blocksize - 1)); x++) {
623
 
                          c_ptr[x]  = pixels[x];
624
 
                          c_ptr1[x] = ptr1[x];
625
 
                      }
626
 
                  }
627
 
                  block += 64; mb++;
628
 
            } else {
629
 
                  y_stride = (mb_y == 134) ? (1 << log2_blocksize) :
630
 
                                             s->picture.linesize[j] << ((!is_field_mode[mb_index]) * log2_blocksize);
631
 
                  linesize = s->picture.linesize[j] << is_field_mode[mb_index];
632
 
                  (mb++)->    idct_put(c_ptr           , linesize, block); block += 64;
633
 
                  if (s->sys->bpm == 8) {
634
 
                      (mb++)->idct_put(c_ptr + y_stride, linesize, block); block += 64;
635
 
                  }
636
 
            }
637
 
        }
638
 
    }
639
 
    return 0;
640
 
}
641
 
 
642
342
#if CONFIG_SMALL
643
343
/* Converts run and level (where level != 0) pair into VLC, returning bit size */
644
344
static av_always_inline int dv_rl2vlc(int run, int level, int sign, uint32_t* vlc)
764
464
    return 0;
765
465
}
766
466
 
 
467
static const int dv_weight_bits = 18;
 
468
static const int dv_weight_88[64] = {
 
469
 131072, 257107, 257107, 242189, 252167, 242189, 235923, 237536,
 
470
 237536, 235923, 229376, 231390, 223754, 231390, 229376, 222935,
 
471
 224969, 217965, 217965, 224969, 222935, 200636, 218652, 211916,
 
472
 212325, 211916, 218652, 200636, 188995, 196781, 205965, 206433,
 
473
 206433, 205965, 196781, 188995, 185364, 185364, 200636, 200704,
 
474
 200636, 185364, 185364, 174609, 180568, 195068, 195068, 180568,
 
475
 174609, 170091, 175557, 189591, 175557, 170091, 165371, 170627,
 
476
 170627, 165371, 160727, 153560, 160727, 144651, 144651, 136258,
 
477
};
 
478
static const int dv_weight_248[64] = {
 
479
 131072, 242189, 257107, 237536, 229376, 200636, 242189, 223754,
 
480
 224969, 196781, 262144, 242189, 229376, 200636, 257107, 237536,
 
481
 211916, 185364, 235923, 217965, 229376, 211916, 206433, 180568,
 
482
 242189, 223754, 224969, 196781, 211916, 185364, 235923, 217965,
 
483
 200704, 175557, 222935, 205965, 200636, 185364, 195068, 170627,
 
484
 229376, 211916, 206433, 180568, 200704, 175557, 222935, 205965,
 
485
 175557, 153560, 188995, 174609, 165371, 144651, 200636, 185364,
 
486
 195068, 170627, 175557, 153560, 188995, 174609, 165371, 144651,
 
487
};
 
488
 
767
489
static av_always_inline int dv_init_enc_block(EncBlockInfo* bi, uint8_t *data, int linesize, DVVideoContext *s, int bias)
768
490
{
769
491
    const int *weight;
875
597
          size[i] = 0;
876
598
          for (j = 0; j < 6; j++, b++) {
877
599
             for (a = 0; a < 4; a++) {
878
 
                if (b->area_q[a] != dv_quant_shifts[qnos[i] + dv_quant_offset[b->cno]][a]) {
 
600
                if (b->area_q[a] != ff_dv_quant_shifts[qnos[i] + ff_dv_quant_offset[b->cno]][a]) {
879
601
                    b->bit_size[a] = 1; // 4 areas 4 bits for EOB :)
880
602
                    b->area_q[a]++;
881
603
                    prev = b->prev[a];
935
657
    int mb_x, mb_y, c_offset, linesize, y_stride;
936
658
    uint8_t*  y_ptr;
937
659
    uint8_t*  dif;
938
 
    LOCAL_ALIGNED_8(uint8_t, scratch, [64]);
 
660
    LOCAL_ALIGNED_8(uint8_t, scratch, [128]);
939
661
    EncBlockInfo  enc_blks[5*DV_MAX_BPM];
940
662
    PutBitContext pbs[5*DV_MAX_BPM];
941
663
    PutBitContext* pb;
950
672
        dv_calculate_mb_xy(s, work_chunk, mb_index, &mb_x, &mb_y);
951
673
 
952
674
        /* initializing luminance blocks */
953
 
        if ((s->sys->pix_fmt == PIX_FMT_YUV420P) ||
954
 
            (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
 
675
        if ((s->sys->pix_fmt == AV_PIX_FMT_YUV420P) ||
 
676
            (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) ||
955
677
            (s->sys->height >= 720 && mb_y != 134)) {
956
678
            y_stride = s->picture.linesize[0] << 3;
957
679
        } else {
976
698
        enc_blk += 4;
977
699
 
978
700
        /* initializing chrominance blocks */
979
 
        c_offset = (((mb_y >>  (s->sys->pix_fmt == PIX_FMT_YUV420P)) * s->picture.linesize[1] +
980
 
                     (mb_x >> ((s->sys->pix_fmt == PIX_FMT_YUV411P) ? 2 : 1))) << 3);
 
701
        c_offset = (((mb_y >>  (s->sys->pix_fmt == AV_PIX_FMT_YUV420P)) * s->picture.linesize[1] +
 
702
                     (mb_x >> ((s->sys->pix_fmt == AV_PIX_FMT_YUV411P) ? 2 : 1))) << 3);
981
703
        for (j = 2; j; j--) {
982
704
            uint8_t *c_ptr = s->picture.data[j] + c_offset;
983
705
            linesize = s->picture.linesize[j];
984
706
            y_stride = (mb_y == 134) ? 8 : (s->picture.linesize[j] << 3);
985
 
            if (s->sys->pix_fmt == PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
 
707
            if (s->sys->pix_fmt == AV_PIX_FMT_YUV411P && mb_x >= (704 / 8)) {
986
708
                uint8_t* d;
987
709
                uint8_t* b = scratch;
988
710
                for (i = 0; i < 8; i++) {
990
712
                    b[0] = c_ptr[0]; b[1] = c_ptr[1]; b[2] = c_ptr[2]; b[3] = c_ptr[3];
991
713
                    b[4] =     d[0]; b[5] =     d[1]; b[6] =     d[2]; b[7] =     d[3];
992
714
                    c_ptr += linesize;
993
 
                    b += 8;
 
715
                    b += 16;
994
716
                }
995
717
                c_ptr = scratch;
996
 
                linesize = 8;
 
718
                linesize = 16;
997
719
            }
998
720
 
999
721
            vs_bit_size += dv_init_enc_block(    enc_blk++, c_ptr           , linesize, s, 1);
1058
780
    return 0;
1059
781
}
1060
782
 
1061
 
#if CONFIG_DVVIDEO_DECODER
1062
 
/* NOTE: exactly one frame must be given (120000 bytes for NTSC,
1063
 
   144000 bytes for PAL - or twice those for 50Mbps) */
1064
 
static int dvvideo_decode_frame(AVCodecContext *avctx,
1065
 
                                 void *data, int *data_size,
1066
 
                                 AVPacket *avpkt)
1067
 
{
1068
 
    const uint8_t *buf = avpkt->data;
1069
 
    int buf_size = avpkt->size;
1070
 
    DVVideoContext *s = avctx->priv_data;
1071
 
    const uint8_t* vsc_pack;
1072
 
    int apt, is16_9;
1073
 
 
1074
 
    s->sys = avpriv_dv_frame_profile(s->sys, buf, buf_size);
1075
 
    if (!s->sys || buf_size < s->sys->frame_size || dv_init_dynamic_tables(s->sys)) {
1076
 
        av_log(avctx, AV_LOG_ERROR, "could not find dv frame profile\n");
1077
 
        return -1; /* NOTE: we only accept several full frames */
1078
 
    }
1079
 
 
1080
 
    if (s->picture.data[0])
1081
 
        avctx->release_buffer(avctx, &s->picture);
1082
 
 
1083
 
    s->picture.reference = 0;
1084
 
    s->picture.key_frame = 1;
1085
 
    s->picture.pict_type = AV_PICTURE_TYPE_I;
1086
 
    avctx->pix_fmt   = s->sys->pix_fmt;
1087
 
    avctx->time_base = s->sys->time_base;
1088
 
    avcodec_set_dimensions(avctx, s->sys->width, s->sys->height);
1089
 
    if (avctx->get_buffer(avctx, &s->picture) < 0) {
1090
 
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1091
 
        return -1;
1092
 
    }
1093
 
    s->picture.interlaced_frame = 1;
1094
 
    s->picture.top_field_first  = 0;
1095
 
 
1096
 
    s->buf = buf;
1097
 
    avctx->execute(avctx, dv_decode_video_segment, s->sys->work_chunks, NULL,
1098
 
                   dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
1099
 
 
1100
 
    emms_c();
1101
 
 
1102
 
    /* return image */
1103
 
    *data_size = sizeof(AVFrame);
1104
 
    *(AVFrame*)data = s->picture;
1105
 
 
1106
 
    /* Determine the codec's sample_aspect ratio from the packet */
1107
 
    vsc_pack = buf + 80*5 + 48 + 5;
1108
 
    if ( *vsc_pack == dv_video_control ) {
1109
 
        apt = buf[4] & 0x07;
1110
 
        is16_9 = (vsc_pack && ((vsc_pack[2] & 0x07) == 0x02 || (!apt && (vsc_pack[2] & 0x07) == 0x07)));
1111
 
        avctx->sample_aspect_ratio = s->sys->sar[is16_9];
1112
 
    }
1113
 
 
1114
 
    return s->sys->frame_size;
1115
 
}
1116
 
#endif /* CONFIG_DVVIDEO_DECODER */
1117
 
 
1118
 
 
1119
783
static inline int dv_write_pack(enum dv_pack_type pack_id, DVVideoContext *c,
1120
784
                                uint8_t* buf)
1121
785
{
1137
801
     *   2. It is not at all clear what STYPE is used for 4:2:0 PAL
1138
802
     *      compression scheme (if any).
1139
803
     */
1140
 
    int apt   = (c->sys->pix_fmt == PIX_FMT_YUV420P ? 0 : 1);
 
804
    int apt   = (c->sys->pix_fmt == AV_PIX_FMT_YUV420P ? 0 : 1);
1141
805
 
1142
806
    uint8_t aspect = 0;
1143
807
    if ((int)(av_q2d(c->avctx->sample_aspect_ratio) * c->avctx->width / c->avctx->height * 10) >= 17) /* 16:9 */
1189
853
}
1190
854
 
1191
855
#if CONFIG_DVVIDEO_ENCODER
 
856
static inline int dv_write_dif_id(enum dv_section_type t, uint8_t chan_num,
 
857
                                  uint8_t seq_num, uint8_t dif_num,
 
858
                                  uint8_t* buf)
 
859
{
 
860
    buf[0] = (uint8_t)t;       /* Section type */
 
861
    buf[1] = (seq_num  << 4) | /* DIF seq number 0-9 for 525/60; 0-11 for 625/50 */
 
862
             (chan_num << 3) | /* FSC: for 50Mb/s 0 - first channel; 1 - second */
 
863
             7;                /* reserved -- always 1 */
 
864
    buf[2] = dif_num;          /* DIF block number Video: 0-134, Audio: 0-8 */
 
865
    return 3;
 
866
}
 
867
 
 
868
 
 
869
static inline int dv_write_ssyb_id(uint8_t syb_num, uint8_t fr, uint8_t* buf)
 
870
{
 
871
    if (syb_num == 0 || syb_num == 6) {
 
872
        buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
 
873
                 (0  << 4) | /* AP3 (Subcode application ID) */
 
874
                 0x0f;       /* reserved -- always 1 */
 
875
    }
 
876
    else if (syb_num == 11) {
 
877
        buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
 
878
                 0x7f;       /* reserved -- always 1 */
 
879
    }
 
880
    else {
 
881
        buf[0] = (fr << 7) | /* FR ID 1 - first half of each channel; 0 - second */
 
882
                 (0  << 4) | /* APT (Track application ID) */
 
883
                 0x0f;       /* reserved -- always 1 */
 
884
    }
 
885
    buf[1] = 0xf0 |            /* reserved -- always 1 */
 
886
             (syb_num & 0x0f); /* SSYB number 0 - 11   */
 
887
    buf[2] = 0xff;             /* reserved -- always 1 */
 
888
    return 3;
 
889
}
 
890
 
1192
891
static void dv_format_frame(DVVideoContext* c, uint8_t* buf)
1193
892
{
1194
893
    int chan, i, j, k;
1239
938
}
1240
939
 
1241
940
 
1242
 
static int dvvideo_encode_frame(AVCodecContext *c, uint8_t *buf, int buf_size,
1243
 
                                void *data)
 
941
static int dvvideo_encode_frame(AVCodecContext *c, AVPacket *pkt,
 
942
                                const AVFrame *frame, int *got_packet)
1244
943
{
1245
944
    DVVideoContext *s = c->priv_data;
 
945
    int ret;
1246
946
 
1247
947
    s->sys = avpriv_dv_codec_profile(c);
1248
 
    if (!s->sys || buf_size < s->sys->frame_size || dv_init_dynamic_tables(s->sys))
 
948
    if (!s->sys || ff_dv_init_dynamic_tables(s->sys))
1249
949
        return -1;
 
950
    if ((ret = ff_alloc_packet(pkt, s->sys->frame_size)) < 0) {
 
951
        av_log(c, AV_LOG_ERROR, "Error getting output packet.\n");
 
952
        return ret;
 
953
    }
1250
954
 
1251
955
    c->pix_fmt           = s->sys->pix_fmt;
1252
 
    s->picture           = *((AVFrame *)data);
 
956
    s->picture           = *frame;
1253
957
    s->picture.key_frame = 1;
1254
958
    s->picture.pict_type = AV_PICTURE_TYPE_I;
1255
959
 
1256
 
    s->buf = buf;
 
960
    s->buf = pkt->data;
1257
961
    c->execute(c, dv_encode_video_segment, s->sys->work_chunks, NULL,
1258
962
               dv_work_pool_size(s->sys), sizeof(DVwork_chunk));
1259
963
 
1260
964
    emms_c();
1261
965
 
1262
 
    dv_format_frame(s, buf);
1263
 
 
1264
 
    return s->sys->frame_size;
1265
 
}
1266
 
#endif
1267
 
 
1268
 
static int dvvideo_close(AVCodecContext *c)
1269
 
{
1270
 
    DVVideoContext *s = c->priv_data;
1271
 
 
1272
 
    if (s->picture.data[0])
1273
 
        c->release_buffer(c, &s->picture);
 
966
    dv_format_frame(s, pkt->data);
 
967
 
 
968
    pkt->flags |= AV_PKT_FLAG_KEY;
 
969
    *got_packet = 1;
1274
970
 
1275
971
    return 0;
1276
972
}
1277
973
 
1278
 
 
1279
 
#if CONFIG_DVVIDEO_ENCODER
1280
974
AVCodec ff_dvvideo_encoder = {
1281
975
    .name           = "dvvideo",
1282
976
    .type           = AVMEDIA_TYPE_VIDEO,
1283
 
    .id             = CODEC_ID_DVVIDEO,
 
977
    .id             = AV_CODEC_ID_DVVIDEO,
1284
978
    .priv_data_size = sizeof(DVVideoContext),
1285
979
    .init           = dvvideo_init_encoder,
1286
 
    .encode         = dvvideo_encode_frame,
1287
 
    .capabilities = CODEC_CAP_SLICE_THREADS,
1288
 
    .pix_fmts  = (const enum PixelFormat[]) {PIX_FMT_YUV411P, PIX_FMT_YUV422P, PIX_FMT_YUV420P, PIX_FMT_NONE},
1289
 
    .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
 
980
    .encode2        = dvvideo_encode_frame,
 
981
    .capabilities   = CODEC_CAP_SLICE_THREADS,
 
982
    .pix_fmts       = (const enum AVPixelFormat[]) {
 
983
        AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
 
984
    },
 
985
    .long_name      = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
1290
986
};
1291
987
#endif // CONFIG_DVVIDEO_ENCODER
1292
 
 
1293
 
#if CONFIG_DVVIDEO_DECODER
1294
 
AVCodec ff_dvvideo_decoder = {
1295
 
    .name           = "dvvideo",
1296
 
    .type           = AVMEDIA_TYPE_VIDEO,
1297
 
    .id             = CODEC_ID_DVVIDEO,
1298
 
    .priv_data_size = sizeof(DVVideoContext),
1299
 
    .init           = dvvideo_init,
1300
 
    .close          = dvvideo_close,
1301
 
    .decode         = dvvideo_decode_frame,
1302
 
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_SLICE_THREADS,
1303
 
    .max_lowres = 3,
1304
 
    .long_name = NULL_IF_CONFIG_SMALL("DV (Digital Video)"),
1305
 
};
1306
 
#endif