* commit '33d18982fa03feb061c8f744a4f0a9175c1f63ab': lavc: add a new bitstream filtering API Conversions-by: Hendrik Leppkes <h.leppkes@gmail.com> Conversions-by: Derek Buitenguis <derek.buitenhuis@gmail.com> Merged-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>pull/206/head
commit
af9cac1be1
26 changed files with 1591 additions and 653 deletions
@ -0,0 +1,137 @@ |
|||||||
|
/*
|
||||||
|
* This file is part of FFmpeg. |
||||||
|
* |
||||||
|
* FFmpeg is free software; you can redistribute it and/or |
||||||
|
* modify it under the terms of the GNU Lesser General Public |
||||||
|
* License as published by the Free Software Foundation; either |
||||||
|
* version 2.1 of the License, or (at your option) any later version. |
||||||
|
* |
||||||
|
* FFmpeg is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||||||
|
* Lesser General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU Lesser General Public |
||||||
|
* License along with FFmpeg; if not, write to the Free Software |
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "config.h" |
||||||
|
|
||||||
|
#include "libavutil/common.h" |
||||||
|
#include "libavutil/log.h" |
||||||
|
|
||||||
|
#include "avcodec.h" |
||||||
|
#include "bsf.h" |
||||||
|
|
||||||
|
extern const AVBitStreamFilter ff_aac_adtstoasc_bsf; |
||||||
|
extern const AVBitStreamFilter ff_chomp_bsf; |
||||||
|
extern const AVBitStreamFilter ff_dump_extradata_bsf; |
||||||
|
extern const AVBitStreamFilter ff_dca_core_bsf; |
||||||
|
extern const AVBitStreamFilter ff_h264_mp4toannexb_bsf; |
||||||
|
extern const AVBitStreamFilter ff_hevc_mp4toannexb_bsf; |
||||||
|
extern const AVBitStreamFilter ff_imx_dump_header_bsf; |
||||||
|
extern const AVBitStreamFilter ff_mjpeg2jpeg_bsf; |
||||||
|
extern const AVBitStreamFilter ff_mjpega_dump_header_bsf; |
||||||
|
extern const AVBitStreamFilter ff_mp3_header_decompress_bsf; |
||||||
|
extern const AVBitStreamFilter ff_mpeg4_unpack_bframes_bsf; |
||||||
|
extern const AVBitStreamFilter ff_mov2textsub_bsf; |
||||||
|
extern const AVBitStreamFilter ff_noise_bsf; |
||||||
|
extern const AVBitStreamFilter ff_remove_extradata_bsf; |
||||||
|
extern const AVBitStreamFilter ff_text2movsub_bsf; |
||||||
|
extern const AVBitStreamFilter ff_vp9_superframe_bsf; |
||||||
|
|
||||||
|
static const AVBitStreamFilter *bitstream_filters[] = { |
||||||
|
#if CONFIG_AAC_ADTSTOASC_BSF |
||||||
|
&ff_aac_adtstoasc_bsf, |
||||||
|
#endif |
||||||
|
#if CONFIG_CHOMP_BSF |
||||||
|
&ff_chomp_bsf, |
||||||
|
#endif |
||||||
|
#if CONFIG_DUMP_EXTRADATA_BSF |
||||||
|
&ff_dump_extradata_bsf, |
||||||
|
#endif |
||||||
|
#if CONFIG_DCA_CORE_BSF |
||||||
|
&ff_dca_core_bsf, |
||||||
|
#endif |
||||||
|
#if CONFIG_H264_MP4TOANNEXB_BSF |
||||||
|
&ff_h264_mp4toannexb_bsf, |
||||||
|
#endif |
||||||
|
#if CONFIG_HEVC_MP4TOANNEXB_BSF |
||||||
|
&ff_hevc_mp4toannexb_bsf, |
||||||
|
#endif |
||||||
|
#if CONFIG_IMX_DUMP_HEADER_BSF |
||||||
|
&ff_imx_dump_header_bsf, |
||||||
|
#endif |
||||||
|
#if CONFIG_MJPEG2JPEG_BSF |
||||||
|
&ff_mjpeg2jpeg_bsf, |
||||||
|
#endif |
||||||
|
#if CONFIG_MJPEGA_DUMP_HEADER_BSF |
||||||
|
&ff_mjpega_dump_header_bsf, |
||||||
|
#endif |
||||||
|
#if CONFIG_MP3_HEADER_DECOMPRESS_BSF |
||||||
|
&ff_mp3_header_decompress_bsf, |
||||||
|
#endif |
||||||
|
#if CONFIG_MPEG4_UNPACK_BFRAMES_BSF |
||||||
|
&ff_mpeg4_unpack_bframes_bsf, |
||||||
|
#endif |
||||||
|
#if CONFIG_MOV2TEXTSUB_BSF |
||||||
|
&ff_mov2textsub_bsf, |
||||||
|
#endif |
||||||
|
#if CONFIG_NOISE_BSF |
||||||
|
&ff_noise_bsf, |
||||||
|
#endif |
||||||
|
#if CONFIG_REMOVE_EXTRADATA_BSF |
||||||
|
&ff_remove_extradata_bsf, |
||||||
|
#endif |
||||||
|
#if CONFIG_TEXT2MOVSUB_BSF |
||||||
|
&ff_text2movsub_bsf, |
||||||
|
#endif |
||||||
|
#if CONFIG_VP9_SUPERFRAME_BSF |
||||||
|
&ff_vp9_superframe_bsf, |
||||||
|
#endif |
||||||
|
NULL, |
||||||
|
}; |
||||||
|
|
||||||
|
const AVBitStreamFilter *av_bsf_next(void **opaque) |
||||||
|
{ |
||||||
|
uintptr_t i = (uintptr_t)*opaque; |
||||||
|
const AVBitStreamFilter *f = bitstream_filters[i]; |
||||||
|
|
||||||
|
if (f) |
||||||
|
*opaque = (void*)(i + 1); |
||||||
|
|
||||||
|
return f; |
||||||
|
} |
||||||
|
|
||||||
|
const AVBitStreamFilter *av_bsf_get_by_name(const char *name) |
||||||
|
{ |
||||||
|
int i; |
||||||
|
|
||||||
|
for (i = 0; bitstream_filters[i]; i++) { |
||||||
|
const AVBitStreamFilter *f = bitstream_filters[i]; |
||||||
|
if (!strcmp(f->name, name)) |
||||||
|
return f; |
||||||
|
} |
||||||
|
|
||||||
|
return NULL; |
||||||
|
} |
||||||
|
|
||||||
|
const AVClass *ff_bsf_child_class_next(const AVClass *prev) |
||||||
|
{ |
||||||
|
int i; |
||||||
|
|
||||||
|
/* find the filter that corresponds to prev */ |
||||||
|
for (i = 0; prev && bitstream_filters[i]; i++) { |
||||||
|
if (bitstream_filters[i]->priv_class == prev) { |
||||||
|
i++; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/* find next filter with priv options */ |
||||||
|
for (; bitstream_filters[i]; i++) |
||||||
|
if (bitstream_filters[i]->priv_class) |
||||||
|
return bitstream_filters[i]->priv_class; |
||||||
|
return NULL; |
||||||
|
} |
@ -0,0 +1,219 @@ |
|||||||
|
/*
|
||||||
|
* This file is part of FFmpeg. |
||||||
|
* |
||||||
|
* FFmpeg is free software; you can redistribute it and/or |
||||||
|
* modify it under the terms of the GNU Lesser General Public |
||||||
|
* License as published by the Free Software Foundation; either |
||||||
|
* version 2.1 of the License, or (at your option) any later version. |
||||||
|
* |
||||||
|
* FFmpeg is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||||||
|
* Lesser General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU Lesser General Public |
||||||
|
* License along with FFmpeg; if not, write to the Free Software |
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <string.h> |
||||||
|
|
||||||
|
#include "libavutil/log.h" |
||||||
|
#include "libavutil/mem.h" |
||||||
|
#include "libavutil/opt.h" |
||||||
|
|
||||||
|
#include "avcodec.h" |
||||||
|
#include "bsf.h" |
||||||
|
|
||||||
|
struct AVBSFInternal { |
||||||
|
AVPacket *buffer_pkt; |
||||||
|
int eof; |
||||||
|
}; |
||||||
|
|
||||||
|
void av_bsf_free(AVBSFContext **pctx) |
||||||
|
{ |
||||||
|
AVBSFContext *ctx; |
||||||
|
|
||||||
|
if (!pctx || !*pctx) |
||||||
|
return; |
||||||
|
ctx = *pctx; |
||||||
|
|
||||||
|
if (ctx->filter->close) |
||||||
|
ctx->filter->close(ctx); |
||||||
|
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); |
||||||
|
avcodec_parameters_free(&ctx->par_out); |
||||||
|
|
||||||
|
av_freep(pctx); |
||||||
|
} |
||||||
|
|
||||||
|
static void *bsf_child_next(void *obj, void *prev) |
||||||
|
{ |
||||||
|
AVBSFContext *ctx = obj; |
||||||
|
if (!prev && ctx->filter->priv_class) |
||||||
|
return ctx->priv_data; |
||||||
|
return NULL; |
||||||
|
} |
||||||
|
|
||||||
|
static const AVClass bsf_class = { |
||||||
|
.class_name = "AVBSFContext", |
||||||
|
.item_name = av_default_item_name, |
||||||
|
.version = LIBAVUTIL_VERSION_INT, |
||||||
|
.child_next = bsf_child_next, |
||||||
|
.child_class_next = ff_bsf_child_class_next, |
||||||
|
}; |
||||||
|
|
||||||
|
const AVClass *av_bsf_get_class(void) |
||||||
|
{ |
||||||
|
return &bsf_class; |
||||||
|
} |
||||||
|
|
||||||
|
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx) |
||||||
|
{ |
||||||
|
AVBSFContext *ctx; |
||||||
|
int ret; |
||||||
|
|
||||||
|
ctx = av_mallocz(sizeof(*ctx)); |
||||||
|
if (!ctx) |
||||||
|
return AVERROR(ENOMEM); |
||||||
|
|
||||||
|
ctx->av_class = &bsf_class; |
||||||
|
ctx->filter = filter; |
||||||
|
|
||||||
|
ctx->par_in = avcodec_parameters_alloc(); |
||||||
|
ctx->par_out = avcodec_parameters_alloc(); |
||||||
|
if (!ctx->par_in || !ctx->par_out) { |
||||||
|
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); |
||||||
|
if (!ctx->priv_data) { |
||||||
|
ret = AVERROR(ENOMEM); |
||||||
|
goto fail; |
||||||
|
} |
||||||
|
if (filter->priv_class) { |
||||||
|
*(const AVClass **)ctx->priv_data = filter->priv_class; |
||||||
|
av_opt_set_defaults(ctx->priv_data); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
*pctx = ctx; |
||||||
|
return 0; |
||||||
|
fail: |
||||||
|
av_bsf_free(&ctx); |
||||||
|
return ret; |
||||||
|
} |
||||||
|
|
||||||
|
int av_bsf_init(AVBSFContext *ctx) |
||||||
|
{ |
||||||
|
int ret, i; |
||||||
|
|
||||||
|
/* check that the codec is supported */ |
||||||
|
if (ctx->filter->codec_ids) { |
||||||
|
for (i = 0; ctx->filter->codec_ids[i] != AV_CODEC_ID_NONE; i++) |
||||||
|
if (ctx->par_in->codec_id == ctx->filter->codec_ids[i]) |
||||||
|
break; |
||||||
|
if (ctx->filter->codec_ids[i] == AV_CODEC_ID_NONE) { |
||||||
|
const AVCodecDescriptor *desc = avcodec_descriptor_get(ctx->par_in->codec_id); |
||||||
|
av_log(ctx, AV_LOG_ERROR, "Codec '%s' (%d) is not supported by the " |
||||||
|
"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]); |
||||||
|
av_log(ctx, AV_LOG_ERROR, "%s (%d) ", |
||||||
|
desc ? desc->name : "unknown", ctx->filter->codec_ids[i]); |
||||||
|
} |
||||||
|
av_log(ctx, AV_LOG_ERROR, "\n"); |
||||||
|
return AVERROR(EINVAL); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/* initialize output parameters to be the same as input
|
||||||
|
* init below might overwrite that */ |
||||||
|
ret = avcodec_parameters_copy(ctx->par_out, ctx->par_in); |
||||||
|
if (ret < 0) |
||||||
|
return ret; |
||||||
|
|
||||||
|
ctx->time_base_out = ctx->time_base_in; |
||||||
|
|
||||||
|
if (ctx->filter->init) { |
||||||
|
ret = ctx->filter->init(ctx); |
||||||
|
if (ret < 0) |
||||||
|
return ret; |
||||||
|
} |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt) |
||||||
|
{ |
||||||
|
if (!pkt || !pkt->data) { |
||||||
|
ctx->internal->eof = 1; |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
if (ctx->internal->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) |
||||||
|
return AVERROR(EAGAIN); |
||||||
|
|
||||||
|
av_packet_move_ref(ctx->internal->buffer_pkt, pkt); |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt) |
||||||
|
{ |
||||||
|
return ctx->filter->filter(ctx, pkt); |
||||||
|
} |
||||||
|
|
||||||
|
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt) |
||||||
|
{ |
||||||
|
AVBSFInternal *in = ctx->internal; |
||||||
|
AVPacket *tmp_pkt; |
||||||
|
|
||||||
|
if (in->eof) |
||||||
|
return AVERROR_EOF; |
||||||
|
|
||||||
|
if (!ctx->internal->buffer_pkt->data && |
||||||
|
!ctx->internal->buffer_pkt->side_data_elems) |
||||||
|
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; |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
@ -0,0 +1,33 @@ |
|||||||
|
/*
|
||||||
|
* This file is part of FFmpeg. |
||||||
|
* |
||||||
|
* FFmpeg is free software; you can redistribute it and/or |
||||||
|
* modify it under the terms of the GNU Lesser General Public |
||||||
|
* License as published by the Free Software Foundation; either |
||||||
|
* version 2.1 of the License, or (at your option) any later version. |
||||||
|
* |
||||||
|
* FFmpeg is distributed in the hope that it will be useful, |
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
||||||
|
* Lesser General Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU Lesser General Public |
||||||
|
* License along with FFmpeg; if not, write to the Free Software |
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef AVCODEC_BSF_H |
||||||
|
#define AVCODEC_BSF_H |
||||||
|
|
||||||
|
#include "avcodec.h" |
||||||
|
|
||||||
|
/**
|
||||||
|
* Called by the biststream filters to get the next packet for filtering. |
||||||
|
* The filter is responsible for either freeing the packet or passing it to the |
||||||
|
* caller. |
||||||
|
*/ |
||||||
|
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt); |
||||||
|
|
||||||
|
const AVClass *ff_bsf_child_class_next(const AVClass *prev); |
||||||
|
|
||||||
|
#endif /* AVCODEC_BSF_H */ |
Loading…
Reference in new issue