History

Merge commit '972c71e9cb63e24f57ee481e413199c7d88a8813'

* commit '972c71e9cb63e24f57ee481e413199c7d88a8813': lavc: add support for filtering packets before decoding

James Almer committed 9 years ago

Commit 79778bb2 parents f92e1af 972c71e6 files · +170 −13Browse files

Unified · Split

6 files
  1. configure +1 −0
  2. libavcodec/avcodec.h +6 −0
  3. libavcodec/decode.c +152 −13
  4. libavcodec/decode.h +2 −0
  5. libavcodec/internal.h +6 −0
  6. libavcodec/utils.c +3 −0
configure+1−0
@@ -3187,6 +3187,7 @@ transcoding_example_deps="avfilter avcodec avformat avutil" · 3186 unmodified lines
31873187
31883188 # libraries, in linking order
31893189 avcodec_deps="avutil"
3190+avcodec_select="null_bsf"
31903191 avdevice_deps="avformat avcodec avutil"
31913192 avfilter_deps="avutil"
31923193 avformat_deps="avcodec avutil"
libavcodec/avcodec.h+6−0
@@ -3790,6 +3790,12 @@ typedef struct AVCodec { · 3789 unmodified lines
37903790 * See FF_CODEC_CAP_* in internal.h
37913791 */
37923792 int caps_internal;
3793+
3794+ /**
3795+ * Decoding only, a comma-separated list of bitstream filters to apply to
3796+ * packets before decoding.
3797+ */
3798+ const char *bsfs;
37933799 } AVCodec;
37943800
37953801 int av_codec_get_max_lowres(const AVCodec *codec);
libavcodec/decode.c+152−13
@@ -179,6 +179,116 @@ static int unrefcount_frame(AVCodecInternal *avci, AVFrame *frame) · 178 unmodified lines
179179 return 0;
180180 }
181181
182+static int bsfs_init(AVCodecContext *avctx)
183+{
184+ AVCodecInternal *avci = avctx->internal;
185+ DecodeFilterContext *s = &avci->filter;
186+ const char *bsfs_str;
187+ int ret;
188+
189+ if (s->nb_bsfs)
190+ return 0;
191+
192+ bsfs_str = avctx->codec->bsfs ? avctx->codec->bsfs : "null";
193+ while (bsfs_str && *bsfs_str) {
194+ AVBSFContext **tmp;
195+ const AVBitStreamFilter *filter;
196+ char *bsf;
197+
198+ bsf = av_get_token(&bsfs_str, ",");
199+ if (!bsf) {
200+ ret = AVERROR(ENOMEM);
201+ goto fail;
202+ }
203+
204+ filter = av_bsf_get_by_name(bsf);
205+ if (!filter) {
206+ av_log(avctx, AV_LOG_ERROR, "A non-existing bitstream filter %s "
207+ "requested by a decoder. This is a bug, please report it.\n",
208+ bsf);
209+ ret = AVERROR_BUG;
210+ av_freep(&bsf);
211+ goto fail;
212+ }
213+ av_freep(&bsf);
214+
215+ tmp = av_realloc_array(s->bsfs, s->nb_bsfs + 1, sizeof(*s->bsfs));
216+ if (!tmp) {
217+ ret = AVERROR(ENOMEM);
218+ goto fail;
219+ }
220+ s->bsfs = tmp;
221+ s->nb_bsfs++;
222+
223+ ret = av_bsf_alloc(filter, &s->bsfs[s->nb_bsfs - 1]);
224+ if (ret < 0)
225+ goto fail;
226+
227+ if (s->nb_bsfs == 1) {
228+ /* We do not currently have an API for passing the input timebase into decoders,
229+ * but no filters used here should actually need it.
230+ * So we make up some plausible-looking number (the MPEG 90kHz timebase) */
231+ s->bsfs[s->nb_bsfs - 1]->time_base_in = (AVRational){ 1, 90000 };
232+ ret = avcodec_parameters_from_context(s->bsfs[s->nb_bsfs - 1]->par_in,
233+ avctx);
234+ } else {
235+ s->bsfs[s->nb_bsfs - 1]->time_base_in = s->bsfs[s->nb_bsfs - 2]->time_base_out;
236+ ret = avcodec_parameters_copy(s->bsfs[s->nb_bsfs - 1]->par_in,
237+ s->bsfs[s->nb_bsfs - 2]->par_out);
238+ }
239+ if (ret < 0)
240+ goto fail;
241+
242+ ret = av_bsf_init(s->bsfs[s->nb_bsfs - 1]);
243+ if (ret < 0)
244+ goto fail;
245+ }
246+
247+ return 0;
248+fail:
249+ ff_decode_bsfs_uninit(avctx);
250+ return ret;
251+}
252+
253+/* try to get one output packet from the filter chain */
254+static int bsfs_poll(AVCodecContext *avctx, AVPacket *pkt)
255+{
256+ DecodeFilterContext *s = &avctx->internal->filter;
257+ int idx, ret;
258+
259+ /* start with the last filter in the chain */
260+ idx = s->nb_bsfs - 1;
261+ while (idx >= 0) {
262+ /* request a packet from the currently selected filter */
263+ ret = av_bsf_receive_packet(s->bsfs[idx], pkt);
264+ if (ret == AVERROR(EAGAIN)) {
265+ /* no packets available, try the next filter up the chain */
266+ ret = 0;
267+ idx--;
268+ continue;
269+ } else if (ret < 0 && ret != AVERROR_EOF) {
270+ return ret;
271+ }
272+
273+ /* got a packet or EOF -- pass it to the caller or to the next filter
274+ * down the chain */
275+ if (idx == s->nb_bsfs - 1) {
276+ return ret;
277+ } else {
278+ idx++;
279+ ret = av_bsf_send_packet(s->bsfs[idx], ret < 0 ? NULL : pkt);
280+ if (ret < 0) {
281+ av_log(avctx, AV_LOG_ERROR,
282+ "Error pre-processing a packet before decoding\n");
283+ av_packet_unref(pkt);
284+ return ret;
285+ }
286+ }
287+ }
288+
289+ return AVERROR(EAGAIN);
290+}
291+
182292 int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
183293 {
184294 AVCodecInternal *avci = avctx->internal;
@@ -187,10 +297,11 @@ int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt) · 2 unmodified lines
187297 if (avci->draining)
188298 return AVERROR_EOF;
189299
190− if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data_elems)
191− return AVERROR(EAGAIN);
192−
193− av_packet_move_ref(pkt, avci->buffer_pkt);
300+ ret = bsfs_poll(avctx, pkt);
301+ if (ret == AVERROR_EOF)
302+ avci->draining = 1;
303+ if (ret < 0)
304+ return ret;
194305
195306 ret = extract_packet_props(avctx->internal, pkt);
196307 if (ret < 0)
@@ -525,20 +636,26 @@ int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacke · 328 unmodified lines
525636 if (avctx->internal->draining)
526637 return AVERROR_EOF;
527638
528− if (avci->buffer_pkt->data || avci->buffer_pkt->side_data_elems)
529− return AVERROR(EAGAIN);
530−
531639 if (avpkt && !avpkt->size && avpkt->data)
532640 return AVERROR(EINVAL);
533641
534− if (!avpkt || !avpkt->size) {
535− avctx->internal->draining = 1;
536− } else {
642+ ret = bsfs_init(avctx);
643+ if (ret < 0)
644+ return ret;
645+
646+ av_packet_unref(avci->buffer_pkt);
647+ if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
537648 ret = av_packet_ref(avci->buffer_pkt, avpkt);
538649 if (ret < 0)
539650 return ret;
540651 }
541652
653+ ret = av_bsf_send_packet(avci->filter.bsfs[0], avci->buffer_pkt);
654+ if (ret < 0) {
655+ av_packet_unref(avci->buffer_pkt);
656+ return ret;
657+ }
658+
542659 if (!avci->buffer_frame->buf[0]) {
543660 ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
544661 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
@@ -558,6 +675,10 @@ int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *fr · 13 unmodified lines
558675 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
559676 return AVERROR(EINVAL);
560677
678+ ret = bsfs_init(avctx);
679+ if (ret < 0)
680+ return ret;
681+
561682 if (avci->buffer_frame->buf[0]) {
562683 av_frame_move_ref(frame, avci->buffer_frame);
563684 } else {
@@ -630,13 +751,18 @@ static int compat_decode(AVCodecContext *avctx, AVFrame *frame, · 66 unmodified lines
630751 }
631752 }
632753
633− if (avci->draining || avci->compat_decode_consumed < pkt->size)
754+ if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size))
634755 break;
635756 }
636757
637758 finish:
638− if (ret == 0)
639− ret = FFMIN(avci->compat_decode_consumed, pkt->size);
759+ if (ret == 0) {
760+ /* if there are any bsfs then assume full packet is always consumed */
761+ if (avctx->codec->bsfs)
762+ ret = pkt->size;
763+ else
764+ ret = FFMIN(avci->compat_decode_consumed, pkt->size);
765+ }
640766 avci->compat_decode_consumed = 0;
641767 avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
642768
@@ -1549,6 +1675,19 @@ void avcodec_flush_buffers(AVCodecContext *avctx) · 906 unmodified lines
15491675 avctx->pts_correction_last_pts =
15501676 avctx->pts_correction_last_dts = INT64_MIN;
15511677
1678+ ff_decode_bsfs_uninit(avctx);
1679+
15521680 if (!avctx->refcounted_frames)
15531681 av_frame_unref(avctx->internal->to_free);
15541682 }
1683+
1684+void ff_decode_bsfs_uninit(AVCodecContext *avctx)
1685+{
1686+ DecodeFilterContext *s = &avctx->internal->filter;
1687+ int i;
1688+
1689+ for (i = 0; i < s->nb_bsfs; i++)
1690+ av_bsf_free(&s->bsfs[i]);
1691+ av_freep(&s->bsfs);
1692+ s->nb_bsfs = 0;
1693+}
libavcodec/decode.h+2−0
@@ -32,4 +32,6 @@ · 31 unmodified lines
3232 */
3333 int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt);
3434
35+void ff_decode_bsfs_uninit(AVCodecContext *avctx);
36+
3537 #endif /* AVCODEC_DECODE_H */
libavcodec/internal.h+6−0
@@ -106,6 +106,11 @@ typedef struct DecodeSimpleContext { · 105 unmodified lines
106106 AVFrame *out_frame;
107107 } DecodeSimpleContext;
108108
109+typedef struct DecodeFilterContext {
110+ AVBSFContext **bsfs;
111+ int nb_bsfs;
112+} DecodeFilterContext;
113+
109114 typedef struct AVCodecInternal {
110115 /**
111116 * Whether the parent AVCodecContext is a copy of the context which had
@@ -143,6 +148,7 @@ typedef struct AVCodecInternal { · 31 unmodified lines
143148 void *thread_ctx;
144149
145150 DecodeSimpleContext ds;
151+ DecodeFilterContext filter;
146152
147153 /**
148154 * Properties (timestamps+side data) extracted from the last packet passed
libavcodec/utils.c+3−0
@@ -44,6 +44,7 @@ · 43 unmodified lines
4444 #include "libavutil/dict.h"
4545 #include "libavutil/thread.h"
4646 #include "avcodec.h"
47+#include "decode.h"
4748 #include "libavutil/opt.h"
4849 #include "me_cmp.h"
4950 #include "mpegvideo.h"
@@ -1194,6 +1195,8 @@ av_cold int avcodec_close(AVCodecContext *avctx) · 1144 unmodified lines
11941195 avctx->hwaccel->uninit(avctx);
11951196 av_freep(&avctx->internal->hwaccel_priv_data);
11961197
1198+ ff_decode_bsfs_uninit(avctx);
1199+
11971200 av_freep(&avctx->internal);
11981201 }
11991202

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t