2015-04-03 18:19:53 +02:00
|
|
|
/*
|
|
|
|
* Bitstream filter for unpacking DivX-style packed B-frames in MPEG-4 (divx_packed)
|
|
|
|
* Copyright (c) 2015 Andreas Cadhalpun <Andreas.Cadhalpun@googlemail.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 "avcodec.h"
|
2016-04-17 19:47:25 +02:00
|
|
|
#include "bsf.h"
|
2019-02-02 21:34:11 +02:00
|
|
|
#include "internal.h"
|
2015-04-03 18:19:53 +02:00
|
|
|
#include "mpeg4video.h"
|
|
|
|
|
|
|
|
typedef struct UnpackBFramesBSFContext {
|
2018-03-21 02:59:10 +02:00
|
|
|
AVPacket *b_frame;
|
2015-04-03 18:19:53 +02:00
|
|
|
} UnpackBFramesBSFContext;
|
|
|
|
|
|
|
|
/* determine the position of the packed marker in the userdata,
|
|
|
|
* the number of VOPs and the position of the second VOP */
|
|
|
|
static void scan_buffer(const uint8_t *buf, int buf_size,
|
|
|
|
int *pos_p, int *nb_vop, int *pos_vop2) {
|
2019-02-02 21:34:11 +02:00
|
|
|
uint32_t startcode;
|
|
|
|
const uint8_t *end = buf + buf_size, *pos = buf;
|
2015-04-03 18:19:53 +02:00
|
|
|
|
2019-02-02 21:34:11 +02:00
|
|
|
while (pos < end) {
|
|
|
|
startcode = -1;
|
|
|
|
pos = avpriv_find_start_code(pos, end, &startcode);
|
2015-04-03 18:19:53 +02:00
|
|
|
|
|
|
|
if (startcode == USER_DATA_STARTCODE && pos_p) {
|
|
|
|
/* check if the (DivX) userdata string ends with 'p' (packed) */
|
2019-02-02 21:34:11 +02:00
|
|
|
for (int i = 0; i < 255 && pos + i + 1 < end; i++) {
|
|
|
|
if (pos[i] == 'p' && pos[i + 1] == '\0') {
|
|
|
|
*pos_p = pos + i - buf;
|
2015-04-03 18:19:53 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (startcode == VOP_STARTCODE && nb_vop) {
|
|
|
|
*nb_vop += 1;
|
|
|
|
if (*nb_vop == 2 && pos_vop2) {
|
2019-02-02 21:34:11 +02:00
|
|
|
*pos_vop2 = pos - buf - 4; /* subtract 4 bytes startcode */
|
2015-04-03 18:19:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-17 19:47:25 +02:00
|
|
|
static int mpeg4_unpack_bframes_filter(AVBSFContext *ctx, AVPacket *out)
|
2015-04-03 18:19:53 +02:00
|
|
|
{
|
2016-04-17 19:47:25 +02:00
|
|
|
UnpackBFramesBSFContext *s = ctx->priv_data;
|
2015-04-03 18:19:53 +02:00
|
|
|
int pos_p = -1, nb_vop = 0, pos_vop2 = -1, ret = 0;
|
2016-04-17 19:47:25 +02:00
|
|
|
AVPacket *in;
|
2015-04-03 18:19:53 +02:00
|
|
|
|
2016-04-17 19:47:25 +02:00
|
|
|
ret = ff_bsf_get_packet(ctx, &in);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2015-04-03 18:19:53 +02:00
|
|
|
|
2016-04-17 19:47:25 +02:00
|
|
|
scan_buffer(in->data, in->size, &pos_p, &nb_vop, &pos_vop2);
|
|
|
|
av_log(ctx, AV_LOG_DEBUG, "Found %d VOP startcode(s) in this packet.\n", nb_vop);
|
2015-04-03 18:19:53 +02:00
|
|
|
|
|
|
|
if (pos_vop2 >= 0) {
|
2018-03-21 02:59:10 +02:00
|
|
|
if (s->b_frame->data) {
|
2016-04-17 19:47:25 +02:00
|
|
|
av_log(ctx, AV_LOG_WARNING,
|
2015-04-03 18:19:53 +02:00
|
|
|
"Missing one N-VOP packet, discarding one B-frame.\n");
|
2018-03-21 02:59:10 +02:00
|
|
|
av_packet_unref(s->b_frame);
|
2015-04-03 18:19:53 +02:00
|
|
|
}
|
|
|
|
/* store the packed B-frame in the BSFContext */
|
2018-03-21 02:59:10 +02:00
|
|
|
ret = av_packet_ref(s->b_frame, in);
|
|
|
|
if (ret < 0) {
|
2018-03-19 04:25:32 +02:00
|
|
|
goto fail;
|
2015-04-03 18:19:53 +02:00
|
|
|
}
|
2018-03-21 02:59:10 +02:00
|
|
|
s->b_frame->size -= pos_vop2;
|
|
|
|
s->b_frame->data += pos_vop2;
|
2015-04-03 18:19:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nb_vop > 2) {
|
2016-04-17 19:47:25 +02:00
|
|
|
av_log(ctx, AV_LOG_WARNING,
|
2015-04-03 18:19:53 +02:00
|
|
|
"Found %d VOP headers in one packet, only unpacking one.\n", nb_vop);
|
|
|
|
}
|
|
|
|
|
2018-03-21 02:59:10 +02:00
|
|
|
if (nb_vop == 1 && s->b_frame->data) {
|
2015-04-03 18:19:53 +02:00
|
|
|
/* use frame from BSFContext */
|
2018-03-21 02:59:10 +02:00
|
|
|
av_packet_move_ref(out, s->b_frame);
|
|
|
|
|
|
|
|
/* use properties from current input packet */
|
2016-07-22 17:59:29 +02:00
|
|
|
ret = av_packet_copy_props(out, in);
|
|
|
|
if (ret < 0) {
|
2018-03-19 04:25:32 +02:00
|
|
|
goto fail;
|
2016-07-22 17:59:29 +02:00
|
|
|
}
|
|
|
|
|
2016-04-17 19:47:25 +02:00
|
|
|
if (in->size <= MAX_NVOP_SIZE) {
|
2015-04-03 18:19:53 +02:00
|
|
|
/* N-VOP */
|
2016-04-17 19:47:25 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "Skipping N-VOP.\n");
|
2015-04-03 18:19:53 +02:00
|
|
|
} else {
|
|
|
|
/* copy packet into BSFContext */
|
2018-03-21 02:59:10 +02:00
|
|
|
av_packet_move_ref(s->b_frame, in);
|
2015-04-03 18:19:53 +02:00
|
|
|
}
|
|
|
|
} else if (nb_vop >= 2) {
|
|
|
|
/* use first frame of the packet */
|
2016-04-17 19:47:25 +02:00
|
|
|
av_packet_move_ref(out, in);
|
|
|
|
out->size = pos_vop2;
|
2015-04-03 18:19:53 +02:00
|
|
|
} else if (pos_p >= 0) {
|
2018-03-22 03:37:46 +02:00
|
|
|
ret = av_packet_make_writable(in);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2016-04-17 19:47:25 +02:00
|
|
|
av_log(ctx, AV_LOG_DEBUG, "Updating DivX userdata (remove trailing 'p').\n");
|
|
|
|
av_packet_move_ref(out, in);
|
2015-04-03 18:19:53 +02:00
|
|
|
/* remove 'p' (packed) from the end of the (DivX) userdata string */
|
2016-04-17 19:47:25 +02:00
|
|
|
out->data[pos_p] = '\0';
|
2015-04-03 18:19:53 +02:00
|
|
|
} else {
|
|
|
|
/* copy packet */
|
2016-04-17 19:47:25 +02:00
|
|
|
av_packet_move_ref(out, in);
|
|
|
|
}
|
|
|
|
|
2018-03-19 04:25:32 +02:00
|
|
|
fail:
|
|
|
|
if (ret < 0)
|
|
|
|
av_packet_unref(out);
|
2016-04-17 19:47:25 +02:00
|
|
|
av_packet_free(&in);
|
|
|
|
|
2018-03-19 04:25:32 +02:00
|
|
|
return ret;
|
2016-04-17 19:47:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int mpeg4_unpack_bframes_init(AVBSFContext *ctx)
|
|
|
|
{
|
2018-03-21 02:59:10 +02:00
|
|
|
UnpackBFramesBSFContext *s = ctx->priv_data;
|
|
|
|
|
|
|
|
s->b_frame = av_packet_alloc();
|
|
|
|
if (!s->b_frame)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2016-04-17 19:47:25 +02:00
|
|
|
if (ctx->par_in->extradata) {
|
|
|
|
int pos_p_ext = -1;
|
|
|
|
scan_buffer(ctx->par_in->extradata, ctx->par_in->extradata_size, &pos_p_ext, NULL, NULL);
|
|
|
|
if (pos_p_ext >= 0) {
|
|
|
|
av_log(ctx, AV_LOG_DEBUG,
|
|
|
|
"Updating DivX userdata (remove trailing 'p') in extradata.\n");
|
|
|
|
ctx->par_out->extradata[pos_p_ext] = '\0';
|
|
|
|
}
|
2015-04-03 18:19:53 +02:00
|
|
|
}
|
|
|
|
|
2016-04-17 19:47:25 +02:00
|
|
|
return 0;
|
2015-04-03 18:19:53 +02:00
|
|
|
}
|
|
|
|
|
2018-07-27 03:26:01 +02:00
|
|
|
static void mpeg4_unpack_bframes_flush(AVBSFContext *bsfc)
|
|
|
|
{
|
|
|
|
UnpackBFramesBSFContext *ctx = bsfc->priv_data;
|
|
|
|
av_packet_unref(ctx->b_frame);
|
|
|
|
}
|
|
|
|
|
2016-04-17 19:47:25 +02:00
|
|
|
static void mpeg4_unpack_bframes_close(AVBSFContext *bsfc)
|
2015-04-03 18:19:53 +02:00
|
|
|
{
|
|
|
|
UnpackBFramesBSFContext *ctx = bsfc->priv_data;
|
2018-03-21 02:59:10 +02:00
|
|
|
av_packet_free(&ctx->b_frame);
|
2015-04-03 18:19:53 +02:00
|
|
|
}
|
|
|
|
|
2016-04-17 19:47:25 +02:00
|
|
|
static const enum AVCodecID codec_ids[] = {
|
|
|
|
AV_CODEC_ID_MPEG4, AV_CODEC_ID_NONE,
|
|
|
|
};
|
|
|
|
|
|
|
|
const AVBitStreamFilter ff_mpeg4_unpack_bframes_bsf = {
|
2015-04-03 18:19:53 +02:00
|
|
|
.name = "mpeg4_unpack_bframes",
|
|
|
|
.priv_data_size = sizeof(UnpackBFramesBSFContext),
|
2016-04-17 19:47:25 +02:00
|
|
|
.init = mpeg4_unpack_bframes_init,
|
2015-04-03 18:19:53 +02:00
|
|
|
.filter = mpeg4_unpack_bframes_filter,
|
2018-07-27 03:26:01 +02:00
|
|
|
.flush = mpeg4_unpack_bframes_flush,
|
2016-04-17 19:47:25 +02:00
|
|
|
.close = mpeg4_unpack_bframes_close,
|
|
|
|
.codec_ids = codec_ids,
|
2015-04-03 18:19:53 +02:00
|
|
|
};
|