ffmpeg | branch: master | James Almer <jamr...@gmail.com> | Sat Apr 22 23:07:59 2017 -0300| [fdeab95a823b10adda6f29d134122537c21578e2] | committer: James Almer
Merge commit '03a80925effc2698d21dc0b00290eecf42dd9e68' * commit '03a80925effc2698d21dc0b00290eecf42dd9e68': lavc: add a bitstream filter for splitting VP9 superframes Merged-by: James Almer <jamr...@gmail.com> > http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=fdeab95a823b10adda6f29d134122537c21578e2 --- doc/bitstream_filters.texi | 4 + libavcodec/Makefile | 1 + libavcodec/bitstream_filters.c | 1 + libavcodec/vp9_superframe_split_bsf.c | 146 ++++++++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+) diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi index 6f4846f55f..d394a12db1 100644 --- a/doc/bitstream_filters.texi +++ b/doc/bitstream_filters.texi @@ -273,4 +273,8 @@ Merge VP9 invisible (alt-ref) frames back into VP9 superframes. This fixes merging of split/segmented VP9 streams where the alt-ref frame was split from its visible counterpart. +@section vp9_superframe_split + +Split VP9 superframes into single frames. + @c man end BITSTREAM FILTERS diff --git a/libavcodec/Makefile b/libavcodec/Makefile index 3275654a45..21cd81c6b2 100644 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@ -999,6 +999,7 @@ OBJS-$(CONFIG_NULL_BSF) += null_bsf.o OBJS-$(CONFIG_REMOVE_EXTRADATA_BSF) += remove_extradata_bsf.o OBJS-$(CONFIG_TEXT2MOVSUB_BSF) += movsub_bsf.o OBJS-$(CONFIG_VP9_SUPERFRAME_BSF) += vp9_superframe_bsf.o +OBJS-$(CONFIG_VP9_SUPERFRAME_SPLIT_BSF) += vp9_superframe_split_bsf.o # thread libraries OBJS-$(HAVE_LIBC_MSVCRT) += file_open.o diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c index 6893a4b6e4..1f6dd75d09 100644 --- a/libavcodec/bitstream_filters.c +++ b/libavcodec/bitstream_filters.c @@ -42,6 +42,7 @@ extern const AVBitStreamFilter ff_null_bsf; extern const AVBitStreamFilter ff_remove_extradata_bsf; extern const AVBitStreamFilter ff_text2movsub_bsf; extern const AVBitStreamFilter ff_vp9_superframe_bsf; +extern const AVBitStreamFilter ff_vp9_superframe_split_bsf; #include "libavcodec/bsf_list.c" diff --git a/libavcodec/vp9_superframe_split_bsf.c b/libavcodec/vp9_superframe_split_bsf.c new file mode 100644 index 0000000000..6d6d8e664d --- /dev/null +++ b/libavcodec/vp9_superframe_split_bsf.c @@ -0,0 +1,146 @@ +/* + * 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 + */ + +/** + * @file + * This bitstream filter splits VP9 superframes into packets containing + * just one frame. + */ + +#include <stddef.h> + +#include "avcodec.h" +#include "bsf.h" +#include "bytestream.h" +#include "get_bits.h" + +typedef struct VP9SFSplitContext { + AVPacket *buffer_pkt; + + int nb_frames; + int next_frame; + size_t next_frame_offset; + int sizes[8]; +} VP9SFSplitContext; + +static int vp9_superframe_split_filter(AVBSFContext *ctx, AVPacket *out) +{ + VP9SFSplitContext *s = ctx->priv_data; + AVPacket *in; + int i, j, ret, marker; + int is_superframe = !!s->buffer_pkt; + + if (!s->buffer_pkt) { + ret = ff_bsf_get_packet(ctx, &s->buffer_pkt); + if (ret < 0) + return ret; + in = s->buffer_pkt; + + marker = in->data[in->size - 1]; + if ((marker & 0xe0) == 0xc0) { + int length_size = 1 + ((marker >> 3) & 0x3); + int nb_frames = 1 + (marker & 0x7); + int idx_size = 2 + nb_frames * length_size; + + if (in->size >= idx_size && in->data[in->size - idx_size] == marker) { + GetByteContext bc; + int total_size = 0; + + bytestream2_init(&bc, in->data + in->size + 1 - idx_size, + nb_frames * length_size); + + for (i = 0; i < nb_frames; i++) { + int frame_size = 0; + for (j = 0; j < length_size; j++) + frame_size |= bytestream2_get_byte(&bc) << (j * 8); + + total_size += frame_size; + if (total_size > in->size - idx_size) { + av_log(ctx, AV_LOG_ERROR, + "Invalid frame size in a superframe: %d\n", frame_size); + ret = AVERROR(EINVAL); + goto fail; + } + s->sizes[i] = frame_size; + } + s->nb_frames = nb_frames; + s->next_frame = 0; + s->next_frame_offset = 0; + is_superframe = 1; + } + } + } + + if (is_superframe) { + GetBitContext gb; + int profile, invisible = 0; + + ret = av_packet_ref(out, s->buffer_pkt); + if (ret < 0) + goto fail; + + out->data += s->next_frame_offset; + out->size = s->sizes[s->next_frame]; + + s->next_frame_offset += out->size; + s->next_frame++; + + if (s->next_frame >= s->nb_frames) + av_packet_free(&s->buffer_pkt); + + ret = init_get_bits8(&gb, out->data, out->size); + if (ret < 0) + goto fail; + + get_bits(&gb, 2); // frame_marker + profile = get_bits1(&gb); + profile |= get_bits1(&gb) << 1; + if (profile == 3) + get_bits1(&gb); + if (!get_bits1(&gb)) { + get_bits1(&gb); + invisible = !get_bits1(&gb); + } + + if (invisible) + out->pts = AV_NOPTS_VALUE; + + } else { + av_packet_move_ref(out, s->buffer_pkt); + av_packet_free(&s->buffer_pkt); + } + + return 0; +fail: + av_packet_free(&s->buffer_pkt); + return ret; +} + +static void vp9_superframe_split_uninit(AVBSFContext *ctx) +{ + VP9SFSplitContext *s = ctx->priv_data; + av_packet_free(&s->buffer_pkt); +} + +const AVBitStreamFilter ff_vp9_superframe_split_bsf = { + .name = "vp9_superframe_split", + .priv_data_size = sizeof(VP9SFSplitContext), + .close = vp9_superframe_split_uninit, + .filter = vp9_superframe_split_filter, + .codec_ids = (const enum AVCodecID []){ AV_CODEC_ID_VP9, AV_CODEC_ID_NONE }, +}; ====================================================================== diff --cc doc/bitstream_filters.texi index 6f4846f55f,49b8a645d0..d394a12db1 --- a/doc/bitstream_filters.texi +++ b/doc/bitstream_filters.texi @@@ -237,40 -93,10 +237,44 @@@ applies the modification to every byte @section null This bitstream filter passes the packets through unchanged. -@section remove_extradata +@section remove_extra + +Remove extradata from packets. + +It accepts the following parameter: +@table @option +@item freq +Set which frame types to remove extradata from. + +@table @samp +@item k +Remove extradata from non-keyframes only. + +@item keyframe +Remove extradata from keyframes only. + +@item e, all +Remove extradata from all frames. + +@end table +@end table + +@anchor{text2movsub} +@section text2movsub + +Convert text subtitles to MOV subtitles (as used by the @code{mov_text} +codec) with metadata headers. + +See also the @ref{mov2textsub} filter. + +@section vp9_superframe + +Merge VP9 invisible (alt-ref) frames back into VP9 superframes. This +fixes merging of split/segmented VP9 streams where the alt-ref frame +was split from its visible counterpart. + @section vp9_superframe_split + + Split VP9 superframes into single frames. + @c man end BITSTREAM FILTERS diff --cc libavcodec/Makefile index 3275654a45,f07253a84d..21cd81c6b2 --- a/libavcodec/Makefile +++ b/libavcodec/Makefile @@@ -998,7 -767,7 +998,8 @@@ OBJS-$(CONFIG_NOISE_BSF OBJS-$(CONFIG_NULL_BSF) += null_bsf.o OBJS-$(CONFIG_REMOVE_EXTRADATA_BSF) += remove_extradata_bsf.o OBJS-$(CONFIG_TEXT2MOVSUB_BSF) += movsub_bsf.o +OBJS-$(CONFIG_VP9_SUPERFRAME_BSF) += vp9_superframe_bsf.o + OBJS-$(CONFIG_VP9_SUPERFRAME_SPLIT_BSF) += vp9_superframe_split_bsf.o # thread libraries OBJS-$(HAVE_LIBC_MSVCRT) += file_open.o diff --cc libavcodec/bitstream_filters.c index 6893a4b6e4,1cea6d77af..1f6dd75d09 --- a/libavcodec/bitstream_filters.c +++ b/libavcodec/bitstream_filters.c @@@ -34,14 -33,12 +34,15 @@@ extern const AVBitStreamFilter ff_hevc_ 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_null_bsf; -extern const AVBitStreamFilter ff_text2movsub_bsf; extern const AVBitStreamFilter ff_noise_bsf; +extern const AVBitStreamFilter ff_null_bsf; extern const AVBitStreamFilter ff_remove_extradata_bsf; +extern const AVBitStreamFilter ff_text2movsub_bsf; +extern const AVBitStreamFilter ff_vp9_superframe_bsf; + extern const AVBitStreamFilter ff_vp9_superframe_split_bsf; #include "libavcodec/bsf_list.c" diff --cc libavcodec/vp9_superframe_split_bsf.c index 0000000000,4e635a3070..6d6d8e664d mode 000000,100644..100644 --- a/libavcodec/vp9_superframe_split_bsf.c +++ b/libavcodec/vp9_superframe_split_bsf.c @@@ -1,0 -1,146 +1,146 @@@ + /* - * 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 + */ + + /** + * @file + * This bitstream filter splits VP9 superframes into packets containing + * just one frame. + */ + + #include <stddef.h> + + #include "avcodec.h" + #include "bsf.h" -#include "bitstream.h" + #include "bytestream.h" ++#include "get_bits.h" + + typedef struct VP9SFSplitContext { + AVPacket *buffer_pkt; + + int nb_frames; + int next_frame; + size_t next_frame_offset; + int sizes[8]; + } VP9SFSplitContext; + + static int vp9_superframe_split_filter(AVBSFContext *ctx, AVPacket *out) + { + VP9SFSplitContext *s = ctx->priv_data; + AVPacket *in; + int i, j, ret, marker; + int is_superframe = !!s->buffer_pkt; + + if (!s->buffer_pkt) { + ret = ff_bsf_get_packet(ctx, &s->buffer_pkt); + if (ret < 0) + return ret; + in = s->buffer_pkt; + + marker = in->data[in->size - 1]; + if ((marker & 0xe0) == 0xc0) { + int length_size = 1 + ((marker >> 3) & 0x3); + int nb_frames = 1 + (marker & 0x7); + int idx_size = 2 + nb_frames * length_size; + + if (in->size >= idx_size && in->data[in->size - idx_size] == marker) { + GetByteContext bc; + int total_size = 0; + + bytestream2_init(&bc, in->data + in->size + 1 - idx_size, + nb_frames * length_size); + + for (i = 0; i < nb_frames; i++) { + int frame_size = 0; + for (j = 0; j < length_size; j++) + frame_size |= bytestream2_get_byte(&bc) << (j * 8); + + total_size += frame_size; + if (total_size > in->size - idx_size) { + av_log(ctx, AV_LOG_ERROR, + "Invalid frame size in a superframe: %d\n", frame_size); + ret = AVERROR(EINVAL); + goto fail; + } + s->sizes[i] = frame_size; + } + s->nb_frames = nb_frames; + s->next_frame = 0; + s->next_frame_offset = 0; + is_superframe = 1; + } + } + } + + if (is_superframe) { - BitstreamContext bc; ++ GetBitContext gb; + int profile, invisible = 0; + + ret = av_packet_ref(out, s->buffer_pkt); + if (ret < 0) + goto fail; + + out->data += s->next_frame_offset; + out->size = s->sizes[s->next_frame]; + + s->next_frame_offset += out->size; + s->next_frame++; + + if (s->next_frame >= s->nb_frames) + av_packet_free(&s->buffer_pkt); + - ret = bitstream_init8(&bc, out->data, out->size); ++ ret = init_get_bits8(&gb, out->data, out->size); + if (ret < 0) + goto fail; + - bitstream_read(&bc, 2); // frame_marker - profile = bitstream_read(&bc, 1); - profile |= bitstream_read(&bc, 1) << 1; ++ get_bits(&gb, 2); // frame_marker ++ profile = get_bits1(&gb); ++ profile |= get_bits1(&gb) << 1; + if (profile == 3) - bitstream_read(&bc, 1); - if (!bitstream_read(&bc, 1)) { - bitstream_read(&bc, 1); - invisible = !bitstream_read(&bc, 1); ++ get_bits1(&gb); ++ if (!get_bits1(&gb)) { ++ get_bits1(&gb); ++ invisible = !get_bits1(&gb); + } + + if (invisible) + out->pts = AV_NOPTS_VALUE; + + } else { + av_packet_move_ref(out, s->buffer_pkt); + av_packet_free(&s->buffer_pkt); + } + + return 0; + fail: + av_packet_free(&s->buffer_pkt); + return ret; + } + + static void vp9_superframe_split_uninit(AVBSFContext *ctx) + { + VP9SFSplitContext *s = ctx->priv_data; + av_packet_free(&s->buffer_pkt); + } + + const AVBitStreamFilter ff_vp9_superframe_split_bsf = { + .name = "vp9_superframe_split", + .priv_data_size = sizeof(VP9SFSplitContext), + .close = vp9_superframe_split_uninit, + .filter = vp9_superframe_split_filter, + .codec_ids = (const enum AVCodecID []){ AV_CODEC_ID_VP9, AV_CODEC_ID_NONE }, + }; _______________________________________________ ffmpeg-cvslog mailing list ffmpeg-cvslog@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-cvslog