Revision 4a5a1689

View differences:

libavcodec/vp3.c
40 40

  
41 41
#include "vp3data.h"
42 42
#include "xiph.h"
43
#include "thread.h"
43 44

  
44 45
#define FRAGMENT_PIXELS 8
45 46

  
......
1318 1319
    int h, cy;
1319 1320
    int offset[4];
1320 1321

  
1322
    if (HAVE_PTHREADS && s->avctx->active_thread_type&FF_THREAD_FRAME) {
1323
        int y_flipped = s->flipped_image ? s->avctx->height-y : y;
1324

  
1325
        // At the end of the frame, report INT_MAX instead of the height of the frame.
1326
        // This makes the other threads' ff_thread_await_progress() calls cheaper, because
1327
        // they don't have to clip their values.
1328
        ff_thread_report_progress(&s->current_frame, y_flipped==s->avctx->height ? INT_MAX : y_flipped-1, 0);
1329
    }
1330

  
1321 1331
    if(s->avctx->draw_horiz_band==NULL)
1322 1332
        return;
1323 1333

  
......
1339 1349
    s->avctx->draw_horiz_band(s->avctx, &s->current_frame, offset, y, 3, h);
1340 1350
}
1341 1351

  
1352
/**
1353
 * Wait for the reference frame of the current fragment.
1354
 * The progress value is in luma pixel rows.
1355
 */
1356
static void await_reference_row(Vp3DecodeContext *s, Vp3Fragment *fragment, int motion_y, int y)
1357
{
1358
    AVFrame *ref_frame;
1359
    int ref_row;
1360
    int border = motion_y&1;
1361

  
1362
    if (fragment->coding_method == MODE_USING_GOLDEN ||
1363
        fragment->coding_method == MODE_GOLDEN_MV)
1364
        ref_frame = &s->golden_frame;
1365
    else
1366
        ref_frame = &s->last_frame;
1367

  
1368
    ref_row = y + (motion_y>>1);
1369
    ref_row = FFMAX(FFABS(ref_row), ref_row + 8 + border);
1370

  
1371
    ff_thread_await_progress(ref_frame, ref_row, 0);
1372
}
1373

  
1342 1374
/*
1343 1375
 * Perform the final rendering for a particular slice of data.
1344 1376
 * The slice number ranges from 0..(c_superblock_height - 1).
......
1371 1403
        int fragment_width    = s->fragment_width[!!plane];
1372 1404
        int fragment_height   = s->fragment_height[!!plane];
1373 1405
        int fragment_start    = s->fragment_start[plane];
1406
        int do_await          = !plane && HAVE_PTHREADS && (s->avctx->active_thread_type&FF_THREAD_FRAME);
1374 1407

  
1375 1408
        if (!s->flipped_image) stride = -stride;
1376 1409
        if (CONFIG_GRAY && plane && (s->avctx->flags & CODEC_FLAG_GRAY))
......
1400 1433

  
1401 1434
                first_pixel = 8*y*stride + 8*x;
1402 1435

  
1436
                if (do_await && s->all_fragments[i].coding_method != MODE_INTRA)
1437
                    await_reference_row(s, &s->all_fragments[i], motion_val[fragment][1], (16*y) >> s->chroma_y_shift);
1438

  
1403 1439
                /* transform if this block was coded */
1404 1440
                if (s->all_fragments[i].coding_method != MODE_COPY) {
1405 1441
                    if ((s->all_fragments[i].coding_method == MODE_USING_GOLDEN) ||
......
1721 1757
    return -1;
1722 1758
}
1723 1759

  
1760
/// Release and shuffle frames after decode finishes
1761
static void update_frames(AVCodecContext *avctx)
1762
{
1763
    Vp3DecodeContext *s = avctx->priv_data;
1764

  
1765
    /* release the last frame, if it is allocated and if it is not the
1766
     * golden frame */
1767
    if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
1768
        ff_thread_release_buffer(avctx, &s->last_frame);
1769

  
1770
    /* shuffle frames (last = current) */
1771
    s->last_frame= s->current_frame;
1772

  
1773
    if (s->keyframe) {
1774
        if (s->golden_frame.data[0])
1775
            ff_thread_release_buffer(avctx, &s->golden_frame);
1776
        s->golden_frame = s->current_frame;
1777
        s->last_frame.type = FF_BUFFER_TYPE_COPY;
1778
    }
1779

  
1780
    s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
1781
}
1782

  
1783
static int vp3_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1784
{
1785
    Vp3DecodeContext *s = dst->priv_data, *s1 = src->priv_data;
1786
    int qps_changed = 0, i, err;
1787

  
1788
    if (!s1->current_frame.data[0]
1789
        ||s->width != s1->width
1790
        ||s->height!= s1->height)
1791
        return -1;
1792

  
1793
    if (s != s1) {
1794
        // init tables if the first frame hasn't been decoded
1795
        if (!s->current_frame.data[0]) {
1796
            int y_fragment_count, c_fragment_count;
1797
            s->avctx = dst;
1798
            err = allocate_tables(dst);
1799
            if (err)
1800
                return err;
1801
            y_fragment_count = s->fragment_width[0] * s->fragment_height[0];
1802
            c_fragment_count = s->fragment_width[1] * s->fragment_height[1];
1803
            memcpy(s->motion_val[0], s1->motion_val[0], y_fragment_count * sizeof(*s->motion_val[0]));
1804
            memcpy(s->motion_val[1], s1->motion_val[1], c_fragment_count * sizeof(*s->motion_val[1]));
1805
        }
1806

  
1807
#define copy_fields(to, from, start_field, end_field) memcpy(&to->start_field, &from->start_field, (char*)&to->end_field - (char*)&to->start_field)
1808

  
1809
        // copy previous frame data
1810
        copy_fields(s, s1, golden_frame, dsp);
1811

  
1812
        // copy qscale data if necessary
1813
        for (i = 0; i < 3; i++) {
1814
            if (s->qps[i] != s1->qps[1]) {
1815
                qps_changed = 1;
1816
                memcpy(&s->qmat[i], &s1->qmat[i], sizeof(s->qmat[i]));
1817
            }
1818
        }
1819

  
1820
        if (s->qps[0] != s1->qps[0]) {
1821
            memcpy(&s->qscale_table, &s1->qscale_table, sizeof(s->qscale_table));
1822
            memcpy(&s->bounding_values_array, &s1->bounding_values_array, sizeof(s->bounding_values_array));
1823
        }
1824

  
1825
        if (qps_changed)
1826
            copy_fields(s, s1, qps, superblock_count);
1827
#undef copy_fields
1828
    }
1829

  
1830
    update_frames(dst);
1831

  
1832
    return 0;
1833
}
1834

  
1724 1835
/*
1725 1836
 * This is the ffmpeg/libavcodec API frame decode function.
1726 1837
 */
......
1776 1887

  
1777 1888
    s->current_frame.reference = 3;
1778 1889
    s->current_frame.pict_type = s->keyframe ? FF_I_TYPE : FF_P_TYPE;
1779
    if (avctx->get_buffer(avctx, &s->current_frame) < 0) {
1890
    if (ff_thread_get_buffer(avctx, &s->current_frame) < 0) {
1780 1891
        av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1781 1892
        goto error;
1782 1893
    }
......
1805 1916

  
1806 1917
            s->golden_frame.reference = 3;
1807 1918
            s->golden_frame.pict_type = FF_I_TYPE;
1808
            if (avctx->get_buffer(avctx, &s->golden_frame) < 0) {
1919
            if (ff_thread_get_buffer(avctx, &s->golden_frame) < 0) {
1809 1920
                av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1810 1921
                goto error;
1811 1922
            }
......
1818 1929
    s->current_frame.qstride= 0;
1819 1930

  
1820 1931
    memset(s->all_fragments, 0, s->fragment_count * sizeof(Vp3Fragment));
1932
    ff_thread_finish_setup(avctx);
1821 1933

  
1822 1934
    if (unpack_superblocks(s, &gb)){
1823 1935
        av_log(s->avctx, AV_LOG_ERROR, "error in unpack_superblocks\n");
......
1862 1974
    *data_size=sizeof(AVFrame);
1863 1975
    *(AVFrame*)data= s->current_frame;
1864 1976

  
1865
    /* release the last frame, if it is allocated and if it is not the
1866
     * golden frame */
1867
    if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
1868
        avctx->release_buffer(avctx, &s->last_frame);
1869

  
1870
    /* shuffle frames (last = current) */
1871
    s->last_frame= s->current_frame;
1872

  
1873
    if (s->keyframe) {
1874
        if (s->golden_frame.data[0])
1875
            avctx->release_buffer(avctx, &s->golden_frame);
1876
        s->golden_frame = s->current_frame;
1877
        s->last_frame.type = FF_BUFFER_TYPE_COPY;
1878
    }
1879

  
1880
    s->current_frame.data[0]= NULL; /* ensure that we catch any access to this released frame */
1977
    if (!HAVE_PTHREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
1978
        update_frames(avctx);
1881 1979

  
1882 1980
    return buf_size;
1883 1981

  
1884 1982
error:
1885
    if (s->current_frame.data[0])
1983
    ff_thread_report_progress(&s->current_frame, INT_MAX, 0);
1984

  
1985
    if (!HAVE_PTHREADS || !(s->avctx->active_thread_type&FF_THREAD_FRAME))
1886 1986
        avctx->release_buffer(avctx, &s->current_frame);
1987

  
1887 1988
    return -1;
1888 1989
}
1889 1990

  
......
1895 1996
    Vp3DecodeContext *s = avctx->priv_data;
1896 1997
    int i;
1897 1998

  
1999
    if (avctx->is_copy && !s->current_frame.data[0])
2000
        return 0;
2001

  
1898 2002
    av_free(s->superblock_coding);
1899 2003
    av_free(s->all_fragments);
1900 2004
    av_free(s->coded_fragment_list[0]);
......
1904 2008
    av_free(s->motion_val[0]);
1905 2009
    av_free(s->motion_val[1]);
1906 2010

  
2011
    if (avctx->is_copy) return 0;
2012

  
1907 2013
    for (i = 0; i < 16; i++) {
1908 2014
        free_vlc(&s->dc_vlc[i]);
1909 2015
        free_vlc(&s->ac_vlc_1[i]);
......
1919 2025

  
1920 2026
    /* release all frames */
1921 2027
    if (s->golden_frame.data[0])
1922
        avctx->release_buffer(avctx, &s->golden_frame);
2028
        ff_thread_release_buffer(avctx, &s->golden_frame);
1923 2029
    if (s->last_frame.data[0] && s->last_frame.type != FF_BUFFER_TYPE_COPY)
1924
        avctx->release_buffer(avctx, &s->last_frame);
2030
        ff_thread_release_buffer(avctx, &s->last_frame);
1925 2031
    /* no need to release the current_frame since it will always be pointing
1926 2032
     * to the same frame as either the golden or last frame */
1927 2033

  
......
2232 2338
    NULL,
2233 2339
    vp3_decode_end,
2234 2340
    vp3_decode_frame,
2235
    CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
2341
    CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
2236 2342
    NULL,
2237 2343
    .long_name = NULL_IF_CONFIG_SMALL("Theora"),
2344
    .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
2238 2345
};
2239 2346
#endif
2240 2347

  
......
2247 2354
    NULL,
2248 2355
    vp3_decode_end,
2249 2356
    vp3_decode_frame,
2250
    CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND,
2357
    CODEC_CAP_DR1 | CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_FRAME_THREADS,
2251 2358
    NULL,
2252 2359
    .long_name = NULL_IF_CONFIG_SMALL("On2 VP3"),
2360
    .update_thread_context = ONLY_IF_THREADS_ENABLED(vp3_update_thread_context)
2253 2361
};

Also available in: Unified diff