2016-02-29 14:43:07 +00:00
|
|
|
/*
|
|
|
|
* Vp9 invisible (alt-ref) frame to superframe merge bitstream filter
|
|
|
|
* Copyright (c) 2016 Ronald S. Bultje <rsbultje@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 "libavutil/avassert.h"
|
|
|
|
#include "avcodec.h"
|
2016-04-17 17:47:25 +00:00
|
|
|
#include "bsf.h"
|
2016-02-29 14:43:07 +00:00
|
|
|
#include "get_bits.h"
|
|
|
|
|
|
|
|
#define MAX_CACHE 8
|
|
|
|
typedef struct VP9BSFContext {
|
|
|
|
int n_cache;
|
2017-04-26 07:42:32 +00:00
|
|
|
AVPacket *cache[MAX_CACHE];
|
2016-02-29 14:43:07 +00:00
|
|
|
} VP9BSFContext;
|
|
|
|
|
2017-04-26 07:42:32 +00:00
|
|
|
static void stats(AVPacket * const *in, int n_in,
|
2016-02-29 14:43:07 +00:00
|
|
|
unsigned *_max, unsigned *_sum)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
unsigned max = 0, sum = 0;
|
|
|
|
|
|
|
|
for (n = 0; n < n_in; n++) {
|
2017-04-26 07:42:32 +00:00
|
|
|
unsigned sz = in[n]->size;
|
2016-02-29 14:43:07 +00:00
|
|
|
|
|
|
|
if (sz > max)
|
|
|
|
max = sz;
|
|
|
|
sum += sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
*_max = max;
|
|
|
|
*_sum = sum;
|
|
|
|
}
|
|
|
|
|
2017-04-26 07:42:32 +00:00
|
|
|
static int merge_superframe(AVPacket * const *in, int n_in, AVPacket *out)
|
2016-02-29 14:43:07 +00:00
|
|
|
{
|
|
|
|
unsigned max, sum, mag, marker, n, sz;
|
|
|
|
uint8_t *ptr;
|
2016-04-17 17:47:25 +00:00
|
|
|
int res;
|
2016-02-29 14:43:07 +00:00
|
|
|
|
|
|
|
stats(in, n_in, &max, &sum);
|
|
|
|
mag = av_log2(max) >> 3;
|
|
|
|
marker = 0xC0 + (mag << 3) + (n_in - 1);
|
2016-04-17 17:47:25 +00:00
|
|
|
sz = sum + 2 + (mag + 1) * n_in;
|
|
|
|
res = av_new_packet(out, sz);
|
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
ptr = out->data;
|
2016-02-29 14:43:07 +00:00
|
|
|
for (n = 0; n < n_in; n++) {
|
2017-04-26 07:42:32 +00:00
|
|
|
memcpy(ptr, in[n]->data, in[n]->size);
|
|
|
|
ptr += in[n]->size;
|
2016-02-29 14:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define wloop(mag, wr) \
|
2017-10-26 17:38:14 +00:00
|
|
|
do { \
|
2016-02-29 14:43:07 +00:00
|
|
|
for (n = 0; n < n_in; n++) { \
|
|
|
|
wr; \
|
|
|
|
ptr += mag + 1; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2016-02-29 14:43:07 +00:00
|
|
|
|
|
|
|
// write superframe with marker 110[mag:2][nframes:3]
|
|
|
|
*ptr++ = marker;
|
|
|
|
switch (mag) {
|
|
|
|
case 0:
|
2017-04-26 07:42:32 +00:00
|
|
|
wloop(mag, *ptr = in[n]->size);
|
2016-02-29 14:43:07 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2017-04-26 07:42:32 +00:00
|
|
|
wloop(mag, AV_WL16(ptr, in[n]->size));
|
2016-02-29 14:43:07 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2017-04-26 07:42:32 +00:00
|
|
|
wloop(mag, AV_WL24(ptr, in[n]->size));
|
2016-02-29 14:43:07 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2017-04-26 07:42:32 +00:00
|
|
|
wloop(mag, AV_WL32(ptr, in[n]->size));
|
2016-02-29 14:43:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
*ptr++ = marker;
|
2016-04-17 17:47:25 +00:00
|
|
|
av_assert0(ptr == &out->data[out->size]);
|
2016-02-29 14:43:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-17 17:47:25 +00:00
|
|
|
static int vp9_superframe_filter(AVBSFContext *ctx, AVPacket *out)
|
2016-02-29 14:43:07 +00:00
|
|
|
{
|
|
|
|
GetBitContext gb;
|
2016-04-17 17:47:25 +00:00
|
|
|
VP9BSFContext *s = ctx->priv_data;
|
|
|
|
AVPacket *in;
|
2016-02-29 14:43:07 +00:00
|
|
|
int res, invisible, profile, marker, uses_superframe_syntax = 0, n;
|
|
|
|
|
2016-04-17 17:47:25 +00:00
|
|
|
res = ff_bsf_get_packet(ctx, &in);
|
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
marker = in->data[in->size - 1];
|
2016-02-29 14:43:07 +00:00
|
|
|
if ((marker & 0xe0) == 0xc0) {
|
|
|
|
int nbytes = 1 + ((marker >> 3) & 0x3);
|
|
|
|
int n_frames = 1 + (marker & 0x7), idx_sz = 2 + n_frames * nbytes;
|
|
|
|
|
2016-04-17 17:47:25 +00:00
|
|
|
uses_superframe_syntax = in->size >= idx_sz && in->data[in->size - idx_sz] == marker;
|
2016-02-29 14:43:07 +00:00
|
|
|
}
|
|
|
|
|
2016-04-17 17:47:25 +00:00
|
|
|
if ((res = init_get_bits8(&gb, in->data, in->size)) < 0)
|
|
|
|
goto done;
|
2016-02-29 14:43:07 +00:00
|
|
|
|
|
|
|
get_bits(&gb, 2); // frame marker
|
|
|
|
profile = get_bits1(&gb);
|
|
|
|
profile |= get_bits1(&gb) << 1;
|
|
|
|
if (profile == 3) profile += get_bits1(&gb);
|
|
|
|
|
|
|
|
if (get_bits1(&gb)) {
|
|
|
|
invisible = 0;
|
|
|
|
} else {
|
|
|
|
get_bits1(&gb); // keyframe
|
|
|
|
invisible = !get_bits1(&gb);
|
|
|
|
}
|
|
|
|
|
2016-04-17 17:47:25 +00:00
|
|
|
if (uses_superframe_syntax && s->n_cache > 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
2016-02-29 14:43:07 +00:00
|
|
|
"Mixing of superframe syntax and naked VP9 frames not supported");
|
2016-02-29 14:43:07 +00:00
|
|
|
res = AVERROR(ENOSYS);
|
2016-04-17 17:47:25 +00:00
|
|
|
goto done;
|
|
|
|
} else if ((!invisible || uses_superframe_syntax) && !s->n_cache) {
|
2016-02-29 14:43:07 +00:00
|
|
|
// passthrough
|
2016-04-17 17:47:25 +00:00
|
|
|
av_packet_move_ref(out, in);
|
|
|
|
goto done;
|
|
|
|
} else if (s->n_cache + 1 >= MAX_CACHE) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR,
|
2016-02-29 14:43:07 +00:00
|
|
|
"Too many invisible frames");
|
2016-04-17 17:47:25 +00:00
|
|
|
res = AVERROR_INVALIDDATA;
|
|
|
|
goto done;
|
2016-02-29 14:43:07 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 01:19:42 +00:00
|
|
|
av_packet_move_ref(s->cache[s->n_cache++], in);
|
2017-10-31 19:18:32 +00:00
|
|
|
|
2016-02-29 14:43:07 +00:00
|
|
|
if (invisible) {
|
2016-04-17 17:47:25 +00:00
|
|
|
res = AVERROR(EAGAIN);
|
|
|
|
goto done;
|
2016-02-29 14:43:07 +00:00
|
|
|
}
|
2016-04-17 17:47:25 +00:00
|
|
|
av_assert0(s->n_cache > 0);
|
2016-02-29 14:43:07 +00:00
|
|
|
|
|
|
|
// build superframe
|
2017-04-26 07:42:32 +00:00
|
|
|
if ((res = merge_superframe(s->cache, s->n_cache, out)) < 0)
|
2016-04-17 17:47:25 +00:00
|
|
|
goto done;
|
|
|
|
|
2017-04-26 07:42:32 +00:00
|
|
|
res = av_packet_copy_props(out, s->cache[s->n_cache - 1]);
|
2016-04-17 17:47:25 +00:00
|
|
|
if (res < 0)
|
|
|
|
goto done;
|
|
|
|
|
2017-04-26 07:42:32 +00:00
|
|
|
for (n = 0; n < s->n_cache; n++)
|
2017-10-31 19:18:32 +00:00
|
|
|
av_packet_unref(s->cache[n]);
|
2017-04-26 07:42:32 +00:00
|
|
|
s->n_cache = 0;
|
|
|
|
|
2016-04-17 17:47:25 +00:00
|
|
|
done:
|
|
|
|
if (res < 0)
|
|
|
|
av_packet_unref(out);
|
|
|
|
av_packet_free(&in);
|
|
|
|
return res;
|
2016-02-29 14:43:07 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 19:18:32 +00:00
|
|
|
static int vp9_superframe_init(AVBSFContext *ctx)
|
|
|
|
{
|
|
|
|
VP9BSFContext *s = ctx->priv_data;
|
|
|
|
int n;
|
|
|
|
|
2017-11-05 16:35:40 +00:00
|
|
|
// alloc cache packets
|
2017-10-31 19:18:32 +00:00
|
|
|
for (n = 0; n < MAX_CACHE; n++) {
|
|
|
|
s->cache[n] = av_packet_alloc();
|
|
|
|
if (!s->cache[n])
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-17 17:47:25 +00:00
|
|
|
static void vp9_superframe_close(AVBSFContext *ctx)
|
2016-02-29 14:43:07 +00:00
|
|
|
{
|
2016-04-17 17:47:25 +00:00
|
|
|
VP9BSFContext *s = ctx->priv_data;
|
2016-02-29 14:43:07 +00:00
|
|
|
int n;
|
|
|
|
|
|
|
|
// free cached data
|
2017-10-31 19:18:32 +00:00
|
|
|
for (n = 0; n < MAX_CACHE; n++)
|
2017-04-26 07:42:32 +00:00
|
|
|
av_packet_free(&s->cache[n]);
|
2016-02-29 14:43:07 +00:00
|
|
|
}
|
|
|
|
|
2016-04-17 17:47:25 +00:00
|
|
|
static const enum AVCodecID codec_ids[] = {
|
|
|
|
AV_CODEC_ID_VP9, AV_CODEC_ID_NONE,
|
|
|
|
};
|
|
|
|
|
|
|
|
const AVBitStreamFilter ff_vp9_superframe_bsf = {
|
2016-02-29 14:43:07 +00:00
|
|
|
.name = "vp9_superframe",
|
|
|
|
.priv_data_size = sizeof(VP9BSFContext),
|
|
|
|
.filter = vp9_superframe_filter,
|
2017-10-31 19:18:32 +00:00
|
|
|
.init = vp9_superframe_init,
|
2016-02-29 14:43:07 +00:00
|
|
|
.close = vp9_superframe_close,
|
2016-04-17 17:47:25 +00:00
|
|
|
.codec_ids = codec_ids,
|
2016-02-29 14:43:07 +00:00
|
|
|
};
|