2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 /* shared code for simple demux/decode tools */
24 #include "decode_simple.h"
26 #include "libavformat/avformat.h"
28 #include "libavcodec/avcodec.h"
29 #include "libavcodec/packet.h"
31 #include "libavutil/dict.h"
32 #include "libavutil/error.h"
33 #include "libavutil/frame.h"
35 static int decode_read(DecodeContext
*dc
, int flush
)
37 const int ret_done
= flush
? AVERROR_EOF
: AVERROR(EAGAIN
);
41 (dc
->max_frames
== 0 || dc
->decoder
->frame_number
< dc
->max_frames
)) {
42 ret
= avcodec_receive_frame(dc
->decoder
, dc
->frame
);
44 if (ret
== AVERROR_EOF
) {
45 int err
= dc
->process_frame(dc
, NULL
);
50 return (ret
== ret_done
) ? 0 : ret
;
53 ret
= dc
->process_frame(dc
, dc
->frame
);
54 av_frame_unref(dc
->frame
);
58 if (dc
->max_frames
&& dc
->decoder
->frame_number
== dc
->max_frames
)
62 return (dc
->max_frames
== 0 || dc
->decoder
->frame_number
< dc
->max_frames
) ? 0 : 1;
65 int ds_run(DecodeContext
*dc
)
69 ret
= avcodec_open2(dc
->decoder
, NULL
, &dc
->decoder_opts
);
74 ret
= av_read_frame(dc
->demuxer
, dc
->pkt
);
77 if (dc
->pkt
->stream_index
!= dc
->stream
->index
) {
78 av_packet_unref(dc
->pkt
);
82 ret
= avcodec_send_packet(dc
->decoder
, dc
->pkt
);
84 fprintf(stderr
, "Error decoding: %d\n", ret
);
87 av_packet_unref(dc
->pkt
);
89 ret
= decode_read(dc
, 0);
91 fprintf(stderr
, "Error decoding: %d\n", ret
);
98 avcodec_send_packet(dc
->decoder
, NULL
);
99 ret
= decode_read(dc
, 1);
101 fprintf(stderr
, "Error flushing: %d\n", ret
);
108 void ds_free(DecodeContext
*dc
)
110 av_dict_free(&dc
->decoder_opts
);
112 av_frame_free(&dc
->frame
);
113 av_packet_free(&dc
->pkt
);
115 avcodec_free_context(&dc
->decoder
);
116 avformat_close_input(&dc
->demuxer
);
119 int ds_open(DecodeContext
*dc
, const char *url
, int stream_idx
)
121 const AVCodec
*codec
;
124 memset(dc
, 0, sizeof(*dc
));
126 dc
->pkt
= av_packet_alloc();
127 dc
->frame
= av_frame_alloc();
128 if (!dc
->pkt
|| !dc
->frame
) {
129 ret
= AVERROR(ENOMEM
);
133 ret
= avformat_open_input(&dc
->demuxer
, url
, NULL
, NULL
);
135 fprintf(stderr
, "Error opening input file: %d\n", ret
);
139 if (stream_idx
< 0 || stream_idx
>= dc
->demuxer
->nb_streams
)
140 return AVERROR(EINVAL
);
142 dc
->stream
= dc
->demuxer
->streams
[stream_idx
];
144 codec
= avcodec_find_decoder(dc
->stream
->codecpar
->codec_id
);
146 return AVERROR_DECODER_NOT_FOUND
;
148 dc
->decoder
= avcodec_alloc_context3(codec
);
150 return AVERROR(ENOMEM
);