ffmpeg | branch: master | James Almer <[email protected]> | Thu Mar 23 16:31:15 2017 -0300| [7ebc9f8df4035ecaa84ad4429480986e3e7597ae] | committer: James Almer
Merge commit '89b35a139e838deeb32ec20d8d034c81014401d0' * commit '89b35a139e838deeb32ec20d8d034c81014401d0': lavc: add a bitstream filter for extracting extradata from packets Merged-by: James Almer <[email protected]> > http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=7ebc9f8df4035ecaa84ad4429480986e3e7597ae --- doc/bitstream_filters.texi | 18 +++ libavcodec/Makefile | 1 + libavcodec/bitstream_filters.c | 1 + libavcodec/extract_extradata_bsf.c | 300 +++++++++++++++++++++++++++++++++++++ libavcodec/version.h | 2 +- 5 files changed, 321 insertions(+), 1 deletion(-) diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi index e397ff9..d9d4a34 100644 --- a/doc/bitstream_filters.texi +++ b/doc/bitstream_filters.texi @@ -74,6 +74,24 @@ the header stored in extradata to the key packets: ffmpeg -i INPUT -map 0 -flags:v +global_header -c:v libx264 -bsf:v dump_extra out.ts @end example +@section extract_extradata + +Extract the in-band extradata. + +Certain codecs allow the long-term headers (e.g. MPEG-2 sequence headers, +or H.264/HEVC (VPS/)SPS/PPS) to be transmitted either "in-band" (i.e. as a part +of the bitstream containing the coded frames) or "out of band" (e.g. on the +container level). This latter form is called "extradata" in Libav terminology. + +This bitstream filter detects the in-band headers and makes them available as +extradata. + +@table @option +@item remove +When this option is enabled, the long-term headers are removed from the +bitstream after extraction. +@end table + @section h264_mp4toannexb Convert an H.264 bitstream from length prefixed mode to start code diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 3889ced..89296cd 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -971,6 +971,7 @@ OBJS-$(CONFIG_AAC_ADTSTOASC_BSF) += aac_adtstoasc_bsf.o aacadtsdec.o \ OBJS-$(CONFIG_CHOMP_BSF) += chomp_bsf.o OBJS-$(CONFIG_DUMP_EXTRADATA_BSF) += dump_extradata_bsf.o OBJS-$(CONFIG_DCA_CORE_BSF) += dca_core_bsf.o +OBJS-$(CONFIG_EXTRACT_EXTRADATA_BSF) += extract_extradata_bsf.o OBJS-$(CONFIG_H264_MP4TOANNEXB_BSF) += h264_mp4toannexb_bsf.o OBJS-$(CONFIG_HEVC_MP4TOANNEXB_BSF) += hevc_mp4toannexb_bsf.o OBJS-$(CONFIG_IMX_DUMP_HEADER_BSF) += imx_dump_header_bsf.o diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c index 840bb43..2045e18 100644 --- a/libavcodec/bitstream_filters.c +++ b/libavcodec/bitstream_filters.c @@ -28,6 +28,7 @@ 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_extract_extradata_bsf; extern const AVBitStreamFilter ff_h264_mp4toannexb_bsf; extern const AVBitStreamFilter ff_hevc_mp4toannexb_bsf; extern const AVBitStreamFilter ff_imx_dump_header_bsf; diff --git a/libavcodec/extract_extradata_bsf.c b/libavcodec/extract_extradata_bsf.c new file mode 100644 index 0000000..0d11f86 --- /dev/null +++ b/libavcodec/extract_extradata_bsf.c @@ -0,0 +1,300 @@ +/* + * 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 <stdint.h> + +#include "libavutil/common.h" +#include "libavutil/intreadwrite.h" +#include "libavutil/log.h" +#include "libavutil/opt.h" + +#include "avcodec.h" +#include "bsf.h" +#include "h2645_parse.h" +#include "h264.h" +#include "hevc.h" +#include "vc1_common.h" + +typedef struct ExtractExtradataContext { + const AVClass *class; + + int (*extract)(AVBSFContext *ctx, AVPacket *pkt, + uint8_t **data, int *size); + + /* AVOptions */ + int remove; +} ExtractExtradataContext; + +static int val_in_array(const int *arr, int len, int val) +{ + int i; + for (i = 0; i < len; i++) + if (arr[i] == val) + return 1; + return 0; +} + +static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt, + uint8_t **data, int *size) +{ + static const int extradata_nal_types_hevc[] = { + HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS, + }; + static const int extradata_nal_types_h264[] = { + H264_NAL_SPS, H264_NAL_PPS, + }; + + ExtractExtradataContext *s = ctx->priv_data; + + H2645Packet h2645_pkt = { 0 }; + int extradata_size = 0; + const int *extradata_nal_types; + int nb_extradata_nal_types; + int i, ret = 0; + + if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) { + extradata_nal_types = extradata_nal_types_hevc; + nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc); + } else { + extradata_nal_types = extradata_nal_types_h264; + nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264); + } + + ret = ff_h2645_packet_split(&h2645_pkt, pkt->data, pkt->size, + ctx, 0, 0, ctx->par_in->codec_id, 1); + if (ret < 0) + return ret; + + for (i = 0; i < h2645_pkt.nb_nals; i++) { + H2645NAL *nal = &h2645_pkt.nals[i]; + if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) + extradata_size += nal->raw_size + 3; + } + + if (extradata_size) { + AVBufferRef *filtered_buf; + uint8_t *extradata, *filtered_data; + + if (s->remove) { + filtered_buf = av_buffer_alloc(pkt->size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!filtered_buf) + goto fail; + filtered_data = filtered_buf->data; + } + + extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!extradata) { + av_buffer_unref(&filtered_buf); + goto fail; + } + + *data = extradata; + *size = extradata_size; + + for (i = 0; i < h2645_pkt.nb_nals; i++) { + H2645NAL *nal = &h2645_pkt.nals[i]; + if (val_in_array(extradata_nal_types, nb_extradata_nal_types, + nal->type)) { + AV_WB24(extradata, 1); // startcode + memcpy(extradata + 3, nal->raw_data, nal->raw_size); + extradata += 3 + nal->raw_size; + } else if (s->remove) { + AV_WB24(filtered_data, 1); // startcode + memcpy(filtered_data + 3, nal->raw_data, nal->raw_size); + filtered_data += 3 + nal->raw_size; + } + } + + if (s->remove) { + av_buffer_unref(&pkt->buf); + pkt->buf = filtered_buf; + pkt->data = filtered_buf->data; + pkt->size = filtered_data - filtered_buf->data; + } + } + +fail: + ff_h2645_packet_uninit(&h2645_pkt); + return ret; +} + +static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt, + uint8_t **data, int *size) +{ + ExtractExtradataContext *s = ctx->priv_data; + uint32_t state = UINT32_MAX; + int has_extradata = 0, extradata_size = 0; + int i; + + for (i = 0; i < pkt->size; i++) { + state = (state << 8) | pkt->data[i]; + if (IS_MARKER(state)) { + if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) { + has_extradata = 1; + } else if (has_extradata) { + extradata_size = i - 3; + break; + } + } + } + + if (extradata_size) { + *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!*data) + return AVERROR(ENOMEM); + + memcpy(*data, pkt->data, extradata_size); + *size = extradata_size; + + if (s->remove) { + pkt->data += extradata_size; + pkt->size -= extradata_size; + } + } + + return 0; +} + +static int extract_extradata_mpeg124(AVBSFContext *ctx, AVPacket *pkt, + uint8_t **data, int *size) +{ + ExtractExtradataContext *s = ctx->priv_data; + int is_mpeg12 = ctx->par_in->codec_id == AV_CODEC_ID_MPEG1VIDEO || + ctx->par_in->codec_id == AV_CODEC_ID_MPEG2VIDEO; + uint32_t state = UINT32_MAX; + int i; + + for (i = 0; i < pkt->size; i++) { + state = (state << 8) | pkt->data[i]; + if ((is_mpeg12 && state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100) || + (!is_mpeg12 && (state == 0x1B3 || state == 0x1B6))) { + if (i > 3) { + *size = i - 3; + *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!*data) + return AVERROR(ENOMEM); + + memcpy(*data, pkt->data, *size); + + if (s->remove) { + pkt->data += *size; + pkt->size -= *size; + } + } + break; + } + } + return 0; +} + +static const struct { + enum AVCodecID id; + int (*extract)(AVBSFContext *ctx, AVPacket *pkt, + uint8_t **data, int *size); +} extract_tab[] = { + { AV_CODEC_ID_CAVS, extract_extradata_mpeg124 }, + { AV_CODEC_ID_H264, extract_extradata_h2645 }, + { AV_CODEC_ID_HEVC, extract_extradata_h2645 }, + { AV_CODEC_ID_MPEG1VIDEO, extract_extradata_mpeg124 }, + { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg124 }, + { AV_CODEC_ID_MPEG4, extract_extradata_mpeg124 }, + { AV_CODEC_ID_VC1, extract_extradata_vc1 }, +}; + +static int extract_extradata_init(AVBSFContext *ctx) +{ + ExtractExtradataContext *s = ctx->priv_data; + int i; + + for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) { + if (extract_tab[i].id == ctx->par_in->codec_id) { + s->extract = extract_tab[i].extract; + break; + } + } + if (!s->extract) + return AVERROR_BUG; + + return 0; +} + +static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *out) +{ + ExtractExtradataContext *s = ctx->priv_data; + AVPacket *in; + uint8_t *extradata = NULL; + int extradata_size; + int ret = 0; + + ret = ff_bsf_get_packet(ctx, &in); + if (ret < 0) + return ret; + + ret = s->extract(ctx, in, &extradata, &extradata_size); + if (ret < 0) + goto fail; + + if (extradata) { + ret = av_packet_add_side_data(in, AV_PKT_DATA_NEW_EXTRADATA, + extradata, extradata_size); + if (ret < 0) { + av_freep(&extradata); + goto fail; + } + } + + av_packet_move_ref(out, in); + +fail: + av_packet_free(&in); + return ret; +} + +static const enum AVCodecID codec_ids[] = { + AV_CODEC_ID_CAVS, + AV_CODEC_ID_H264, + AV_CODEC_ID_HEVC, + AV_CODEC_ID_MPEG1VIDEO, + AV_CODEC_ID_MPEG2VIDEO, + AV_CODEC_ID_MPEG4, + AV_CODEC_ID_VC1, + AV_CODEC_ID_NONE, +}; + +#define OFFSET(x) offsetof(ExtractExtradataContext, x) +static const AVOption options[] = { + { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT, + { .i64 = 0 }, 0, 1 }, + { NULL }, +}; + +static const AVClass extract_extradata_class = { + .class_name = "extract_extradata", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +const AVBitStreamFilter ff_extract_extradata_bsf = { + .name = "extract_extradata", + .codec_ids = codec_ids, + .priv_data_size = sizeof(ExtractExtradataContext), + .priv_class = &extract_extradata_class, + .init = extract_extradata_init, + .filter = extract_extradata_filter, +}; diff --git a/libavcodec/version.h b/libavcodec/version.h index 5ca008f..4a384d6 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -28,7 +28,7 @@ #include "libavutil/version.h" #define LIBAVCODEC_VERSION_MAJOR 57 -#define LIBAVCODEC_VERSION_MINOR 85 +#define LIBAVCODEC_VERSION_MINOR 86 #define LIBAVCODEC_VERSION_MICRO 102 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ ====================================================================== diff --cc doc/bitstream_filters.texi index e397ff9,7ddf52e..d9d4a34 --- a/doc/bitstream_filters.texi +++ b/doc/bitstream_filters.texi @@@ -26,105 -17,31 +26,123 @@@ with their parameters, if any @section aac_adtstoasc +Convert MPEG-2/4 AAC ADTS to an MPEG-4 Audio Specific Configuration +bitstream. + +This filter creates an MPEG-4 AudioSpecificConfig from an MPEG-2/4 +ADTS header and removes the ADTS header. + +This filter is required for example when copying an AAC stream from a +raw ADTS AAC or an MPEG-TS container to MP4A-LATM, to an FLV file, or +to MOV/MP4 files and related formats such as 3GP or M4A. Please note +that it is auto-inserted for MP4A-LATM and MOV/MP4 and related formats. + @section chomp -@section dump_extradata +Remove zero padding at the end of a packet. + +@section dca_core + +Extract the core from a DCA/DTS stream, dropping extensions such as +DTS-HD. + +@section dump_extra + +Add extradata to the beginning of the filtered packets. + +The additional argument specifies which packets should be filtered. +It accepts the values: +@table @samp +@item a +add extradata to all key packets, but only if @var{local_header} is +set in the @option{flags2} codec context field + +@item k +add extradata to all key packets + +@item e +add extradata to all packets +@end table + +If not specified it is assumed @samp{k}. + +For example the following @command{ffmpeg} command forces a global +header (thus disabling individual packet headers) in the H.264 packets +generated by the @code{libx264} encoder, but corrects them by adding +the header stored in extradata to the key packets: +@example +ffmpeg -i INPUT -map 0 -flags:v +global_header -c:v libx264 -bsf:v dump_extra out.ts +@end example + @section extract_extradata + + Extract the in-band extradata. + + Certain codecs allow the long-term headers (e.g. MPEG-2 sequence headers, + or H.264/HEVC (VPS/)SPS/PPS) to be transmitted either "in-band" (i.e. as a part + of the bitstream containing the coded frames) or "out of band" (e.g. on the + container level). This latter form is called "extradata" in Libav terminology. + + This bitstream filter detects the in-band headers and makes them available as + extradata. + + @table @option + @item remove + When this option is enabled, the long-term headers are removed from the + bitstream after extraction. + @end table + @section h264_mp4toannexb -@section imx_dump_header +Convert an H.264 bitstream from length prefixed mode to start code +prefixed mode (as defined in the Annex B of the ITU-T H.264 +specification). + +This is required by some streaming formats, typically the MPEG-2 +transport stream format (muxer @code{mpegts}). + +For example to remux an MP4 file containing an H.264 stream to mpegts +format with @command{ffmpeg}, you can use the command: + +@example +ffmpeg -i INPUT.mp4 -codec copy -bsf:v h264_mp4toannexb OUTPUT.ts +@end example + +Please note that this filter is auto-inserted for MPEG-TS (muxer +@code{mpegts}) and raw H.264 (muxer @code{h264}) output formats. + +@section hevc_mp4toannexb + +Convert an HEVC/H.265 bitstream from length prefixed mode to start code +prefixed mode (as defined in the Annex B of the ITU-T H.265 +specification). + +This is required by some streaming formats, typically the MPEG-2 +transport stream format (muxer @code{mpegts}). + +For example to remux an MP4 file containing an HEVC stream to mpegts +format with @command{ffmpeg}, you can use the command: + +@example +ffmpeg -i INPUT.mp4 -codec copy -bsf:v hevc_mp4toannexb OUTPUT.ts +@end example + +Please note that this filter is auto-inserted for MPEG-TS (muxer +@code{mpegts}) and raw HEVC/H.265 (muxer @code{h265} or +@code{hevc}) output formats. + +@section imxdump + +Modifies the bitstream to fit in MOV and to be usable by the Final Cut +Pro decoder. This filter only applies to the mpeg2video codec, and is +likely not needed for Final Cut Pro 7 and newer with the appropriate +@option{-tag:v}. + +For example, to remux 30 MB/sec NTSC IMX to MOV: + +@example +ffmpeg -i input.mxf -c copy -bsf:v imxdump -tag:v mx3n output.mov +@end example @section mjpeg2jpeg diff --cc libavcodec/Makefile index 3889ced,66a6c66..89296cd --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@@ -970,7 -748,7 +970,8 @@@ OBJS-$(CONFIG_AAC_ADTSTOASC_BSF mpeg4audio.o OBJS-$(CONFIG_CHOMP_BSF) += chomp_bsf.o OBJS-$(CONFIG_DUMP_EXTRADATA_BSF) += dump_extradata_bsf.o +OBJS-$(CONFIG_DCA_CORE_BSF) += dca_core_bsf.o + OBJS-$(CONFIG_EXTRACT_EXTRADATA_BSF) += extract_extradata_bsf.o OBJS-$(CONFIG_H264_MP4TOANNEXB_BSF) += h264_mp4toannexb_bsf.o OBJS-$(CONFIG_HEVC_MP4TOANNEXB_BSF) += hevc_mp4toannexb_bsf.o OBJS-$(CONFIG_IMX_DUMP_HEADER_BSF) += imx_dump_header_bsf.o diff --cc libavcodec/bitstream_filters.c index 840bb43,8a5379e..2045e18 --- a/libavcodec/bitstream_filters.c +++ b/libavcodec/bitstream_filters.c @@@ -27,7 -27,7 +27,8 @@@ 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_extract_extradata_bsf; extern const AVBitStreamFilter ff_h264_mp4toannexb_bsf; extern const AVBitStreamFilter ff_hevc_mp4toannexb_bsf; extern const AVBitStreamFilter ff_imx_dump_header_bsf; diff --cc libavcodec/extract_extradata_bsf.c index 0000000,20b3080..0d11f86 mode 000000,100644..100644 --- a/libavcodec/extract_extradata_bsf.c +++ b/libavcodec/extract_extradata_bsf.c @@@ -1,0 -1,300 +1,300 @@@ + /* - * This file is part of Libav. ++ * This file is part of FFmpeg. + * - * Libav is free software; you can redistribute it and/or ++ * 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. + * - * Libav is distributed in the hope that it will be useful, ++ * 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 Libav; if not, write to the Free Software ++ * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + + #include <stdint.h> + + #include "libavutil/common.h" + #include "libavutil/intreadwrite.h" + #include "libavutil/log.h" + #include "libavutil/opt.h" + + #include "avcodec.h" + #include "bsf.h" + #include "h2645_parse.h" + #include "h264.h" + #include "hevc.h" + #include "vc1_common.h" + + typedef struct ExtractExtradataContext { + const AVClass *class; + + int (*extract)(AVBSFContext *ctx, AVPacket *pkt, + uint8_t **data, int *size); + + /* AVOptions */ + int remove; + } ExtractExtradataContext; + + static int val_in_array(const int *arr, int len, int val) + { + int i; + for (i = 0; i < len; i++) + if (arr[i] == val) + return 1; + return 0; + } + + static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt, + uint8_t **data, int *size) + { + static const int extradata_nal_types_hevc[] = { + HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS, + }; + static const int extradata_nal_types_h264[] = { + H264_NAL_SPS, H264_NAL_PPS, + }; + + ExtractExtradataContext *s = ctx->priv_data; + + H2645Packet h2645_pkt = { 0 }; + int extradata_size = 0; + const int *extradata_nal_types; + int nb_extradata_nal_types; + int i, ret = 0; + + if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) { + extradata_nal_types = extradata_nal_types_hevc; + nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc); + } else { + extradata_nal_types = extradata_nal_types_h264; + nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264); + } + + ret = ff_h2645_packet_split(&h2645_pkt, pkt->data, pkt->size, - ctx, 0, 0, ctx->par_in->codec_id); ++ ctx, 0, 0, ctx->par_in->codec_id, 1); + if (ret < 0) + return ret; + + for (i = 0; i < h2645_pkt.nb_nals; i++) { + H2645NAL *nal = &h2645_pkt.nals[i]; + if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) + extradata_size += nal->raw_size + 3; + } + + if (extradata_size) { + AVBufferRef *filtered_buf; + uint8_t *extradata, *filtered_data; + + if (s->remove) { + filtered_buf = av_buffer_alloc(pkt->size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!filtered_buf) + goto fail; + filtered_data = filtered_buf->data; + } + + extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!extradata) { + av_buffer_unref(&filtered_buf); + goto fail; + } + + *data = extradata; + *size = extradata_size; + + for (i = 0; i < h2645_pkt.nb_nals; i++) { + H2645NAL *nal = &h2645_pkt.nals[i]; + if (val_in_array(extradata_nal_types, nb_extradata_nal_types, + nal->type)) { + AV_WB24(extradata, 1); // startcode + memcpy(extradata + 3, nal->raw_data, nal->raw_size); + extradata += 3 + nal->raw_size; + } else if (s->remove) { + AV_WB24(filtered_data, 1); // startcode + memcpy(filtered_data + 3, nal->raw_data, nal->raw_size); + filtered_data += 3 + nal->raw_size; + } + } + + if (s->remove) { + av_buffer_unref(&pkt->buf); + pkt->buf = filtered_buf; + pkt->data = filtered_buf->data; + pkt->size = filtered_data - filtered_buf->data; + } + } + + fail: + ff_h2645_packet_uninit(&h2645_pkt); + return ret; + } + + static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt, + uint8_t **data, int *size) + { + ExtractExtradataContext *s = ctx->priv_data; + uint32_t state = UINT32_MAX; + int has_extradata = 0, extradata_size = 0; + int i; + + for (i = 0; i < pkt->size; i++) { + state = (state << 8) | pkt->data[i]; + if (IS_MARKER(state)) { + if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) { + has_extradata = 1; + } else if (has_extradata) { + extradata_size = i - 3; + break; + } + } + } + + if (extradata_size) { + *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!*data) + return AVERROR(ENOMEM); + + memcpy(*data, pkt->data, extradata_size); + *size = extradata_size; + + if (s->remove) { + pkt->data += extradata_size; + pkt->size -= extradata_size; + } + } + + return 0; + } + + static int extract_extradata_mpeg124(AVBSFContext *ctx, AVPacket *pkt, + uint8_t **data, int *size) + { + ExtractExtradataContext *s = ctx->priv_data; + int is_mpeg12 = ctx->par_in->codec_id == AV_CODEC_ID_MPEG1VIDEO || + ctx->par_in->codec_id == AV_CODEC_ID_MPEG2VIDEO; + uint32_t state = UINT32_MAX; + int i; + + for (i = 0; i < pkt->size; i++) { + state = (state << 8) | pkt->data[i]; + if ((is_mpeg12 && state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100) || + (!is_mpeg12 && (state == 0x1B3 || state == 0x1B6))) { + if (i > 3) { + *size = i - 3; + *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!*data) + return AVERROR(ENOMEM); + + memcpy(*data, pkt->data, *size); + + if (s->remove) { + pkt->data += *size; + pkt->size -= *size; + } + } + break; + } + } + return 0; + } + + static const struct { + enum AVCodecID id; + int (*extract)(AVBSFContext *ctx, AVPacket *pkt, + uint8_t **data, int *size); + } extract_tab[] = { + { AV_CODEC_ID_CAVS, extract_extradata_mpeg124 }, + { AV_CODEC_ID_H264, extract_extradata_h2645 }, + { AV_CODEC_ID_HEVC, extract_extradata_h2645 }, + { AV_CODEC_ID_MPEG1VIDEO, extract_extradata_mpeg124 }, + { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg124 }, + { AV_CODEC_ID_MPEG4, extract_extradata_mpeg124 }, + { AV_CODEC_ID_VC1, extract_extradata_vc1 }, + }; + + static int extract_extradata_init(AVBSFContext *ctx) + { + ExtractExtradataContext *s = ctx->priv_data; + int i; + + for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) { + if (extract_tab[i].id == ctx->par_in->codec_id) { + s->extract = extract_tab[i].extract; + break; + } + } + if (!s->extract) + return AVERROR_BUG; + + return 0; + } + + static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *out) + { + ExtractExtradataContext *s = ctx->priv_data; + AVPacket *in; + uint8_t *extradata = NULL; + int extradata_size; + int ret = 0; + + ret = ff_bsf_get_packet(ctx, &in); + if (ret < 0) + return ret; + + ret = s->extract(ctx, in, &extradata, &extradata_size); + if (ret < 0) + goto fail; + + if (extradata) { + ret = av_packet_add_side_data(in, AV_PKT_DATA_NEW_EXTRADATA, + extradata, extradata_size); + if (ret < 0) { + av_freep(&extradata); + goto fail; + } + } + + av_packet_move_ref(out, in); + + fail: + av_packet_free(&in); + return ret; + } + + static const enum AVCodecID codec_ids[] = { + AV_CODEC_ID_CAVS, + AV_CODEC_ID_H264, + AV_CODEC_ID_HEVC, + AV_CODEC_ID_MPEG1VIDEO, + AV_CODEC_ID_MPEG2VIDEO, + AV_CODEC_ID_MPEG4, + AV_CODEC_ID_VC1, + AV_CODEC_ID_NONE, + }; + + #define OFFSET(x) offsetof(ExtractExtradataContext, x) + static const AVOption options[] = { + { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT, + { .i64 = 0 }, 0, 1 }, + { NULL }, + }; + + static const AVClass extract_extradata_class = { + .class_name = "extract_extradata", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, + }; + + const AVBitStreamFilter ff_extract_extradata_bsf = { + .name = "extract_extradata", + .codec_ids = codec_ids, + .priv_data_size = sizeof(ExtractExtradataContext), + .priv_class = &extract_extradata_class, + .init = extract_extradata_init, + .filter = extract_extradata_filter, + }; diff --cc libavcodec/version.h index 5ca008f,64b0ee6..4a384d6 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@@ -27,9 -27,9 +27,9 @@@ #include "libavutil/version.h" -#define LIBAVCODEC_VERSION_MAJOR 57 -#define LIBAVCODEC_VERSION_MINOR 28 -#define LIBAVCODEC_VERSION_MICRO 0 +#define LIBAVCODEC_VERSION_MAJOR 57 - #define LIBAVCODEC_VERSION_MINOR 85 ++#define LIBAVCODEC_VERSION_MINOR 86 +#define LIBAVCODEC_VERSION_MICRO 102 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \ _______________________________________________ ffmpeg-cvslog mailing list [email protected] http://ffmpeg.org/mailman/listinfo/ffmpeg-cvslog
