~medibuntu-maintainers/mplayer/medibuntu.precise

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/vp3.c

  • Committer: Package Import Robot
  • Author(s): Reinhard Tartler
  • Date: 2012-01-12 22:23:28 UTC
  • mfrom: (0.4.7 sid)
  • mto: This revision was merged to the branch mainline in revision 76.
  • Revision ID: package-import@ubuntu.com-20120112222328-8jqdyodym3p84ygu
Tags: 2:1.0~rc4.dfsg1+svn34540-1
* New upstream snapshot
* upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
 
36
36
#include "libavutil/imgutils.h"
37
37
#include "avcodec.h"
 
38
#include "internal.h"
38
39
#include "dsputil.h"
39
40
#include "get_bits.h"
40
41
 
44
45
 
45
46
#define FRAGMENT_PIXELS 8
46
47
 
47
 
static av_cold int vp3_decode_end(AVCodecContext *avctx);
48
 
 
49
48
//FIXME split things out into their own arrays
50
49
typedef struct Vp3Fragment {
51
50
    int16_t dc;
225
224
 
226
225
    /* these arrays need to be on 16-byte boundaries since SSE2 operations
227
226
     * index into them */
228
 
    DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64];     //<qmat[qpi][is_inter][plane]
 
227
    DECLARE_ALIGNED(16, int16_t, qmat)[3][2][3][64];     ///< qmat[qpi][is_inter][plane]
229
228
 
230
229
    /* This table contains superblock_count * 16 entries. Each set of 16
231
230
     * numbers corresponds to the fragment indexes 0..15 of the superblock.
254
253
 * VP3 specific functions
255
254
 ************************************************************************/
256
255
 
 
256
static void vp3_decode_flush(AVCodecContext *avctx)
 
257
{
 
258
    Vp3DecodeContext *s = avctx->priv_data;
 
259
 
 
260
    if (s->golden_frame.data[0]) {
 
261
        if (s->golden_frame.data[0] == s->last_frame.data[0])
 
262
            memset(&s->last_frame, 0, sizeof(AVFrame));
 
263
        if (s->current_frame.data[0] == s->golden_frame.data[0])
 
264
            memset(&s->current_frame, 0, sizeof(AVFrame));
 
265
        ff_thread_release_buffer(avctx, &s->golden_frame);
 
266
    }
 
267
    if (s->last_frame.data[0]) {
 
268
        if (s->current_frame.data[0] == s->last_frame.data[0])
 
269
            memset(&s->current_frame, 0, sizeof(AVFrame));
 
270
        ff_thread_release_buffer(avctx, &s->last_frame);
 
271
    }
 
272
    if (s->current_frame.data[0])
 
273
        ff_thread_release_buffer(avctx, &s->current_frame);
 
274
}
 
275
 
 
276
static av_cold int vp3_decode_end(AVCodecContext *avctx)
 
277
{
 
278
    Vp3DecodeContext *s = avctx->priv_data;
 
279
    int i;
 
280
 
 
281
    av_free(s->superblock_coding);
 
282
    av_free(s->all_fragments);
 
283
    av_free(s->coded_fragment_list[0]);
 
284
    av_free(s->dct_tokens_base);
 
285
    av_free(s->superblock_fragments);
 
286
    av_free(s->macroblock_coding);
 
287
    av_free(s->motion_val[0]);
 
288
    av_free(s->motion_val[1]);
 
289
    av_free(s->edge_emu_buffer);
 
290
 
 
291
    if (avctx->internal->is_copy)
 
292
        return 0;
 
293
 
 
294
    for (i = 0; i < 16; i++) {
 
295
        free_vlc(&s->dc_vlc[i]);
 
296
        free_vlc(&s->ac_vlc_1[i]);
 
297
        free_vlc(&s->ac_vlc_2[i]);
 
298
        free_vlc(&s->ac_vlc_3[i]);
 
299
        free_vlc(&s->ac_vlc_4[i]);
 
300
    }
 
301
 
 
302
    free_vlc(&s->superblock_run_length_vlc);
 
303
    free_vlc(&s->fragment_run_length_vlc);
 
304
    free_vlc(&s->mode_code_vlc);
 
305
    free_vlc(&s->motion_vector_vlc);
 
306
 
 
307
    /* release all frames */
 
308
    vp3_decode_flush(avctx);
 
309
 
 
310
    return 0;
 
311
}
 
312
 
257
313
/*
258
314
 * This function sets up all of the various blocks mappings:
259
315
 * superblocks <-> fragments, macroblocks <-> fragments,
890
946
            /* decode a VLC into a token */
891
947
            token = get_vlc2(gb, vlc_table, 11, 3);
892
948
            /* use the token to get a zero run, a coefficient, and an eob run */
893
 
            if (token <= 6) {
 
949
            if ((unsigned) token <= 6U) {
894
950
                eob_run = eob_run_base[token];
895
951
                if (eob_run_get_bits[token])
896
952
                    eob_run += get_bits(gb, eob_run_get_bits[token]);
908
964
                    coeff_i        += eob_run;
909
965
                    eob_run = 0;
910
966
                }
911
 
            } else {
 
967
            } else if (token >= 0) {
912
968
                bits_to_get = coeff_get_bits[token];
913
969
                if (bits_to_get)
914
970
                    bits_to_get = get_bits(gb, bits_to_get);
942
998
                for (i = coeff_index+1; i <= coeff_index+zero_run; i++)
943
999
                    s->num_coded_frags[plane][i]--;
944
1000
                coeff_i++;
 
1001
            } else {
 
1002
                av_log(s->avctx, AV_LOG_ERROR,
 
1003
                       "Invalid token %d\n", token);
 
1004
                return -1;
945
1005
            }
946
1006
    }
947
1007
 
991
1051
    /* unpack the Y plane DC coefficients */
992
1052
    residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_y_table], 0,
993
1053
        0, residual_eob_run);
 
1054
    if (residual_eob_run < 0)
 
1055
        return residual_eob_run;
994
1056
 
995
1057
    /* reverse prediction of the Y-plane DC coefficients */
996
1058
    reverse_dc_prediction(s, 0, s->fragment_width[0], s->fragment_height[0]);
998
1060
    /* unpack the C plane DC coefficients */
999
1061
    residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1000
1062
        1, residual_eob_run);
 
1063
    if (residual_eob_run < 0)
 
1064
        return residual_eob_run;
1001
1065
    residual_eob_run = unpack_vlcs(s, gb, &s->dc_vlc[dc_c_table], 0,
1002
1066
        2, residual_eob_run);
 
1067
    if (residual_eob_run < 0)
 
1068
        return residual_eob_run;
1003
1069
 
1004
1070
    /* reverse prediction of the C-plane DC coefficients */
1005
1071
    if (!(s->avctx->flags & CODEC_FLAG_GRAY))
1036
1102
    for (i = 1; i <= 63; i++) {
1037
1103
            residual_eob_run = unpack_vlcs(s, gb, y_tables[i], i,
1038
1104
                0, residual_eob_run);
 
1105
            if (residual_eob_run < 0)
 
1106
                return residual_eob_run;
1039
1107
 
1040
1108
            residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1041
1109
                1, residual_eob_run);
 
1110
            if (residual_eob_run < 0)
 
1111
                return residual_eob_run;
1042
1112
            residual_eob_run = unpack_vlcs(s, gb, c_tables[i], i,
1043
1113
                2, residual_eob_run);
 
1114
            if (residual_eob_run < 0)
 
1115
                return residual_eob_run;
1044
1116
    }
1045
1117
 
1046
1118
    return 0;
1291
1363
        case 1: // zero run
1292
1364
            s->dct_tokens[plane][i]++;
1293
1365
            i += (token >> 2) & 0x7f;
 
1366
            if (i > 63) {
 
1367
                av_log(s->avctx, AV_LOG_ERROR, "Coefficient index overflow\n");
 
1368
                return i;
 
1369
            }
1294
1370
            block[perm[i]] = (token >> 9) * dequantizer[perm[i]];
1295
1371
            i++;
1296
1372
            break;
1302
1378
            return i;
1303
1379
        }
1304
1380
    } while (i < 64);
 
1381
    // return value is expected to be a valid level
 
1382
    i--;
1305
1383
end:
1306
1384
    // the actual DC+prediction is in the fragment structure
1307
1385
    block[0] = frag->dc * s->qmat[0][inter][plane][0];
1313
1391
 */
1314
1392
static void vp3_draw_horiz_band(Vp3DecodeContext *s, int y)
1315
1393
{
1316
 
    int h, cy;
1317
 
    int offset[4];
 
1394
    int h, cy, i;
 
1395
    int offset[AV_NUM_DATA_POINTERS];
1318
1396
 
1319
 
    if (HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
 
1397
    if (HAVE_THREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
1320
1398
        int y_flipped = s->flipped_image ? s->avctx->height-y : y;
1321
1399
 
1322
1400
        // At the end of the frame, report INT_MAX instead of the height of the frame.
1340
1418
    offset[0] = s->current_frame.linesize[0]*y;
1341
1419
    offset[1] = s->current_frame.linesize[1]*cy;
1342
1420
    offset[2] = s->current_frame.linesize[2]*cy;
1343
 
    offset[3] = 0;
 
1421
    for (i = 3; i < AV_NUM_DATA_POINTERS; i++)
 
1422
        offset[i] = 0;
1344
1423
 
1345
1424
    emms_c();
1346
1425
    s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h);
1400
1479
        int fragment_width    = s->fragment_width[!!plane];
1401
1480
        int fragment_height   = s->fragment_height[!!plane];
1402
1481
        int fragment_start    = s->fragment_start[plane];
1403
 
        int do_await          = !plane && HAVE_PTHREADS && (s->avctx->active_thread_type&FF_THREAD_FRAME);
 
1482
        int do_await          = !plane && HAVE_THREADS && (s->avctx->active_thread_type&FF_THREAD_FRAME);
1404
1483
 
1405
1484
        if (!s->flipped_image) stride = -stride;
1406
1485
        if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY))
1493
1572
                    /* invert DCT and place (or add) in final output */
1494
1573
 
1495
1574
                    if (s->all_fragments[i].coding_method == MODE_INTRA) {
1496
 
                        vp3_dequant(s, s->all_fragments + i, plane, 0, block);
 
1575
                        int index;
 
1576
                        index = vp3_dequant(s, s->all_fragments + i, plane, 0, block);
 
1577
                        if (index > 63)
 
1578
                            continue;
1497
1579
                        if(s->avctx->idct_algo!=FF_IDCT_VP3)
1498
1580
                            block[0] += 128<<3;
1499
1581
                        s->dsp.idct_put(
1501
1583
                            stride,
1502
1584
                            block);
1503
1585
                    } else {
1504
 
                        if (vp3_dequant(s, s->all_fragments + i, plane, 1, block)) {
 
1586
                        int index = vp3_dequant(s, s->all_fragments + i, plane, 1, block);
 
1587
                        if (index > 63)
 
1588
                            continue;
 
1589
                        if (index > 0) {
1505
1590
                        s->dsp.idct_add(
1506
1591
                            output_plane + first_pixel,
1507
1592
                            stride,
1571
1656
    return 0;
1572
1657
}
1573
1658
 
1574
 
/*
1575
 
 * This is the ffmpeg/libavcodec API init function.
1576
 
 */
1577
1659
static av_cold int vp3_decode_init(AVCodecContext *avctx)
1578
1660
{
1579
1661
    Vp3DecodeContext *s = avctx->priv_data;
1777
1859
    Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
1778
1860
    int qps_changed = 0, i, err;
1779
1861
 
 
1862
#define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
 
1863
 
1780
1864
    if (!s1->current_frame.data[0]
1781
1865
        ||s->width != s1->width
1782
 
        ||s->height!= s1->height)
 
1866
        ||s->height!= s1->height) {
 
1867
        if (s != s1)
 
1868
            copy_fields(s, s1, golden_frame, current_frame);
1783
1869
        return -1;
 
1870
    }
1784
1871
 
1785
1872
    if (s != s1) {
1786
1873
        // init tables if the first frame hasn't been decoded
1796
1883
            memcpy(s->motion_val[1], s1->motion_val[1], c_fragment_count * sizeof(*s->motion_val[1]));
1797
1884
        }
1798
1885
 
1799
 
#define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
1800
 
 
1801
1886
        // copy previous frame data
1802
1887
        copy_fields(s, s1, golden_frame, dsp);
1803
1888
 
1822
1907
    return 0;
1823
1908
}
1824
1909
 
1825
 
/*
1826
 
 * This is the ffmpeg/libavcodec API frame decode function.
1827
 
 */
1828
1910
static int vp3_decode_frame(AVCodecContext *avctx,
1829
1911
                            void *data, int *data_size,
1830
1912
                            AVPacket *avpkt)
1965
2047
    *data_size=sizeof(AVFrame);
1966
2048
    *(AVFrame*)data= s->current_frame;
1967
2049
 
1968
 
    if (!HAVE_PTHREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
 
2050
    if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
1969
2051
        update_frames(avctx);
1970
2052
 
1971
2053
    return buf_size;
1973
2055
error:
1974
2056
    ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
1975
2057
 
1976
 
    if (!HAVE_PTHREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
 
2058
    if (!HAVE_THREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
1977
2059
        avctx->release_buffer(avctx, &s->current_frame);
1978
2060
 
1979
2061
    return -1;
1980
2062
}
1981
2063
 
1982
 
/*
1983
 
 * This is the ffmpeg/libavcodec API module cleanup function.
1984
 
 */
1985
 
static av_cold int vp3_decode_end(AVCodecContext *avctx)
1986
 
{
1987
 
    Vp3DecodeContext *s = avctx->priv_data;
1988
 
    int i;
1989
 
 
1990
 
    if (avctx->is_copy && !s->current_frame.data[0])
1991
 
        return 0;
1992
 
 
1993
 
    av_free(s->superblock_coding);
1994
 
    av_free(s->all_fragments);
1995
 
    av_free(s->coded_fragment_list[0]);
1996
 
    av_free(s->dct_tokens_base);
1997
 
    av_free(s->superblock_fragments);
1998
 
    av_free(s->macroblock_coding);
1999
 
    av_free(s->motion_val[0]);
2000
 
    av_free(s->motion_val[1]);
2001
 
    av_free(s->edge_emu_buffer);
2002
 
 
2003
 
    if (avctx->is_copy) return 0;
2004
 
 
2005
 
    for (i = 0; i < 16; i++) {
2006
 
        free_vlc(&s->dc_vlc[i]);
2007
 
        free_vlc(&s->ac_vlc_1[i]);
2008
 
        free_vlc(&s->ac_vlc_2[i]);
2009
 
        free_vlc(&s->ac_vlc_3[i]);
2010
 
        free_vlc(&s->ac_vlc_4[i]);
2011
 
    }
2012
 
 
2013
 
    free_vlc(&s->superblock_run_length_vlc);
2014
 
    free_vlc(&s->fragment_run_length_vlc);
2015
 
    free_vlc(&s->mode_code_vlc);
2016
 
    free_vlc(&s->motion_vector_vlc);
2017
 
 
2018
 
    /* release all frames */
2019
 
    if (s->golden_frame.data[0])
2020
 
        ff_thread_release_buffer(avctx, &s->golden_frame);
2021
 
    if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
2022
 
        ff_thread_release_buffer(avctx, &s->last_frame);
2023
 
    /* no need to release the current_frame since it will always be pointing
2024
 
     * to the same frame as either the golden or last frame */
2025
 
 
2026
 
    return 0;
2027
 
}
2028
 
 
2029
2064
static int read_huffman_tree(AVCodecContext *avctx, GetBitContext *gb)
2030
2065
{
2031
2066
    Vp3DecodeContext *s = avctx->priv_data;
2060
2095
    return 0;
2061
2096
}
2062
2097
 
 
2098
static int vp3_init_thread_copy(AVCodecContext *avctx)
 
2099
{
 
2100
    Vp3DecodeContext *s = avctx->priv_data;
 
2101
 
 
2102
    s->superblock_coding      = NULL;
 
2103
    s->all_fragments          = NULL;
 
2104
    s->coded_fragment_list[0] = NULL;
 
2105
    s->dct_tokens_base        = NULL;
 
2106
    s->superblock_fragments   = NULL;
 
2107
    s->macroblock_coding      = NULL;
 
2108
    s->motion_val[0]          = NULL;
 
2109
    s->motion_val[1]          = NULL;
 
2110
    s->edge_emu_buffer        = NULL;
 
2111
 
 
2112
    return 0;
 
2113
}
 
2114
 
2063
2115
#if CONFIG_THEORA_DECODER
2064
2116
static const enum PixelFormat theora_pix_fmts[4] = {
2065
2117
    PIX_FMT_YUV420P, PIX_FMT_NONE, PIX_FMT_YUV422P, PIX_FMT_YUV444P
2275
2327
        return -1;
2276
2328
    }
2277
2329
 
2278
 
    if (ff_split_xiph_headers(avctx->extradata, avctx->extradata_size,
 
2330
    if (avpriv_split_xiph_headers(avctx->extradata, avctx->extradata_size,
2279
2331
                              42, header_start, header_len) < 0) {
2280
2332
        av_log(avctx, AV_LOG_ERROR, "Corrupt extradata\n");
2281
2333
        return -1;
2322
2374
}
2323
2375
 
2324
2376
AVCodec ff_theora_decoder = {
2325
 
    "theora",
2326
 
    AVMEDIA_TYPE_VIDEO,
2327
 
    CODEC_ID_THEORA,
2328
 
    sizeof(Vp3DecodeContext),
2329
 
    theora_decode_init,
2330
 
    NULL,
2331
 
    vp3_decode_end,
2332
 
    vp3_decode_frame,
2333
 
    CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
2334
 
    NULL,
 
2377
    .name           = "theora",
 
2378
    .type           = AVMEDIA_TYPE_VIDEO,
 
2379
    .id             = CODEC_ID_THEORA,
 
2380
    .priv_data_size = sizeof(Vp3DecodeContext),
 
2381
    .init           = theora_decode_init,
 
2382
    .close          = vp3_decode_end,
 
2383
    .decode         = vp3_decode_frame,
 
2384
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
 
2385
    .flush = vp3_decode_flush,
2335
2386
    .long_name = NULL_IF_CONFIG_SMALL("Theora"),
 
2387
    .init_thread_copy      = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2336
2388
    .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
2337
2389
};
2338
2390
#endif
2339
2391
 
2340
2392
AVCodec ff_vp3_decoder = {
2341
 
    "vp3",
2342
 
    AVMEDIA_TYPE_VIDEO,
2343
 
    CODEC_ID_VP3,
2344
 
    sizeof(Vp3DecodeContext),
2345
 
    vp3_decode_init,
2346
 
    NULL,
2347
 
    vp3_decode_end,
2348
 
    vp3_decode_frame,
2349
 
    CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
2350
 
    NULL,
 
2393
    .name           = "vp3",
 
2394
    .type           = AVMEDIA_TYPE_VIDEO,
 
2395
    .id             = CODEC_ID_VP3,
 
2396
    .priv_data_size = sizeof(Vp3DecodeContext),
 
2397
    .init           = vp3_decode_init,
 
2398
    .close          = vp3_decode_end,
 
2399
    .decode         = vp3_decode_frame,
 
2400
    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
 
2401
    .flush = vp3_decode_flush,
2351
2402
    .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
 
2403
    .init_thread_copy      = ONLY_IF_THREADS_ENABLED(vp3_init_thread_copy),
2352
2404
    .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
2353
2405
};