2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * This bitstream filter splits VP9 superframes into packets containing
29 #include "bytestream.h"
32 typedef struct VP9SFSplitContext {
37 size_t next_frame_offset;
41 static int vp9_superframe_split_filter(AVBSFContext *ctx, AVPacket *out)
43 VP9SFSplitContext *s = ctx->priv_data;
45 int i, j, ret, marker;
46 int is_superframe = !!s->buffer_pkt->data;
48 if (!s->buffer_pkt->data) {
49 ret = ff_bsf_get_packet_ref(ctx, s->buffer_pkt);
54 marker = in->data[in->size - 1];
55 if ((marker & 0xe0) == 0xc0) {
56 int length_size = 1 + ((marker >> 3) & 0x3);
57 int nb_frames = 1 + (marker & 0x7);
58 int idx_size = 2 + nb_frames * length_size;
60 if (in->size >= idx_size && in->data[in->size - idx_size] == marker) {
62 int64_t total_size = 0;
64 bytestream2_init(&bc, in->data + in->size + 1 - idx_size,
65 nb_frames * length_size);
67 for (i = 0; i < nb_frames; i++) {
69 for (j = 0; j < length_size; j++)
70 frame_size |= bytestream2_get_byte(&bc) << (j * 8);
72 total_size += frame_size;
73 if (frame_size < 0 || total_size > in->size - idx_size) {
74 av_log(ctx, AV_LOG_ERROR,
75 "Invalid frame size in a superframe: %d\n", frame_size);
76 ret = AVERROR(EINVAL);
79 s->sizes[i] = frame_size;
81 s->nb_frames = nb_frames;
83 s->next_frame_offset = 0;
91 int profile, invisible = 0;
93 ret = av_packet_ref(out, s->buffer_pkt);
97 out->data += s->next_frame_offset;
98 out->size = s->sizes[s->next_frame];
100 s->next_frame_offset += out->size;
103 if (s->next_frame >= s->nb_frames)
104 av_packet_unref(s->buffer_pkt);
106 ret = init_get_bits8(&gb, out->data, out->size);
110 get_bits(&gb, 2); // frame_marker
111 profile = get_bits1(&gb);
112 profile |= get_bits1(&gb) << 1;
115 if (!get_bits1(&gb)) {
117 invisible = !get_bits1(&gb);
121 out->pts = AV_NOPTS_VALUE;
124 av_packet_move_ref(out, s->buffer_pkt);
130 av_packet_unref(out);
131 av_packet_unref(s->buffer_pkt);
135 static int vp9_superframe_split_init(AVBSFContext *ctx)
137 VP9SFSplitContext *s = ctx->priv_data;
139 s->buffer_pkt = av_packet_alloc();
141 return AVERROR(ENOMEM);
146 static void vp9_superframe_split_flush(AVBSFContext *ctx)
148 VP9SFSplitContext *s = ctx->priv_data;
149 av_packet_unref(s->buffer_pkt);
152 static void vp9_superframe_split_uninit(AVBSFContext *ctx)
154 VP9SFSplitContext *s = ctx->priv_data;
155 av_packet_free(&s->buffer_pkt);
158 const AVBitStreamFilter ff_vp9_superframe_split_bsf = {
159 .name = "vp9_superframe_split",
160 .priv_data_size = sizeof(VP9SFSplitContext),
161 .init = vp9_superframe_split_init,
162 .flush = vp9_superframe_split_flush,
163 .close = vp9_superframe_split_uninit,
164 .filter = vp9_superframe_split_filter,
165 .codec_ids = (const enum AVCodecID []){ AV_CODEC_ID_VP9, AV_CODEC_ID_NONE },