X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fbsf.c;h=9d67ea5395222972a6283afa7bec4e7f4a9807d6;hb=0bf3a7361d17d596a5044882098f56817db0e103;hp=508130760335fa6d285e7262053d7a2cfc08681c;hpb=674b59c07515409eb815c91bee90fe4151905a10;p=ffmpeg diff --git a/libavcodec/bsf.c b/libavcodec/bsf.c index 50813076033..9d67ea53952 100644 --- a/libavcodec/bsf.c +++ b/libavcodec/bsf.c @@ -18,14 +18,19 @@ #include +#include "libavutil/avassert.h" #include "libavutil/log.h" #include "libavutil/mem.h" #include "libavutil/opt.h" #include "libavutil/avstring.h" #include "libavutil/bprint.h" -#include "avcodec.h" #include "bsf.h" +#include "bsf_internal.h" +#include "codec_desc.h" +#include "codec_par.h" + +#define IS_EMPTY(pkt) (!(pkt)->data && !(pkt)->side_data_elems) struct AVBSFInternal { AVPacket *buffer_pkt; @@ -40,15 +45,15 @@ void av_bsf_free(AVBSFContext **pctx) return; ctx = *pctx; - if (ctx->filter->close) - ctx->filter->close(ctx); + if (ctx->internal) { + if (ctx->filter->close) + ctx->filter->close(ctx); + av_packet_free(&ctx->internal->buffer_pkt); + av_freep(&ctx->internal); + } if (ctx->filter->priv_class && ctx->priv_data) av_opt_free(ctx->priv_data); - av_opt_free(ctx); - - av_packet_free(&ctx->internal->buffer_pkt); - av_freep(&ctx->internal); av_freep(&ctx->priv_data); avcodec_parameters_free(&ctx->par_in); @@ -65,12 +70,18 @@ static void *bsf_child_next(void *obj, void *prev) return NULL; } +static const char *bsf_to_name(void *bsf) +{ + return ((AVBSFContext *)bsf)->filter->name; +} + static const AVClass bsf_class = { .class_name = "AVBSFContext", - .item_name = av_default_item_name, + .item_name = bsf_to_name, .version = LIBAVUTIL_VERSION_INT, .child_next = bsf_child_next, - .child_class_next = ff_bsf_child_class_next, + .child_class_iterate = ff_bsf_child_class_iterate, + .category = AV_CLASS_CATEGORY_BITSTREAM_FILTER, }; const AVClass *av_bsf_get_class(void) @@ -81,6 +92,7 @@ const AVClass *av_bsf_get_class(void) int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx) { AVBSFContext *ctx; + AVBSFInternal *bsfi; int ret; ctx = av_mallocz(sizeof(*ctx)); @@ -96,21 +108,6 @@ int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx) ret = AVERROR(ENOMEM); goto fail; } - - ctx->internal = av_mallocz(sizeof(*ctx->internal)); - if (!ctx->internal) { - ret = AVERROR(ENOMEM); - goto fail; - } - - ctx->internal->buffer_pkt = av_packet_alloc(); - if (!ctx->internal->buffer_pkt) { - ret = AVERROR(ENOMEM); - goto fail; - } - - av_opt_set_defaults(ctx); - /* allocate priv data and init private options */ if (filter->priv_data_size) { ctx->priv_data = av_mallocz(filter->priv_data_size); @@ -123,6 +120,20 @@ int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx) av_opt_set_defaults(ctx->priv_data); } } + /* Allocate AVBSFInternal; must happen after priv_data has been allocated + * so that a filter->close needing priv_data is never called without. */ + bsfi = av_mallocz(sizeof(*bsfi)); + if (!bsfi) { + ret = AVERROR(ENOMEM); + goto fail; + } + ctx->internal = bsfi; + + bsfi->buffer_pkt = av_packet_alloc(); + if (!bsfi->buffer_pkt) { + ret = AVERROR(ENOMEM); + goto fail; + } *pctx = ctx; return 0; @@ -146,9 +157,9 @@ int av_bsf_init(AVBSFContext *ctx) "bitstream filter '%s'. Supported codecs are: ", desc ? desc->name : "unknown", ctx->par_in->codec_id, ctx->filter->name); for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) { - desc = avcodec_descriptor_get(ctx->filter->codec_ids[i]); + enum AVCodecID codec_id = ctx->filter->codec_ids[i]; av_log(ctx, AV_LOG_ERROR, "%s (%d) ", - desc ? desc->name : "unknown", ctx->filter->codec_ids[i]); + avcodec_get_name(codec_id), codec_id); } av_log(ctx, AV_LOG_ERROR, "\n"); return AVERROR(EINVAL); @@ -174,9 +185,11 @@ int av_bsf_init(AVBSFContext *ctx) void av_bsf_flush(AVBSFContext *ctx) { - ctx->internal->eof = 0; + AVBSFInternal *bsfi = ctx->internal; + + bsfi->eof = 0; - av_packet_unref(ctx->internal->buffer_pkt); + av_packet_unref(bsfi->buffer_pkt); if (ctx->filter->flush) ctx->filter->flush(ctx); @@ -184,26 +197,26 @@ void av_bsf_flush(AVBSFContext *ctx) int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt) { + AVBSFInternal *bsfi = ctx->internal; int ret; - if (!pkt || (!pkt->data && !pkt->side_data_elems)) { - ctx->internal->eof = 1; + if (!pkt || IS_EMPTY(pkt)) { + bsfi->eof = 1; return 0; } - if (ctx->internal->eof) { + if (bsfi->eof) { av_log(ctx, AV_LOG_ERROR, "A non-NULL packet sent after an EOF.\n"); return AVERROR(EINVAL); } - if (ctx->internal->buffer_pkt->data || - ctx->internal->buffer_pkt->side_data_elems) + if (!IS_EMPTY(bsfi->buffer_pkt)) return AVERROR(EAGAIN); ret = av_packet_make_refcounted(pkt); if (ret < 0) return ret; - av_packet_move_ref(ctx->internal->buffer_pkt, pkt); + av_packet_move_ref(bsfi->buffer_pkt, pkt); return 0; } @@ -215,38 +228,36 @@ int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt) int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt) { - AVBSFInternal *in = ctx->internal; + AVBSFInternal *bsfi = ctx->internal; AVPacket *tmp_pkt; - if (in->eof) + if (bsfi->eof) return AVERROR_EOF; - if (!ctx->internal->buffer_pkt->data && - !ctx->internal->buffer_pkt->side_data_elems) + if (IS_EMPTY(bsfi->buffer_pkt)) return AVERROR(EAGAIN); tmp_pkt = av_packet_alloc(); if (!tmp_pkt) return AVERROR(ENOMEM); - *pkt = ctx->internal->buffer_pkt; - ctx->internal->buffer_pkt = tmp_pkt; + *pkt = bsfi->buffer_pkt; + bsfi->buffer_pkt = tmp_pkt; return 0; } int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt) { - AVBSFInternal *in = ctx->internal; + AVBSFInternal *bsfi = ctx->internal; - if (in->eof) + if (bsfi->eof) return AVERROR_EOF; - if (!ctx->internal->buffer_pkt->data && - !ctx->internal->buffer_pkt->side_data_elems) + if (IS_EMPTY(bsfi->buffer_pkt)) return AVERROR(EAGAIN); - av_packet_move_ref(pkt, ctx->internal->buffer_pkt); + av_packet_move_ref(pkt, bsfi->buffer_pkt); return 0; } @@ -258,7 +269,6 @@ typedef struct BSFListContext { int nb_bsfs; unsigned idx; // index of currently processed BSF - unsigned flushed_idx; // index of BSF being flushed char * item_name; } BSFListContext; @@ -296,58 +306,43 @@ fail: static int bsf_list_filter(AVBSFContext *bsf, AVPacket *out) { BSFListContext *lst = bsf->priv_data; - int ret; + int ret, eof = 0; if (!lst->nb_bsfs) return ff_bsf_get_packet_ref(bsf, out); while (1) { - if (lst->idx > lst->flushed_idx) { + /* get a packet from the previous filter up the chain */ + if (lst->idx) ret = av_bsf_receive_packet(lst->bsfs[lst->idx-1], out); - if (ret == AVERROR(EAGAIN)) { - /* no more packets from idx-1, try with previous */ - ret = 0; - lst->idx--; - continue; - } else if (ret == AVERROR_EOF) { - /* filter idx-1 is done, continue with idx...nb_bsfs */ - lst->flushed_idx = lst->idx; - continue; - }else if (ret < 0) { - /* filtering error */ - break; - } - } else { + else ret = ff_bsf_get_packet_ref(bsf, out); - if (ret == AVERROR_EOF) { - lst->idx = lst->flushed_idx; - } else if (ret < 0) - break; - } + if (ret == AVERROR(EAGAIN)) { + if (!lst->idx) + return ret; + lst->idx--; + continue; + } else if (ret == AVERROR_EOF) { + eof = 1; + } else if (ret < 0) + return ret; + /* send it to the next filter down the chain */ if (lst->idx < lst->nb_bsfs) { - AVPacket *pkt; - if (ret == AVERROR_EOF && lst->idx == lst->flushed_idx) { - /* ff_bsf_get_packet_ref returned EOF and idx is first - * filter of yet not flushed filter chain */ - pkt = NULL; - } else { - pkt = out; + ret = av_bsf_send_packet(lst->bsfs[lst->idx], eof ? NULL : out); + av_assert1(ret != AVERROR(EAGAIN)); + if (ret < 0) { + av_packet_unref(out); + return ret; } - ret = av_bsf_send_packet(lst->bsfs[lst->idx], pkt); - if (ret < 0) - break; lst->idx++; + eof = 0; + } else if (eof) { + return ret; } else { - /* The end of filter chain, break to return result */ - break; + return 0; } } - - if (ret < 0) - av_packet_unref(out); - - return ret; } static void bsf_list_flush(AVBSFContext *bsf) @@ -356,7 +351,7 @@ static void bsf_list_flush(AVBSFContext *bsf) for (int i = 0; i < lst->nb_bsfs; i++) av_bsf_flush(lst->bsfs[i]); - lst->idx = lst->flushed_idx = 0; + lst->idx = 0; } static void bsf_list_close(AVBSFContext *bsf) @@ -439,7 +434,7 @@ int av_bsf_list_append(AVBSFList *lst, AVBSFContext *bsf) return av_dynarray_add_nofree(&lst->bsfs, &lst->nb_bsfs, bsf); } -int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options) +static int bsf_list_append_internal(AVBSFList *lst, const char *bsf_name, const char *options, AVDictionary ** options_dict) { int ret; const AVBitStreamFilter *filter; @@ -453,8 +448,20 @@ int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** op if (ret < 0) return ret; - if (options) { - ret = av_opt_set_dict2(bsf, options, AV_OPT_SEARCH_CHILDREN); + if (options && filter->priv_class) { + const AVOption *opt = av_opt_next(bsf->priv_data, NULL); + const char * shorthand[2] = {NULL}; + + if (opt) + shorthand[0] = opt->name; + + ret = av_opt_set_from_string(bsf->priv_data, options, shorthand, "=", ":"); + if (ret < 0) + goto end; + } + + if (options_dict) { + ret = av_opt_set_dict2(bsf, options_dict, AV_OPT_SEARCH_CHILDREN); if (ret < 0) goto end; } @@ -468,6 +475,11 @@ end: return ret; } +int av_bsf_list_append2(AVBSFList *lst, const char *bsf_name, AVDictionary ** options) +{ + return bsf_list_append_internal(lst, bsf_name, NULL, options); +} + int av_bsf_list_finalize(AVBSFList **lst, AVBSFContext **bsf) { int ret = 0; @@ -494,33 +506,15 @@ end: return ret; } -static int bsf_parse_single(const char *str, AVBSFList *bsf_lst) +static int bsf_parse_single(char *str, AVBSFList *bsf_lst) { - char *bsf_name, *bsf_options_str, *buf; - AVDictionary *bsf_options = NULL; - int ret = 0; - - if (!(buf = av_strdup(str))) - return AVERROR(ENOMEM); - - bsf_name = av_strtok(buf, "=", &bsf_options_str); - if (!bsf_name) { - ret = AVERROR(EINVAL); - goto end; - } + char *bsf_name, *bsf_options_str; - if (bsf_options_str) { - ret = av_dict_parse_string(&bsf_options, bsf_options_str, "=", ":", 0); - if (ret < 0) - goto end; - } - - ret = av_bsf_list_append2(bsf_lst, bsf_name, &bsf_options); + bsf_name = av_strtok(str, "=", &bsf_options_str); + if (!bsf_name) + return AVERROR(EINVAL); - av_dict_free(&bsf_options); -end: - av_free(buf); - return ret; + return bsf_list_append_internal(bsf_lst, bsf_name, bsf_options_str, NULL); } int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst) @@ -541,11 +535,7 @@ int av_bsf_list_parse_str(const char *str, AVBSFContext **bsf_lst) goto end; } - while (1) { - bsf_str = av_strtok(buf, ",", &saveptr); - if (!bsf_str) - break; - + while (bsf_str = av_strtok(buf, ",", &saveptr)) { ret = bsf_parse_single(bsf_str, lst); if (ret < 0) goto end;