2016-11-13 10:00:02 +00:00
|
|
|
/*
|
2017-04-23 02:07:59 +00:00
|
|
|
* This file is part of FFmpeg.
|
2016-11-13 10:00:02 +00:00
|
|
|
*
|
2017-04-23 02:07:59 +00:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2016-11-13 10:00:02 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2017-04-23 02:07:59 +00:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2016-11-13 10:00:02 +00:00
|
|
|
* 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
|
2017-04-23 02:07:59 +00:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2016-11-13 10:00:02 +00:00
|
|
|
* 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"
|
2017-04-23 02:07:59 +00:00
|
|
|
#include "get_bits.h"
|
2016-11-13 10:00:02 +00:00
|
|
|
|
|
|
|
typedef struct VP9SFSplitContext {
|
2018-03-16 21:58:04 +00:00
|
|
|
AVPacket buffer_pkt;
|
2016-11-13 10:00:02 +00:00
|
|
|
|
|
|
|
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;
|
2018-03-16 21:58:04 +00:00
|
|
|
int is_superframe = !!s->buffer_pkt.data;
|
2016-11-13 10:00:02 +00:00
|
|
|
|
2018-03-16 21:58:04 +00:00
|
|
|
if (!s->buffer_pkt.data) {
|
|
|
|
ret = ff_bsf_get_packet_ref(ctx, &s->buffer_pkt);
|
2016-11-13 10:00:02 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2018-03-16 21:58:04 +00:00
|
|
|
in = &s->buffer_pkt;
|
2016-11-13 10:00:02 +00:00
|
|
|
|
|
|
|
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;
|
2017-12-06 21:42:05 +00:00
|
|
|
int64_t total_size = 0;
|
2016-11-13 10:00:02 +00:00
|
|
|
|
|
|
|
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;
|
2017-12-06 21:42:05 +00:00
|
|
|
if (frame_size < 0 || total_size > in->size - idx_size) {
|
2016-11-13 10:00:02 +00:00
|
|
|
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) {
|
2017-04-23 02:07:59 +00:00
|
|
|
GetBitContext gb;
|
2016-11-13 10:00:02 +00:00
|
|
|
int profile, invisible = 0;
|
|
|
|
|
2018-03-16 21:58:04 +00:00
|
|
|
ret = av_packet_ref(out, &s->buffer_pkt);
|
2016-11-13 10:00:02 +00:00
|
|
|
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)
|
2018-03-16 21:58:04 +00:00
|
|
|
av_packet_unref(&s->buffer_pkt);
|
2016-11-13 10:00:02 +00:00
|
|
|
|
2017-04-23 02:07:59 +00:00
|
|
|
ret = init_get_bits8(&gb, out->data, out->size);
|
2016-11-13 10:00:02 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2017-04-23 02:07:59 +00:00
|
|
|
get_bits(&gb, 2); // frame_marker
|
|
|
|
profile = get_bits1(&gb);
|
|
|
|
profile |= get_bits1(&gb) << 1;
|
2016-11-13 10:00:02 +00:00
|
|
|
if (profile == 3)
|
2017-04-23 02:07:59 +00:00
|
|
|
get_bits1(&gb);
|
|
|
|
if (!get_bits1(&gb)) {
|
|
|
|
get_bits1(&gb);
|
|
|
|
invisible = !get_bits1(&gb);
|
2016-11-13 10:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (invisible)
|
|
|
|
out->pts = AV_NOPTS_VALUE;
|
|
|
|
|
|
|
|
} else {
|
2018-03-16 21:58:04 +00:00
|
|
|
av_packet_move_ref(out, &s->buffer_pkt);
|
2016-11-13 10:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
fail:
|
2018-03-21 04:38:34 +00:00
|
|
|
if (ret < 0)
|
|
|
|
av_packet_unref(out);
|
2018-03-16 21:58:04 +00:00
|
|
|
av_packet_unref(&s->buffer_pkt);
|
2016-11-13 10:00:02 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vp9_superframe_split_uninit(AVBSFContext *ctx)
|
|
|
|
{
|
|
|
|
VP9SFSplitContext *s = ctx->priv_data;
|
2018-03-16 21:58:04 +00:00
|
|
|
av_packet_unref(&s->buffer_pkt);
|
2016-11-13 10:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 },
|
|
|
|
};
|