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
3187 3187
3188# libraries, in linking order3188# libraries, in linking order
3189avcodec_deps="avutil"3189avcodec_deps="avutil"
3190avcodec_select="null_bsf"
3190avdevice_deps="avformat avcodec avutil"3191avdevice_deps="avformat avcodec avutil"
3191avfilter_deps="avutil"3192avfilter_deps="avutil"
3192avformat_deps="avcodec avutil"3193avformat_deps="avcodec avutil"
libavcodec/avcodec.h+6−0
@@ -3790,6 +3790,12 @@ typedef struct AVCodec { · 3789 unmodified lines
3790 * See FF_CODEC_CAP_* in internal.h3790 * See FF_CODEC_CAP_* in internal.h
3791 */3791 */
3792 int caps_internal;3792 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;
3793} AVCodec;3799} AVCodec;
3794 3800
3795int av_codec_get_max_lowres(const AVCodec *codec);3801int 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
179 return 0;179 return 0;
180}180}
181 181
182static 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;
248fail:
249 ff_decode_bsfs_uninit(avctx);
250 return ret;
251}
252
253/* try to get one output packet from the filter chain */
254static 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
182int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)292int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
183{293{
184 AVCodecInternal *avci = avctx->internal;294 AVCodecInternal *avci = avctx->internal;
@@ -187,10 +297,11 @@ int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt) · 2 unmodified lines
187 if (avci->draining)297 if (avci->draining)
188 return AVERROR_EOF;298 return AVERROR_EOF;
189 299
190 if (!avci->buffer_pkt->data && !avci->buffer_pkt->side_data_elems)300 ret = bsfs_poll(avctx, pkt);
191 return AVERROR(EAGAIN);301 if (ret == AVERROR_EOF)
192 302 avci->draining = 1;
193 av_packet_move_ref(pkt, avci->buffer_pkt);303 if (ret < 0)
304 return ret;
194 305
195 ret = extract_packet_props(avctx->internal, pkt);306 ret = extract_packet_props(avctx->internal, pkt);
196 if (ret < 0)307 if (ret < 0)
@@ -525,20 +636,26 @@ int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacke · 328 unmodified lines
525 if (avctx->internal->draining)636 if (avctx->internal->draining)
526 return AVERROR_EOF;637 return AVERROR_EOF;
527 638
528 if (avci->buffer_pkt->data || avci->buffer_pkt->side_data_elems)
529 return AVERROR(EAGAIN);
530
531 if (avpkt && !avpkt->size && avpkt->data)639 if (avpkt && !avpkt->size && avpkt->data)
532 return AVERROR(EINVAL);640 return AVERROR(EINVAL);
533 641
534 if (!avpkt || !avpkt->size) {642 ret = bsfs_init(avctx);
535 avctx->internal->draining = 1;643 if (ret < 0)
536 } else {644 return ret;
645
646 av_packet_unref(avci->buffer_pkt);
647 if (avpkt && (avpkt->data || avpkt->side_data_elems)) {
537 ret = av_packet_ref(avci->buffer_pkt, avpkt);648 ret = av_packet_ref(avci->buffer_pkt, avpkt);
538 if (ret < 0)649 if (ret < 0)
539 return ret;650 return ret;
540 }651 }
541 652
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
542 if (!avci->buffer_frame->buf[0]) {659 if (!avci->buffer_frame->buf[0]) {
543 ret = decode_receive_frame_internal(avctx, avci->buffer_frame);660 ret = decode_receive_frame_internal(avctx, avci->buffer_frame);
544 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)661 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
558 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))675 if (!avcodec_is_open(avctx) || !av_codec_is_decoder(avctx->codec))
559 return AVERROR(EINVAL);676 return AVERROR(EINVAL);
560 677
678 ret = bsfs_init(avctx);
679 if (ret < 0)
680 return ret;
681
561 if (avci->buffer_frame->buf[0]) {682 if (avci->buffer_frame->buf[0]) {
562 av_frame_move_ref(frame, avci->buffer_frame);683 av_frame_move_ref(frame, avci->buffer_frame);
563 } else {684 } else {
@@ -630,13 +751,18 @@ static int compat_decode(AVCodecContext *avctx, AVFrame *frame, · 66 unmodified lines
630 }751 }
631 }752 }
632 753
633 if (avci->draining || avci->compat_decode_consumed < pkt->size)754 if (avci->draining || (!avctx->codec->bsfs && avci->compat_decode_consumed < pkt->size))
634 break;755 break;
635 }756 }
636 757
637finish:758finish:
638 if (ret == 0)759 if (ret == 0) {
639 ret = FFMIN(avci->compat_decode_consumed, pkt->size);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 }
640 avci->compat_decode_consumed = 0;766 avci->compat_decode_consumed = 0;
641 avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;767 avci->compat_decode_partial_size = (ret >= 0) ? pkt->size - ret : 0;
642 768
@@ -1549,6 +1675,19 @@ void avcodec_flush_buffers(AVCodecContext *avctx) · 906 unmodified lines
1549 avctx->pts_correction_last_pts =1675 avctx->pts_correction_last_pts =
1550 avctx->pts_correction_last_dts = INT64_MIN;1676 avctx->pts_correction_last_dts = INT64_MIN;
1551 1677
1678 ff_decode_bsfs_uninit(avctx);
1679
1552 if (!avctx->refcounted_frames)1680 if (!avctx->refcounted_frames)
1553 av_frame_unref(avctx->internal->to_free);1681 av_frame_unref(avctx->internal->to_free);
1554}1682}
1683
1684void 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
32 */32 */
33int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt);33int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt);
34 34
35void ff_decode_bsfs_uninit(AVCodecContext *avctx);
36
35#endif /* AVCODEC_DECODE_H */37#endif /* AVCODEC_DECODE_H */
libavcodec/internal.h+6−0
@@ -106,6 +106,11 @@ typedef struct DecodeSimpleContext { · 105 unmodified lines
106 AVFrame *out_frame;106 AVFrame *out_frame;
107} DecodeSimpleContext;107} DecodeSimpleContext;
108 108
109typedef struct DecodeFilterContext {
110 AVBSFContext **bsfs;
111 int nb_bsfs;
112} DecodeFilterContext;
113
109typedef struct AVCodecInternal {114typedef struct AVCodecInternal {
110 /**115 /**
111 * Whether the parent AVCodecContext is a copy of the context which had116 * Whether the parent AVCodecContext is a copy of the context which had
@@ -143,6 +148,7 @@ typedef struct AVCodecInternal { · 31 unmodified lines
143 void *thread_ctx;148 void *thread_ctx;
144 149
145 DecodeSimpleContext ds;150 DecodeSimpleContext ds;
151 DecodeFilterContext filter;
146 152
147 /**153 /**
148 * Properties (timestamps+side data) extracted from the last packet passed154 * Properties (timestamps+side data) extracted from the last packet passed
libavcodec/utils.c+3−0
@@ -44,6 +44,7 @@ · 43 unmodified lines
44#include "libavutil/dict.h"44#include "libavutil/dict.h"
45#include "libavutil/thread.h"45#include "libavutil/thread.h"
46#include "avcodec.h"46#include "avcodec.h"
47#include "decode.h"
47#include "libavutil/opt.h"48#include "libavutil/opt.h"
48#include "me_cmp.h"49#include "me_cmp.h"
49#include "mpegvideo.h"50#include "mpegvideo.h"
@@ -1194,6 +1195,8 @@ av_cold int avcodec_close(AVCodecContext *avctx) · 1144 unmodified lines
1194 avctx->hwaccel->uninit(avctx);1195 avctx->hwaccel->uninit(avctx);
1195 av_freep(&avctx->internal->hwaccel_priv_data);1196 av_freep(&avctx->internal->hwaccel_priv_data);
1196 1197
1198 ff_decode_bsfs_uninit(avctx);
1199
1197 av_freep(&avctx->internal);1200 av_freep(&avctx->internal);
1198 }1201 }
1199 1202

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