~ubuntu-branches/ubuntu/trusty/gst-libav1.0/trusty-proposed

« back to all changes in this revision

Viewing changes to gst-libs/ext/libav/avplay.c

  • Committer: Package Import Robot
  • Author(s): Sebastian Dröge
  • Date: 2013-09-24 17:07:00 UTC
  • mfrom: (1.1.17) (7.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20130924170700-4dg62s3pwl0pdakz
Tags: 1.2.0-1
* New upstream stable release:
  + debian/control:
    - Build depend on GStreamer and gst-plugins-base >= 1.2.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#include "libavutil/dict.h"
32
32
#include "libavutil/parseutils.h"
33
33
#include "libavutil/samplefmt.h"
 
34
#include "libavutil/time.h"
34
35
#include "libavformat/avformat.h"
35
36
#include "libavdevice/avdevice.h"
36
37
#include "libswscale/swscale.h"
37
 
#include "libavcodec/audioconvert.h"
 
38
#include "libavresample/avresample.h"
38
39
#include "libavutil/opt.h"
39
40
#include "libavcodec/avfft.h"
40
41
 
41
42
#if CONFIG_AVFILTER
42
43
# include "libavfilter/avfilter.h"
43
44
# include "libavfilter/avfiltergraph.h"
 
45
# include "libavfilter/buffersink.h"
 
46
# include "libavfilter/buffersrc.h"
44
47
#endif
45
48
 
46
49
#include "cmdutils.h"
52
55
#undef main /* We don't want SDL to override our main() */
53
56
#endif
54
57
 
55
 
#include <unistd.h>
56
58
#include <assert.h>
57
59
 
58
60
const char program_name[] = "avplay";
82
84
/* NOTE: the size must be big enough to compensate the hardware audio buffersize size */
83
85
#define SAMPLE_ARRAY_SIZE (2 * 65536)
84
86
 
85
 
static int sws_flags = SWS_BICUBIC;
 
87
static int64_t sws_flags = SWS_BICUBIC;
86
88
 
87
89
typedef struct PacketQueue {
88
90
    AVPacketList *first_pkt, *last_pkt;
97
99
#define SUBPICTURE_QUEUE_SIZE 4
98
100
 
99
101
typedef struct VideoPicture {
100
 
    double pts;                                  ///< presentation time stamp for this picture
101
 
    double target_clock;                         ///< av_gettime() time at which this should be displayed ideally
102
 
    int64_t pos;                                 ///< byte position in file
 
102
    double pts;             // presentation timestamp for this picture
 
103
    double target_clock;    // av_gettime() time at which this should be displayed ideally
 
104
    int64_t pos;            // byte position in file
103
105
    SDL_Overlay *bmp;
104
106
    int width, height; /* source height & width */
105
107
    int allocated;
106
108
    int reallocate;
107
 
    enum PixelFormat pix_fmt;
 
109
    enum AVPixelFormat pix_fmt;
108
110
 
109
111
#if CONFIG_AVFILTER
110
112
    AVFilterBufferRef *picref;
137
139
    int64_t seek_rel;
138
140
    int read_pause_return;
139
141
    AVFormatContext *ic;
140
 
    int dtg_active_format;
141
142
 
142
143
    int audio_stream;
143
144
 
160
161
    int audio_buf_index; /* in bytes */
161
162
    AVPacket audio_pkt_temp;
162
163
    AVPacket audio_pkt;
163
 
    enum AVSampleFormat audio_src_fmt;
164
 
    AVAudioConvert *reformat_ctx;
 
164
    enum AVSampleFormat sdl_sample_fmt;
 
165
    uint64_t sdl_channel_layout;
 
166
    int sdl_channels;
 
167
    int sdl_sample_rate;
 
168
    enum AVSampleFormat resample_sample_fmt;
 
169
    uint64_t resample_channel_layout;
 
170
    int resample_sample_rate;
 
171
    AVAudioResampleContext *avr;
165
172
    AVFrame *frame;
166
173
 
167
174
    int show_audio; /* if true, display audio samples */
186
193
    double frame_timer;
187
194
    double frame_last_pts;
188
195
    double frame_last_delay;
189
 
    double video_clock;                          ///< pts of last decoded frame / predicted pts of next decoded frame
 
196
    double video_clock;             // pts of last decoded frame / predicted pts of next decoded frame
190
197
    int video_stream;
191
198
    AVStream *video_st;
192
199
    PacketQueue videoq;
193
 
    double video_current_pts;                    ///< current displayed pts (different from video_clock if frame fifos are used)
194
 
    double video_current_pts_drift;              ///< video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
195
 
    int64_t video_current_pos;                   ///< current displayed file pos
 
200
    double video_current_pts;       // current displayed pts (different from video_clock if frame fifos are used)
 
201
    double video_current_pts_drift; // video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
 
202
    int64_t video_current_pos;      // current displayed file pos
196
203
    VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
197
204
    int pictq_size, pictq_rindex, pictq_windex;
198
205
    SDL_mutex *pictq_mutex;
208
215
    PtsCorrectionContext pts_ctx;
209
216
 
210
217
#if CONFIG_AVFILTER
211
 
    AVFilterContext *out_video_filter;          ///< the last filter in the video chain
 
218
    AVFilterContext *in_video_filter;   // the first filter in the video chain
 
219
    AVFilterContext *out_video_filter;  // the last filter in the video chain
 
220
    int use_dr1;
 
221
    FrameBuffer *buffer_pool;
212
222
#endif
213
223
 
214
224
    float skip_frames;
216
226
    int refresh;
217
227
} VideoState;
218
228
 
219
 
static void show_help(void);
220
 
 
221
229
/* options specified by the user */
222
230
static AVInputFormat *file_iformat;
223
231
static const char *input_filename;
245
253
static int workaround_bugs = 1;
246
254
static int fast = 0;
247
255
static int genpts = 0;
248
 
static int lowres = 0;
249
256
static int idct = FF_IDCT_AUTO;
250
257
static enum AVDiscard skip_frame       = AVDISCARD_DEFAULT;
251
258
static enum AVDiscard skip_idct        = AVDISCARD_DEFAULT;
252
259
static enum AVDiscard skip_loop_filter = AVDISCARD_DEFAULT;
253
 
static int error_recognition = FF_ER_CAREFUL;
254
260
static int error_concealment = 3;
255
261
static int decoder_reorder_pts = -1;
256
262
static int autoexit;
258
264
static int exit_on_mousedown;
259
265
static int loop = 1;
260
266
static int framedrop = 1;
 
267
static int infinite_buffer = 0;
261
268
 
262
269
static int rdftspeed = 20;
263
270
#if CONFIG_AVFILTER
277
284
 
278
285
static SDL_Surface *screen;
279
286
 
280
 
void exit_program(int ret)
281
 
{
282
 
    exit(ret);
283
 
}
284
 
 
285
287
static int packet_queue_put(PacketQueue *q, AVPacket *pkt);
286
288
 
287
289
/* packet queue handling */
746
748
    nb_freq = 1 << (rdft_bits - 1);
747
749
 
748
750
    /* compute display index : center on currently output samples */
749
 
    channels = s->audio_st->codec->channels;
 
751
    channels = s->sdl_channels;
750
752
    nb_display_channels = channels;
751
753
    if (!s->paused) {
752
754
        int data_used = s->show_audio == 1 ? s->width : (2 * nb_freq);
758
760
           the last buffer computation */
759
761
        if (audio_callback_time) {
760
762
            time_diff = av_gettime() - audio_callback_time;
761
 
            delay -= (time_diff * s->audio_st->codec->sample_rate) / 1000000;
 
763
            delay -= (time_diff * s->sdl_sample_rate) / 1000000;
762
764
        }
763
765
 
764
766
        delay += 2 * data_used;
946
948
            is->refresh = 1;
947
949
            SDL_PushEvent(&event);
948
950
        }
949
 
        usleep(is->audio_st && is->show_audio ? rdftspeed * 1000 : 5000); // FIXME ideally we should wait the correct time but SDLs event passing is so slow it would be silly
 
951
        av_usleep(is->audio_st && is->show_audio ? rdftspeed * 1000 : 5000); // FIXME ideally we should wait the correct time but SDLs event passing is so slow it would be silly
950
952
    }
951
953
    return 0;
952
954
}
960
962
    hw_buf_size = audio_write_get_buf_size(is);
961
963
    bytes_per_sec = 0;
962
964
    if (is->audio_st) {
963
 
        bytes_per_sec = is->audio_st->codec->sample_rate *
964
 
                        2 * is->audio_st->codec->channels;
 
965
        bytes_per_sec = is->sdl_sample_rate * is->sdl_channels *
 
966
                        av_get_bytes_per_sample(is->sdl_sample_fmt);
965
967
    }
966
968
    if (bytes_per_sec)
967
969
        pts -= (double)hw_buf_size / bytes_per_sec;
1228
1230
    for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
1229
1231
        vp = &is->pictq[i];
1230
1232
#if CONFIG_AVFILTER
1231
 
        if (vp->picref) {
1232
 
            avfilter_unref_buffer(vp->picref);
1233
 
            vp->picref = NULL;
1234
 
        }
 
1233
        avfilter_unref_bufferp(&vp->picref);
1235
1234
#endif
1236
1235
        if (vp->bmp) {
1237
1236
            SDL_FreeYUVOverlay(vp->bmp);
1280
1279
        SDL_FreeYUVOverlay(vp->bmp);
1281
1280
 
1282
1281
#if CONFIG_AVFILTER
1283
 
    if (vp->picref)
1284
 
        avfilter_unref_buffer(vp->picref);
1285
 
    vp->picref = NULL;
 
1282
    avfilter_unref_bufferp(&vp->picref);
1286
1283
 
1287
1284
    vp->width   = is->out_video_filter->inputs[0]->w;
1288
1285
    vp->height  = is->out_video_filter->inputs[0]->h;
1300
1297
        /* SDL allocates a buffer smaller than requested if the video
1301
1298
         * overlay hardware is unable to support the requested size. */
1302
1299
        fprintf(stderr, "Error: the video system does not support an image\n"
1303
 
                        "size of %dx%d pixels. Try using -lowres or -vf \"scale=w:h\"\n"
 
1300
                        "size of %dx%d pixels. Try using -vf \"scale=w:h\"\n"
1304
1301
                        "to reduce the image size.\n", vp->width, vp->height );
1305
1302
        do_exit();
1306
1303
    }
1311
1308
    SDL_UnlockMutex(is->pictq_mutex);
1312
1309
}
1313
1310
 
1314
 
/**
1315
 
 *
1316
 
 * @param pts the dts of the pkt / pts of the frame and guessed if not known
1317
 
 */
 
1311
/* The 'pts' parameter is the dts of the packet / pts of the frame and
 
1312
 * guessed if not known. */
1318
1313
static int queue_picture(VideoState *is, AVFrame *src_frame, double pts, int64_t pos)
1319
1314
{
1320
1315
    VideoPicture *vp;
1321
1316
#if CONFIG_AVFILTER
1322
1317
    AVPicture pict_src;
1323
1318
#else
1324
 
    int dst_pix_fmt = PIX_FMT_YUV420P;
 
1319
    int dst_pix_fmt = AV_PIX_FMT_YUV420P;
1325
1320
#endif
1326
1321
    /* wait until we have space to put a new picture */
1327
1322
    SDL_LockMutex(is->pictq_mutex);
1373
1368
 
1374
1369
    /* if the frame is not skipped, then display it */
1375
1370
    if (vp->bmp) {
1376
 
        AVPicture pict;
 
1371
        AVPicture pict = { { 0 } };
1377
1372
#if CONFIG_AVFILTER
1378
 
        if (vp->picref)
1379
 
            avfilter_unref_buffer(vp->picref);
 
1373
        avfilter_unref_bufferp(&vp->picref);
1380
1374
        vp->picref = src_frame->opaque;
1381
1375
#endif
1382
1376
 
1383
1377
        /* get a pointer on the bitmap */
1384
1378
        SDL_LockYUVOverlay (vp->bmp);
1385
1379
 
1386
 
        memset(&pict, 0, sizeof(AVPicture));
1387
1380
        pict.data[0] = vp->bmp->pixels[0];
1388
1381
        pict.data[1] = vp->bmp->pixels[2];
1389
1382
        pict.data[2] = vp->bmp->pixels[1];
1405
1398
        av_picture_copy(&pict, &pict_src,
1406
1399
                        vp->pix_fmt, vp->width, vp->height);
1407
1400
#else
1408
 
        sws_flags = av_get_int(sws_opts, "sws_flags", NULL);
 
1401
        av_opt_get_int(sws_opts, "sws_flags", 0, &sws_flags);
1409
1402
        is->img_convert_ctx = sws_getCachedContext(is->img_convert_ctx,
1410
1403
            vp->width, vp->height, vp->pix_fmt, vp->width, vp->height,
1411
1404
            dst_pix_fmt, sws_flags, NULL, NULL, NULL);
1434
1427
    return 0;
1435
1428
}
1436
1429
 
1437
 
/**
1438
 
 * compute the exact PTS for the picture if it is omitted in the stream
1439
 
 * @param pts1 the dts of the pkt / pts of the frame
1440
 
 */
 
1430
/* Compute the exact PTS for the picture if it is omitted in the stream.
 
1431
 * The 'pts1' parameter is the dts of the packet / pts of the frame. */
1441
1432
static int output_picture2(VideoState *is, AVFrame *src_frame, double pts1, int64_t pos)
1442
1433
{
1443
1434
    double frame_delay, pts;
1516
1507
}
1517
1508
 
1518
1509
#if CONFIG_AVFILTER
1519
 
typedef struct {
1520
 
    VideoState *is;
1521
 
    AVFrame *frame;
1522
 
    int use_dr1;
1523
 
} FilterPriv;
1524
 
 
1525
 
static int input_get_buffer(AVCodecContext *codec, AVFrame *pic)
1526
 
{
1527
 
    AVFilterContext *ctx = codec->opaque;
1528
 
    AVFilterBufferRef  *ref;
1529
 
    int perms = AV_PERM_WRITE;
1530
 
    int i, w, h, stride[4];
1531
 
    unsigned edge;
1532
 
    int pixel_size;
1533
 
 
1534
 
    if (codec->codec->capabilities & CODEC_CAP_NEG_LINESIZES)
1535
 
        perms |= AV_PERM_NEG_LINESIZES;
1536
 
 
1537
 
    if (pic->buffer_hints & FF_BUFFER_HINTS_VALID) {
1538
 
        if (pic->buffer_hints & FF_BUFFER_HINTS_READABLE) perms |= AV_PERM_READ;
1539
 
        if (pic->buffer_hints & FF_BUFFER_HINTS_PRESERVE) perms |= AV_PERM_PRESERVE;
1540
 
        if (pic->buffer_hints & FF_BUFFER_HINTS_REUSABLE) perms |= AV_PERM_REUSE2;
1541
 
    }
1542
 
    if (pic->reference) perms |= AV_PERM_READ | AV_PERM_PRESERVE;
1543
 
 
1544
 
    w = codec->width;
1545
 
    h = codec->height;
1546
 
    avcodec_align_dimensions2(codec, &w, &h, stride);
1547
 
    edge = codec->flags & CODEC_FLAG_EMU_EDGE ? 0 : avcodec_get_edge_width();
1548
 
    w += edge << 1;
1549
 
    h += edge << 1;
1550
 
 
1551
 
    if (!(ref = avfilter_get_video_buffer(ctx->outputs[0], perms, w, h)))
1552
 
        return -1;
1553
 
 
1554
 
    pixel_size = av_pix_fmt_descriptors[ref->format].comp[0].step_minus1 + 1;
1555
 
    ref->video->w = codec->width;
1556
 
    ref->video->h = codec->height;
1557
 
    for (i = 0; i < 4; i ++) {
1558
 
        unsigned hshift = (i == 1 || i == 2) ? av_pix_fmt_descriptors[ref->format].log2_chroma_w : 0;
1559
 
        unsigned vshift = (i == 1 || i == 2) ? av_pix_fmt_descriptors[ref->format].log2_chroma_h : 0;
1560
 
 
1561
 
        if (ref->data[i]) {
1562
 
            ref->data[i]    += ((edge * pixel_size) >> hshift) + ((edge * ref->linesize[i]) >> vshift);
1563
 
        }
1564
 
        pic->data[i]     = ref->data[i];
1565
 
        pic->linesize[i] = ref->linesize[i];
1566
 
    }
1567
 
    pic->opaque = ref;
1568
 
    pic->type   = FF_BUFFER_TYPE_USER;
1569
 
    pic->reordered_opaque = codec->reordered_opaque;
1570
 
    if (codec->pkt) pic->pkt_pts = codec->pkt->pts;
1571
 
    else            pic->pkt_pts = AV_NOPTS_VALUE;
1572
 
    return 0;
1573
 
}
1574
 
 
1575
 
static void input_release_buffer(AVCodecContext *codec, AVFrame *pic)
1576
 
{
1577
 
    memset(pic->data, 0, sizeof(pic->data));
1578
 
    avfilter_unref_buffer(pic->opaque);
1579
 
}
1580
 
 
1581
 
static int input_reget_buffer(AVCodecContext *codec, AVFrame *pic)
1582
 
{
1583
 
    AVFilterBufferRef *ref = pic->opaque;
1584
 
 
1585
 
    if (pic->data[0] == NULL) {
1586
 
        pic->buffer_hints |= FF_BUFFER_HINTS_READABLE;
1587
 
        return codec->get_buffer(codec, pic);
1588
 
    }
1589
 
 
1590
 
    if ((codec->width != ref->video->w) || (codec->height != ref->video->h) ||
1591
 
        (codec->pix_fmt != ref->format)) {
1592
 
        av_log(codec, AV_LOG_ERROR, "Picture properties changed.\n");
1593
 
        return -1;
1594
 
    }
1595
 
 
1596
 
    pic->reordered_opaque = codec->reordered_opaque;
1597
 
    if (codec->pkt) pic->pkt_pts = codec->pkt->pts;
1598
 
    else            pic->pkt_pts = AV_NOPTS_VALUE;
1599
 
    return 0;
1600
 
}
1601
 
 
1602
 
static int input_init(AVFilterContext *ctx, const char *args, void *opaque)
1603
 
{
1604
 
    FilterPriv *priv = ctx->priv;
1605
 
    AVCodecContext *codec;
1606
 
    if (!opaque) return -1;
1607
 
 
1608
 
    priv->is = opaque;
1609
 
    codec    = priv->is->video_st->codec;
1610
 
    codec->opaque = ctx;
1611
 
    if (codec->codec->capabilities & CODEC_CAP_DR1) {
1612
 
        priv->use_dr1 = 1;
1613
 
        codec->get_buffer     = input_get_buffer;
1614
 
        codec->release_buffer = input_release_buffer;
1615
 
        codec->reget_buffer   = input_reget_buffer;
1616
 
        codec->thread_safe_callbacks = 1;
1617
 
    }
1618
 
 
1619
 
    priv->frame = avcodec_alloc_frame();
1620
 
 
1621
 
    return 0;
1622
 
}
1623
 
 
1624
 
static void input_uninit(AVFilterContext *ctx)
1625
 
{
1626
 
    FilterPriv *priv = ctx->priv;
1627
 
    av_free(priv->frame);
1628
 
}
1629
 
 
1630
 
static int input_request_frame(AVFilterLink *link)
1631
 
{
1632
 
    FilterPriv *priv = link->src->priv;
1633
 
    AVFilterBufferRef *picref;
1634
 
    int64_t pts = 0;
1635
 
    AVPacket pkt;
1636
 
    int ret;
1637
 
 
1638
 
    while (!(ret = get_video_frame(priv->is, priv->frame, &pts, &pkt)))
1639
 
        av_free_packet(&pkt);
1640
 
    if (ret < 0)
1641
 
        return -1;
1642
 
 
1643
 
    if (priv->use_dr1) {
1644
 
        picref = avfilter_ref_buffer(priv->frame->opaque, ~0);
1645
 
    } else {
1646
 
        picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, link->w, link->h);
1647
 
        av_image_copy(picref->data, picref->linesize,
1648
 
                      priv->frame->data, priv->frame->linesize,
1649
 
                      picref->format, link->w, link->h);
1650
 
    }
1651
 
    av_free_packet(&pkt);
1652
 
 
1653
 
    avfilter_copy_frame_props(picref, priv->frame);
1654
 
    picref->pts = pts;
1655
 
 
1656
 
    avfilter_start_frame(link, picref);
1657
 
    avfilter_draw_slice(link, 0, link->h, 1);
1658
 
    avfilter_end_frame(link);
1659
 
 
1660
 
    return 0;
1661
 
}
1662
 
 
1663
 
static int input_query_formats(AVFilterContext *ctx)
1664
 
{
1665
 
    FilterPriv *priv = ctx->priv;
1666
 
    enum PixelFormat pix_fmts[] = {
1667
 
        priv->is->video_st->codec->pix_fmt, PIX_FMT_NONE
1668
 
    };
1669
 
 
1670
 
    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
1671
 
    return 0;
1672
 
}
1673
 
 
1674
 
static int input_config_props(AVFilterLink *link)
1675
 
{
1676
 
    FilterPriv *priv  = link->src->priv;
1677
 
    AVCodecContext *c = priv->is->video_st->codec;
1678
 
 
1679
 
    link->w = c->width;
1680
 
    link->h = c->height;
1681
 
    link->time_base = priv->is->video_st->time_base;
1682
 
 
1683
 
    return 0;
1684
 
}
1685
 
 
1686
 
static AVFilter input_filter =
1687
 
{
1688
 
    .name      = "avplay_input",
1689
 
 
1690
 
    .priv_size = sizeof(FilterPriv),
1691
 
 
1692
 
    .init      = input_init,
1693
 
    .uninit    = input_uninit,
1694
 
 
1695
 
    .query_formats = input_query_formats,
1696
 
 
1697
 
    .inputs    = (AVFilterPad[]) {{ .name = NULL }},
1698
 
    .outputs   = (AVFilterPad[]) {{ .name = "default",
1699
 
                                    .type = AVMEDIA_TYPE_VIDEO,
1700
 
                                    .request_frame = input_request_frame,
1701
 
                                    .config_props  = input_config_props, },
1702
 
                                  { .name = NULL }},
1703
 
};
1704
 
 
1705
1510
static int configure_video_filters(AVFilterGraph *graph, VideoState *is, const char *vfilters)
1706
1511
{
1707
1512
    char sws_flags_str[128];
 
1513
    char buffersrc_args[256];
1708
1514
    int ret;
1709
 
    AVSinkContext avsink_ctx = { .pix_fmt = PIX_FMT_YUV420P };
1710
 
    AVFilterContext *filt_src = NULL, *filt_out = NULL;
1711
 
    snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%d", sws_flags);
 
1515
    AVFilterContext *filt_src = NULL, *filt_out = NULL, *filt_format;
 
1516
    AVCodecContext *codec = is->video_st->codec;
 
1517
 
 
1518
    snprintf(sws_flags_str, sizeof(sws_flags_str), "flags=%"PRId64, sws_flags);
1712
1519
    graph->scale_sws_opts = av_strdup(sws_flags_str);
1713
1520
 
1714
 
    if ((ret = avfilter_graph_create_filter(&filt_src, &input_filter, "src",
1715
 
                                            NULL, is, graph)) < 0)
1716
 
        return ret;
1717
 
    if ((ret = avfilter_graph_create_filter(&filt_out, &avsink, "out",
1718
 
                                            NULL, &avsink_ctx, graph)) < 0)
1719
 
        return ret;
 
1521
    snprintf(buffersrc_args, sizeof(buffersrc_args), "%d:%d:%d:%d:%d:%d:%d",
 
1522
             codec->width, codec->height, codec->pix_fmt,
 
1523
             is->video_st->time_base.num, is->video_st->time_base.den,
 
1524
             codec->sample_aspect_ratio.num, codec->sample_aspect_ratio.den);
 
1525
 
 
1526
 
 
1527
    if ((ret = avfilter_graph_create_filter(&filt_src,
 
1528
                                            avfilter_get_by_name("buffer"),
 
1529
                                            "src", buffersrc_args, NULL,
 
1530
                                            graph)) < 0)
 
1531
        return ret;
 
1532
    if ((ret = avfilter_graph_create_filter(&filt_out,
 
1533
                                            avfilter_get_by_name("buffersink"),
 
1534
                                            "out", NULL, NULL, graph)) < 0)
 
1535
        return ret;
 
1536
 
 
1537
    if ((ret = avfilter_graph_create_filter(&filt_format,
 
1538
                                            avfilter_get_by_name("format"),
 
1539
                                            "format", "yuv420p", NULL, graph)) < 0)
 
1540
        return ret;
 
1541
    if ((ret = avfilter_link(filt_format, 0, filt_out, 0)) < 0)
 
1542
        return ret;
 
1543
 
1720
1544
 
1721
1545
    if (vfilters) {
1722
 
        AVFilterInOut *outputs = av_malloc(sizeof(AVFilterInOut));
1723
 
        AVFilterInOut *inputs  = av_malloc(sizeof(AVFilterInOut));
 
1546
        AVFilterInOut *outputs = avfilter_inout_alloc();
 
1547
        AVFilterInOut *inputs  = avfilter_inout_alloc();
1724
1548
 
1725
1549
        outputs->name    = av_strdup("in");
1726
1550
        outputs->filter_ctx = filt_src;
1728
1552
        outputs->next    = NULL;
1729
1553
 
1730
1554
        inputs->name    = av_strdup("out");
1731
 
        inputs->filter_ctx = filt_out;
 
1555
        inputs->filter_ctx = filt_format;
1732
1556
        inputs->pad_idx = 0;
1733
1557
        inputs->next    = NULL;
1734
1558
 
1735
1559
        if ((ret = avfilter_graph_parse(graph, vfilters, inputs, outputs, NULL)) < 0)
1736
1560
            return ret;
1737
 
        av_freep(&vfilters);
1738
1561
    } else {
1739
 
        if ((ret = avfilter_link(filt_src, 0, filt_out, 0)) < 0)
 
1562
        if ((ret = avfilter_link(filt_src, 0, filt_format, 0)) < 0)
1740
1563
            return ret;
1741
1564
    }
1742
1565
 
1743
1566
    if ((ret = avfilter_graph_config(graph, NULL)) < 0)
1744
1567
        return ret;
1745
1568
 
 
1569
    is->in_video_filter  = filt_src;
1746
1570
    is->out_video_filter = filt_out;
1747
1571
 
 
1572
    if (codec->codec->capabilities & CODEC_CAP_DR1) {
 
1573
        is->use_dr1 = 1;
 
1574
        codec->get_buffer     = codec_get_buffer;
 
1575
        codec->release_buffer = codec_release_buffer;
 
1576
        codec->opaque         = &is->buffer_pool;
 
1577
    }
 
1578
 
1748
1579
    return ret;
1749
1580
}
1750
1581
 
1752
1583
 
1753
1584
static int video_thread(void *arg)
1754
1585
{
 
1586
    AVPacket pkt = { 0 };
1755
1587
    VideoState *is = arg;
1756
1588
    AVFrame *frame = avcodec_alloc_frame();
1757
1589
    int64_t pts_int;
1760
1592
 
1761
1593
#if CONFIG_AVFILTER
1762
1594
    AVFilterGraph *graph = avfilter_graph_alloc();
1763
 
    AVFilterContext *filt_out = NULL;
 
1595
    AVFilterContext *filt_out = NULL, *filt_in = NULL;
1764
1596
    int64_t pos;
1765
1597
    int last_w = is->video_st->codec->width;
1766
1598
    int last_h = is->video_st->codec->height;
1767
1599
 
1768
1600
    if ((ret = configure_video_filters(graph, is, vfilters)) < 0)
1769
1601
        goto the_end;
 
1602
    filt_in  = is->in_video_filter;
1770
1603
    filt_out = is->out_video_filter;
1771
1604
#endif
1772
1605
 
1773
1606
    for (;;) {
1774
 
#if !CONFIG_AVFILTER
1775
 
        AVPacket pkt;
1776
 
#else
 
1607
#if CONFIG_AVFILTER
1777
1608
        AVFilterBufferRef *picref;
1778
1609
        AVRational tb;
1779
1610
#endif
1780
1611
        while (is->paused && !is->videoq.abort_request)
1781
1612
            SDL_Delay(10);
 
1613
 
 
1614
        av_free_packet(&pkt);
 
1615
 
 
1616
        ret = get_video_frame(is, frame, &pts_int, &pkt);
 
1617
        if (ret < 0)
 
1618
            goto the_end;
 
1619
 
 
1620
        if (!ret)
 
1621
            continue;
 
1622
 
1782
1623
#if CONFIG_AVFILTER
1783
1624
        if (   last_w != is->video_st->codec->width
1784
1625
            || last_h != is->video_st->codec->height) {
1788
1629
            graph = avfilter_graph_alloc();
1789
1630
            if ((ret = configure_video_filters(graph, is, vfilters)) < 0)
1790
1631
                goto the_end;
 
1632
            filt_in  = is->in_video_filter;
1791
1633
            filt_out = is->out_video_filter;
1792
1634
            last_w = is->video_st->codec->width;
1793
1635
            last_h = is->video_st->codec->height;
1794
1636
        }
1795
 
        ret = get_filtered_video_frame(filt_out, frame, &picref, &tb);
1796
 
        if (picref) {
 
1637
 
 
1638
        frame->pts = pts_int;
 
1639
        if (is->use_dr1) {
 
1640
            FrameBuffer      *buf = frame->opaque;
 
1641
            AVFilterBufferRef *fb = avfilter_get_video_buffer_ref_from_arrays(
 
1642
                                        frame->data, frame->linesize,
 
1643
                                        AV_PERM_READ | AV_PERM_PRESERVE,
 
1644
                                        frame->width, frame->height,
 
1645
                                        frame->format);
 
1646
 
 
1647
            avfilter_copy_frame_props(fb, frame);
 
1648
            fb->buf->priv           = buf;
 
1649
            fb->buf->free           = filter_release_buffer;
 
1650
 
 
1651
            buf->refcount++;
 
1652
            av_buffersrc_buffer(filt_in, fb);
 
1653
 
 
1654
        } else
 
1655
            av_buffersrc_write_frame(filt_in, frame);
 
1656
 
 
1657
        while (ret >= 0) {
 
1658
            ret = av_buffersink_read(filt_out, &picref);
 
1659
            if (ret < 0) {
 
1660
                ret = 0;
 
1661
                break;
 
1662
            }
 
1663
 
 
1664
            avfilter_copy_buf_props(frame, picref);
 
1665
 
1797
1666
            pts_int = picref->pts;
 
1667
            tb      = filt_out->inputs[0]->time_base;
1798
1668
            pos     = picref->pos;
1799
1669
            frame->opaque = picref;
1800
 
        }
1801
1670
 
1802
 
        if (av_cmp_q(tb, is->video_st->time_base)) {
1803
 
            av_unused int64_t pts1 = pts_int;
1804
 
            pts_int = av_rescale_q(pts_int, tb, is->video_st->time_base);
1805
 
            av_dlog(NULL, "video_thread(): "
1806
 
                    "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
1807
 
                    tb.num, tb.den, pts1,
1808
 
                    is->video_st->time_base.num, is->video_st->time_base.den, pts_int);
 
1671
            if (av_cmp_q(tb, is->video_st->time_base)) {
 
1672
                av_unused int64_t pts1 = pts_int;
 
1673
                pts_int = av_rescale_q(pts_int, tb, is->video_st->time_base);
 
1674
                av_dlog(NULL, "video_thread(): "
 
1675
                        "tb:%d/%d pts:%"PRId64" -> tb:%d/%d pts:%"PRId64"\n",
 
1676
                        tb.num, tb.den, pts1,
 
1677
                        is->video_st->time_base.num, is->video_st->time_base.den, pts_int);
 
1678
            }
 
1679
            pts = pts_int * av_q2d(is->video_st->time_base);
 
1680
            ret = output_picture2(is, frame, pts, pos);
1809
1681
        }
1810
1682
#else
1811
 
        ret = get_video_frame(is, frame, &pts_int, &pkt);
1812
 
#endif
1813
 
 
1814
 
        if (ret < 0)
1815
 
            goto the_end;
1816
 
 
1817
 
        if (!ret)
1818
 
            continue;
1819
 
 
1820
1683
        pts = pts_int * av_q2d(is->video_st->time_base);
1821
 
 
1822
 
#if CONFIG_AVFILTER
1823
 
        ret = output_picture2(is, frame, pts, pos);
1824
 
#else
1825
1684
        ret = output_picture2(is, frame, pts,  pkt.pos);
1826
 
        av_free_packet(&pkt);
1827
1685
#endif
 
1686
 
1828
1687
        if (ret < 0)
1829
1688
            goto the_end;
1830
1689
 
1834
1693
    }
1835
1694
 the_end:
1836
1695
#if CONFIG_AVFILTER
 
1696
    av_freep(&vfilters);
1837
1697
    avfilter_graph_free(&graph);
1838
1698
#endif
1839
 
    av_free(frame);
 
1699
    av_free_packet(&pkt);
 
1700
    avcodec_free_frame(&frame);
1840
1701
    return 0;
1841
1702
}
1842
1703
 
1936
1797
    int n, samples_size;
1937
1798
    double ref_clock;
1938
1799
 
1939
 
    n = 2 * is->audio_st->codec->channels;
 
1800
    n = is->sdl_channels * av_get_bytes_per_sample(is->sdl_sample_fmt);
1940
1801
    samples_size = samples_size1;
1941
1802
 
1942
1803
    /* if not master, then we try to remove or add samples to correct the clock */
1958
1819
                avg_diff = is->audio_diff_cum * (1.0 - is->audio_diff_avg_coef);
1959
1820
 
1960
1821
                if (fabs(avg_diff) >= is->audio_diff_threshold) {
1961
 
                    wanted_size = samples_size + ((int)(diff * is->audio_st->codec->sample_rate) * n);
 
1822
                    wanted_size = samples_size + ((int)(diff * is->sdl_sample_rate) * n);
1962
1823
                    nb_samples = samples_size / n;
1963
1824
 
1964
1825
                    min_size = ((nb_samples * (100 - SAMPLE_CORRECTION_PERCENT_MAX)) / 100) * n;
2017
1878
    for (;;) {
2018
1879
        /* NOTE: the audio packet can contain several frames */
2019
1880
        while (pkt_temp->size > 0 || (!pkt_temp->data && new_packet)) {
 
1881
            int resample_changed, audio_resample;
 
1882
 
2020
1883
            if (!is->frame) {
2021
1884
                if (!(is->frame = avcodec_alloc_frame()))
2022
1885
                    return AVERROR(ENOMEM);
2044
1907
            }
2045
1908
            data_size = av_samples_get_buffer_size(NULL, dec->channels,
2046
1909
                                                   is->frame->nb_samples,
2047
 
                                                   dec->sample_fmt, 1);
2048
 
 
2049
 
            if (dec->sample_fmt != is->audio_src_fmt) {
2050
 
                if (is->reformat_ctx)
2051
 
                    av_audio_convert_free(is->reformat_ctx);
2052
 
                is->reformat_ctx= av_audio_convert_alloc(AV_SAMPLE_FMT_S16, 1,
2053
 
                                                         dec->sample_fmt, 1, NULL, 0);
2054
 
                if (!is->reformat_ctx) {
2055
 
                    fprintf(stderr, "Cannot convert %s sample format to %s sample format\n",
2056
 
                        av_get_sample_fmt_name(dec->sample_fmt),
2057
 
                        av_get_sample_fmt_name(AV_SAMPLE_FMT_S16));
2058
 
                        break;
2059
 
                }
2060
 
                is->audio_src_fmt= dec->sample_fmt;
 
1910
                                                   is->frame->format, 1);
 
1911
 
 
1912
            audio_resample = is->frame->format         != is->sdl_sample_fmt     ||
 
1913
                             is->frame->channel_layout != is->sdl_channel_layout ||
 
1914
                             is->frame->sample_rate    != is->sdl_sample_rate;
 
1915
 
 
1916
            resample_changed = is->frame->format         != is->resample_sample_fmt     ||
 
1917
                               is->frame->channel_layout != is->resample_channel_layout ||
 
1918
                               is->frame->sample_rate    != is->resample_sample_rate;
 
1919
 
 
1920
            if ((!is->avr && audio_resample) || resample_changed) {
 
1921
                int ret;
 
1922
                if (is->avr)
 
1923
                    avresample_close(is->avr);
 
1924
                else if (audio_resample) {
 
1925
                    is->avr = avresample_alloc_context();
 
1926
                    if (!is->avr) {
 
1927
                        fprintf(stderr, "error allocating AVAudioResampleContext\n");
 
1928
                        break;
 
1929
                    }
 
1930
                }
 
1931
                if (audio_resample) {
 
1932
                    av_opt_set_int(is->avr, "in_channel_layout",  is->frame->channel_layout, 0);
 
1933
                    av_opt_set_int(is->avr, "in_sample_fmt",      is->frame->format,         0);
 
1934
                    av_opt_set_int(is->avr, "in_sample_rate",     is->frame->sample_rate,    0);
 
1935
                    av_opt_set_int(is->avr, "out_channel_layout", is->sdl_channel_layout,    0);
 
1936
                    av_opt_set_int(is->avr, "out_sample_fmt",     is->sdl_sample_fmt,        0);
 
1937
                    av_opt_set_int(is->avr, "out_sample_rate",    is->sdl_sample_rate,       0);
 
1938
 
 
1939
                    if ((ret = avresample_open(is->avr)) < 0) {
 
1940
                        fprintf(stderr, "error initializing libavresample\n");
 
1941
                        break;
 
1942
                    }
 
1943
                }
 
1944
                is->resample_sample_fmt     = is->frame->format;
 
1945
                is->resample_channel_layout = is->frame->channel_layout;
 
1946
                is->resample_sample_rate    = is->frame->sample_rate;
2061
1947
            }
2062
1948
 
2063
 
            if (is->reformat_ctx) {
2064
 
                const void *ibuf[6] = { is->frame->data[0] };
2065
 
                void *obuf[6];
2066
 
                int istride[6] = { av_get_bytes_per_sample(dec->sample_fmt) };
2067
 
                int ostride[6] = { 2 };
2068
 
                int len= data_size/istride[0];
2069
 
                obuf[0] = av_realloc(is->audio_buf1, FFALIGN(len * ostride[0], 32));
2070
 
                if (!obuf[0]) {
 
1949
            if (audio_resample) {
 
1950
                void *tmp_out;
 
1951
                int out_samples, out_size, out_linesize;
 
1952
                int osize      = av_get_bytes_per_sample(is->sdl_sample_fmt);
 
1953
                int nb_samples = is->frame->nb_samples;
 
1954
 
 
1955
                out_size = av_samples_get_buffer_size(&out_linesize,
 
1956
                                                      is->sdl_channels,
 
1957
                                                      nb_samples,
 
1958
                                                      is->sdl_sample_fmt, 0);
 
1959
                tmp_out = av_realloc(is->audio_buf1, out_size);
 
1960
                if (!tmp_out)
2071
1961
                    return AVERROR(ENOMEM);
2072
 
                }
2073
 
                is->audio_buf1 = obuf[0];
2074
 
                if (av_audio_convert(is->reformat_ctx, obuf, ostride, ibuf, istride, len) < 0) {
2075
 
                    printf("av_audio_convert() failed\n");
 
1962
                is->audio_buf1 = tmp_out;
 
1963
 
 
1964
                out_samples = avresample_convert(is->avr,
 
1965
                                                 &is->audio_buf1,
 
1966
                                                 out_linesize, nb_samples,
 
1967
                                                 is->frame->data,
 
1968
                                                 is->frame->linesize[0],
 
1969
                                                 is->frame->nb_samples);
 
1970
                if (out_samples < 0) {
 
1971
                    fprintf(stderr, "avresample_convert() failed\n");
2076
1972
                    break;
2077
1973
                }
2078
1974
                is->audio_buf = is->audio_buf1;
2079
 
                /* FIXME: existing code assume that data_size equals framesize*channels*2
2080
 
                          remove this legacy cruft */
2081
 
                data_size = len * 2;
 
1975
                data_size = out_samples * osize * is->sdl_channels;
2082
1976
            } else {
2083
1977
                is->audio_buf = is->frame->data[0];
2084
1978
            }
2086
1980
            /* if no pts, then compute it */
2087
1981
            pts = is->audio_clock;
2088
1982
            *pts_ptr = pts;
2089
 
            n = 2 * dec->channels;
 
1983
            n = is->sdl_channels * av_get_bytes_per_sample(is->sdl_sample_fmt);
2090
1984
            is->audio_clock += (double)data_size /
2091
 
                (double)(n * dec->sample_rate);
 
1985
                (double)(n * is->sdl_sample_rate);
2092
1986
#ifdef DEBUG
2093
1987
            {
2094
1988
                static double last_clock;
2114
2008
        if ((new_packet = packet_queue_get(&is->audioq, pkt, 1)) < 0)
2115
2009
            return -1;
2116
2010
 
2117
 
        if (pkt->data == flush_pkt.data)
 
2011
        if (pkt->data == flush_pkt.data) {
2118
2012
            avcodec_flush_buffers(dec);
 
2013
            flush_complete = 0;
 
2014
        }
2119
2015
 
2120
2016
        *pkt_temp = *pkt;
2121
2017
 
2175
2071
        return -1;
2176
2072
    avctx = ic->streams[stream_index]->codec;
2177
2073
 
2178
 
    opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index]);
 
2074
    opts = filter_codec_opts(codec_opts, avctx->codec_id, ic, ic->streams[stream_index], NULL);
2179
2075
 
2180
2076
    codec = avcodec_find_decoder(avctx->codec_id);
2181
2077
    avctx->debug_mv          = debug_mv;
2182
2078
    avctx->debug             = debug;
2183
2079
    avctx->workaround_bugs   = workaround_bugs;
2184
 
    avctx->lowres            = lowres;
2185
2080
    avctx->idct_algo         = idct;
2186
2081
    avctx->skip_frame        = skip_frame;
2187
2082
    avctx->skip_idct         = skip_idct;
2188
2083
    avctx->skip_loop_filter  = skip_loop_filter;
2189
 
    avctx->error_recognition = error_recognition;
2190
2084
    avctx->error_concealment = error_concealment;
2191
2085
 
2192
 
    if (lowres) avctx->flags  |= CODEC_FLAG_EMU_EDGE;
2193
2086
    if (fast)   avctx->flags2 |= CODEC_FLAG2_FAST;
2194
2087
 
2195
2088
    if (!av_dict_get(opts, "threads", NULL, 0))
2204
2097
 
2205
2098
    /* prepare audio output */
2206
2099
    if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2207
 
        wanted_spec.freq = avctx->sample_rate;
 
2100
        is->sdl_sample_rate = avctx->sample_rate;
 
2101
 
 
2102
        if (!avctx->channel_layout)
 
2103
            avctx->channel_layout = av_get_default_channel_layout(avctx->channels);
 
2104
        if (!avctx->channel_layout) {
 
2105
            fprintf(stderr, "unable to guess channel layout\n");
 
2106
            return -1;
 
2107
        }
 
2108
        if (avctx->channels == 1)
 
2109
            is->sdl_channel_layout = AV_CH_LAYOUT_MONO;
 
2110
        else
 
2111
            is->sdl_channel_layout = AV_CH_LAYOUT_STEREO;
 
2112
        is->sdl_channels = av_get_channel_layout_nb_channels(is->sdl_channel_layout);
 
2113
 
2208
2114
        wanted_spec.format = AUDIO_S16SYS;
2209
 
        wanted_spec.channels = avctx->channels;
 
2115
        wanted_spec.freq = is->sdl_sample_rate;
 
2116
        wanted_spec.channels = is->sdl_channels;
2210
2117
        wanted_spec.silence = 0;
2211
2118
        wanted_spec.samples = SDL_AUDIO_BUFFER_SIZE;
2212
2119
        wanted_spec.callback = sdl_audio_callback;
2216
2123
            return -1;
2217
2124
        }
2218
2125
        is->audio_hw_buf_size = spec.size;
2219
 
        is->audio_src_fmt = AV_SAMPLE_FMT_S16;
 
2126
        is->sdl_sample_fmt          = AV_SAMPLE_FMT_S16;
 
2127
        is->resample_sample_fmt     = is->sdl_sample_fmt;
 
2128
        is->resample_channel_layout = avctx->channel_layout;
 
2129
        is->resample_sample_rate    = avctx->sample_rate;
2220
2130
    }
2221
2131
 
2222
2132
    ic->streams[stream_index]->discard = AVDISCARD_DEFAULT;
2275
2185
 
2276
2186
        packet_queue_end(&is->audioq);
2277
2187
        av_free_packet(&is->audio_pkt);
2278
 
        if (is->reformat_ctx)
2279
 
            av_audio_convert_free(is->reformat_ctx);
2280
 
        is->reformat_ctx = NULL;
 
2188
        if (is->avr)
 
2189
            avresample_free(&is->avr);
2281
2190
        av_freep(&is->audio_buf1);
2282
2191
        is->audio_buf = NULL;
2283
 
        av_freep(&is->frame);
 
2192
        avcodec_free_frame(&is->frame);
2284
2193
 
2285
2194
        if (is->rdft) {
2286
2195
            av_rdft_end(is->rdft);
2323
2232
 
2324
2233
    ic->streams[stream_index]->discard = AVDISCARD_ALL;
2325
2234
    avcodec_close(avctx);
 
2235
#if CONFIG_AVFILTER
 
2236
    free_buffer_pool(&is->buffer_pool);
 
2237
#endif
2326
2238
    switch (avctx->codec_type) {
2327
2239
    case AVMEDIA_TYPE_AUDIO:
2328
2240
        is->audio_st = NULL;
2519
2431
        }
2520
2432
 
2521
2433
        /* if the queue are full, no need to read more */
2522
 
        if (   is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
 
2434
        if (!infinite_buffer &&
 
2435
              (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
2523
2436
            || (   (is->audioq   .size  > MIN_AUDIOQ_SIZE || is->audio_stream < 0)
2524
2437
                && (is->videoq   .nb_packets > MIN_FRAMES || is->video_stream < 0)
2525
 
                && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0))) {
 
2438
                && (is->subtitleq.nb_packets > MIN_FRAMES || is->subtitle_stream < 0)))) {
2526
2439
            /* wait 10 ms */
2527
2440
            SDL_Delay(10);
2528
2441
            continue;
2690
2603
 
2691
2604
static void toggle_full_screen(void)
2692
2605
{
2693
 
    is_full_screen = !is_full_screen;
2694
2606
#if defined(__APPLE__) && SDL_VERSION_ATLEAST(1, 2, 14)
2695
2607
    /* OS X needs to empty the picture_queue */
2696
 
    for (int i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++) {
 
2608
    int i;
 
2609
    for (i = 0; i < VIDEO_PICTURE_QUEUE_SIZE; i++)
2697
2610
        cur_stream->pictq[i].reallocate = 1;
2698
 
    }
2699
2611
#endif
 
2612
    is_full_screen = !is_full_screen;
2700
2613
    video_open(cur_stream);
2701
2614
}
2702
2615
 
2876
2789
    }
2877
2790
}
2878
2791
 
2879
 
static int opt_frame_size(const char *opt, const char *arg)
 
2792
static int opt_frame_size(void *optctx, const char *opt, const char *arg)
2880
2793
{
2881
2794
    av_log(NULL, AV_LOG_ERROR,
2882
2795
           "Option '%s' has been removed, use private format options instead\n", opt);
2883
2796
    return AVERROR(EINVAL);
2884
2797
}
2885
2798
 
2886
 
static int opt_width(const char *opt, const char *arg)
 
2799
static int opt_width(void *optctx, const char *opt, const char *arg)
2887
2800
{
2888
2801
    screen_width = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
2889
2802
    return 0;
2890
2803
}
2891
2804
 
2892
 
static int opt_height(const char *opt, const char *arg)
 
2805
static int opt_height(void *optctx, const char *opt, const char *arg)
2893
2806
{
2894
2807
    screen_height = parse_number_or_die(opt, arg, OPT_INT64, 1, INT_MAX);
2895
2808
    return 0;
2896
2809
}
2897
2810
 
2898
 
static int opt_format(const char *opt, const char *arg)
 
2811
static int opt_format(void *optctx, const char *opt, const char *arg)
2899
2812
{
2900
2813
    file_iformat = av_find_input_format(arg);
2901
2814
    if (!file_iformat) {
2905
2818
    return 0;
2906
2819
}
2907
2820
 
2908
 
static int opt_frame_pix_fmt(const char *opt, const char *arg)
 
2821
static int opt_frame_pix_fmt(void *optctx, const char *opt, const char *arg)
2909
2822
{
2910
2823
    av_log(NULL, AV_LOG_ERROR,
2911
2824
           "Option '%s' has been removed, use private format options instead\n", opt);
2912
2825
    return AVERROR(EINVAL);
2913
2826
}
2914
2827
 
2915
 
static int opt_sync(const char *opt, const char *arg)
 
2828
static int opt_sync(void *optctx, const char *opt, const char *arg)
2916
2829
{
2917
2830
    if (!strcmp(arg, "audio"))
2918
2831
        av_sync_type = AV_SYNC_AUDIO_MASTER;
2927
2840
    return 0;
2928
2841
}
2929
2842
 
2930
 
static int opt_seek(const char *opt, const char *arg)
 
2843
static int opt_seek(void *optctx, const char *opt, const char *arg)
2931
2844
{
2932
2845
    start_time = parse_time_or_die(opt, arg, 1);
2933
2846
    return 0;
2934
2847
}
2935
2848
 
2936
 
static int opt_duration(const char *opt, const char *arg)
 
2849
static int opt_duration(void *optctx, const char *opt, const char *arg)
2937
2850
{
2938
2851
    duration = parse_time_or_die(opt, arg, 1);
2939
2852
    return 0;
2940
2853
}
2941
2854
 
2942
 
static int opt_debug(const char *opt, const char *arg)
 
2855
static int opt_debug(void *optctx, const char *opt, const char *arg)
2943
2856
{
2944
2857
    av_log_set_level(99);
2945
2858
    debug = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
2946
2859
    return 0;
2947
2860
}
2948
2861
 
2949
 
static int opt_vismv(const char *opt, const char *arg)
 
2862
static int opt_vismv(void *optctx, const char *opt, const char *arg)
2950
2863
{
2951
2864
    debug_mv = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
2952
2865
    return 0;
2954
2867
 
2955
2868
static const OptionDef options[] = {
2956
2869
#include "cmdutils_common_opts.h"
2957
 
    { "x", HAS_ARG, { (void*)opt_width }, "force displayed width", "width" },
2958
 
    { "y", HAS_ARG, { (void*)opt_height }, "force displayed height", "height" },
2959
 
    { "s", HAS_ARG | OPT_VIDEO, { (void*)opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
2960
 
    { "fs", OPT_BOOL, { (void*)&is_full_screen }, "force full screen" },
2961
 
    { "an", OPT_BOOL, { (void*)&audio_disable }, "disable audio" },
2962
 
    { "vn", OPT_BOOL, { (void*)&video_disable }, "disable video" },
2963
 
    { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
2964
 
    { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
2965
 
    { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
2966
 
    { "ss", HAS_ARG, { (void*)&opt_seek }, "seek to a given position in seconds", "pos" },
2967
 
    { "t", HAS_ARG, { (void*)&opt_duration }, "play  \"duration\" seconds of audio/video", "duration" },
2968
 
    { "bytes", OPT_INT | HAS_ARG, { (void*)&seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
2969
 
    { "nodisp", OPT_BOOL, { (void*)&display_disable }, "disable graphical display" },
2970
 
    { "f", HAS_ARG, { (void*)opt_format }, "force format", "fmt" },
2971
 
    { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { (void*)opt_frame_pix_fmt }, "set pixel format", "format" },
2972
 
    { "stats", OPT_BOOL | OPT_EXPERT, { (void*)&show_status }, "show status", "" },
2973
 
    { "debug", HAS_ARG | OPT_EXPERT, { (void*)opt_debug }, "print specific debug info", "" },
2974
 
    { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&workaround_bugs }, "workaround bugs", "" },
2975
 
    { "vismv", HAS_ARG | OPT_EXPERT, { (void*)opt_vismv }, "visualize motion vectors", "" },
2976
 
    { "fast", OPT_BOOL | OPT_EXPERT, { (void*)&fast }, "non spec compliant optimizations", "" },
2977
 
    { "genpts", OPT_BOOL | OPT_EXPERT, { (void*)&genpts }, "generate pts", "" },
2978
 
    { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
2979
 
    { "lowres", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&lowres }, "", "" },
2980
 
    { "skiploop", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&skip_loop_filter }, "", "" },
2981
 
    { "skipframe", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&skip_frame }, "", "" },
2982
 
    { "skipidct", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&skip_idct }, "", "" },
2983
 
    { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&idct }, "set idct algo",  "algo" },
2984
 
    { "er", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&error_recognition }, "set error detection threshold (0-4)",  "threshold" },
2985
 
    { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&error_concealment }, "set error concealment options",  "bit_mask" },
2986
 
    { "sync", HAS_ARG | OPT_EXPERT, { (void*)opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
2987
 
    { "autoexit", OPT_BOOL | OPT_EXPERT, { (void*)&autoexit }, "exit at the end", "" },
2988
 
    { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { (void*)&exit_on_keydown }, "exit on key down", "" },
2989
 
    { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { (void*)&exit_on_mousedown }, "exit on mouse down", "" },
2990
 
    { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { (void*)&loop }, "set number of times the playback shall be looped", "loop count" },
2991
 
    { "framedrop", OPT_BOOL | OPT_EXPERT, { (void*)&framedrop }, "drop frames when cpu is too slow", "" },
2992
 
    { "window_title", OPT_STRING | HAS_ARG, { (void*)&window_title }, "set window title", "window title" },
 
2870
    { "x", HAS_ARG, { .func_arg = opt_width }, "force displayed width", "width" },
 
2871
    { "y", HAS_ARG, { .func_arg = opt_height }, "force displayed height", "height" },
 
2872
    { "s", HAS_ARG | OPT_VIDEO, { .func_arg = opt_frame_size }, "set frame size (WxH or abbreviation)", "size" },
 
2873
    { "fs", OPT_BOOL, { &is_full_screen }, "force full screen" },
 
2874
    { "an", OPT_BOOL, { &audio_disable }, "disable audio" },
 
2875
    { "vn", OPT_BOOL, { &video_disable }, "disable video" },
 
2876
    { "ast", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_AUDIO] }, "select desired audio stream", "stream_number" },
 
2877
    { "vst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_VIDEO] }, "select desired video stream", "stream_number" },
 
2878
    { "sst", OPT_INT | HAS_ARG | OPT_EXPERT, { &wanted_stream[AVMEDIA_TYPE_SUBTITLE] }, "select desired subtitle stream", "stream_number" },
 
2879
    { "ss", HAS_ARG, { .func_arg = opt_seek }, "seek to a given position in seconds", "pos" },
 
2880
    { "t", HAS_ARG, { .func_arg = opt_duration }, "play  \"duration\" seconds of audio/video", "duration" },
 
2881
    { "bytes", OPT_INT | HAS_ARG, { &seek_by_bytes }, "seek by bytes 0=off 1=on -1=auto", "val" },
 
2882
    { "nodisp", OPT_BOOL, { &display_disable }, "disable graphical display" },
 
2883
    { "f", HAS_ARG, { .func_arg = opt_format }, "force format", "fmt" },
 
2884
    { "pix_fmt", HAS_ARG | OPT_EXPERT | OPT_VIDEO, { .func_arg = opt_frame_pix_fmt }, "set pixel format", "format" },
 
2885
    { "stats", OPT_BOOL | OPT_EXPERT, { &show_status }, "show status", "" },
 
2886
    { "debug", HAS_ARG | OPT_EXPERT, { .func_arg = opt_debug }, "print specific debug info", "" },
 
2887
    { "bug", OPT_INT | HAS_ARG | OPT_EXPERT, { &workaround_bugs }, "workaround bugs", "" },
 
2888
    { "vismv", HAS_ARG | OPT_EXPERT, { .func_arg = opt_vismv }, "visualize motion vectors", "" },
 
2889
    { "fast", OPT_BOOL | OPT_EXPERT, { &fast }, "non spec compliant optimizations", "" },
 
2890
    { "genpts", OPT_BOOL | OPT_EXPERT, { &genpts }, "generate pts", "" },
 
2891
    { "drp", OPT_INT | HAS_ARG | OPT_EXPERT, { &decoder_reorder_pts }, "let decoder reorder pts 0=off 1=on -1=auto", ""},
 
2892
    { "skiploop", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_loop_filter }, "", "" },
 
2893
    { "skipframe", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_frame }, "", "" },
 
2894
    { "skipidct", OPT_INT | HAS_ARG | OPT_EXPERT, { &skip_idct }, "", "" },
 
2895
    { "idct", OPT_INT | HAS_ARG | OPT_EXPERT, { &idct }, "set idct algo",  "algo" },
 
2896
    { "ec", OPT_INT | HAS_ARG | OPT_EXPERT, { &error_concealment }, "set error concealment options",  "bit_mask" },
 
2897
    { "sync", HAS_ARG | OPT_EXPERT, { .func_arg = opt_sync }, "set audio-video sync. type (type=audio/video/ext)", "type" },
 
2898
    { "autoexit", OPT_BOOL | OPT_EXPERT, { &autoexit }, "exit at the end", "" },
 
2899
    { "exitonkeydown", OPT_BOOL | OPT_EXPERT, { &exit_on_keydown }, "exit on key down", "" },
 
2900
    { "exitonmousedown", OPT_BOOL | OPT_EXPERT, { &exit_on_mousedown }, "exit on mouse down", "" },
 
2901
    { "loop", OPT_INT | HAS_ARG | OPT_EXPERT, { &loop }, "set number of times the playback shall be looped", "loop count" },
 
2902
    { "framedrop", OPT_BOOL | OPT_EXPERT, { &framedrop }, "drop frames when cpu is too slow", "" },
 
2903
    { "infbuf", OPT_BOOL | OPT_EXPERT, { &infinite_buffer }, "don't limit the input buffer size (useful with realtime streams)", "" },
 
2904
    { "window_title", OPT_STRING | HAS_ARG, { &window_title }, "set window title", "window title" },
2993
2905
#if CONFIG_AVFILTER
2994
 
    { "vf", OPT_STRING | HAS_ARG, { (void*)&vfilters }, "video filters", "filter list" },
 
2906
    { "vf", OPT_STRING | HAS_ARG, { &vfilters }, "video filters", "filter list" },
2995
2907
#endif
2996
 
    { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { (void*)&rdftspeed }, "rdft speed", "msecs" },
2997
 
    { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { (void*)opt_default }, "generic catch all option", "" },
 
2908
    { "rdftspeed", OPT_INT | HAS_ARG| OPT_AUDIO | OPT_EXPERT, { &rdftspeed }, "rdft speed", "msecs" },
 
2909
    { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, { opt_default }, "generic catch all option", "" },
2998
2910
    { "i", 0, { NULL }, "avconv compatibility dummy option", ""},
2999
2911
    { NULL, },
3000
2912
};
3006
2918
    printf("\n");
3007
2919
}
3008
2920
 
3009
 
static void show_help(void)
 
2921
void show_help_default(const char *opt, const char *arg)
3010
2922
{
3011
2923
    av_log_set_callback(log_callback_help);
3012
2924
    show_usage();
3013
 
    show_help_options(options, "Main options:\n",
3014
 
                      OPT_EXPERT, 0);
3015
 
    show_help_options(options, "\nAdvanced options:\n",
3016
 
                      OPT_EXPERT, OPT_EXPERT);
 
2925
    show_help_options(options, "Main options:", 0, OPT_EXPERT, 0);
 
2926
    show_help_options(options, "Advanced options:", OPT_EXPERT, 0, 0);
3017
2927
    printf("\n");
3018
2928
    show_help_children(avcodec_get_class(), AV_OPT_FLAG_DECODING_PARAM);
3019
2929
    show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);