FFmpeg
mpeg4_unpack_bframes_bsf.c
Go to the documentation of this file.
1 /*
2  * Bitstream filter for unpacking DivX-style packed B-frames in MPEG-4 (divx_packed)
3  * Copyright (c) 2015 Andreas Cadhalpun <Andreas.Cadhalpun@googlemail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "bsf.h"
23 #include "bsf_internal.h"
24 #include "internal.h"
25 #include "mpeg4video.h"
26 
27 typedef struct UnpackBFramesBSFContext {
30 
31 /* determine the position of the packed marker in the userdata,
32  * the number of VOPs and the position of the second VOP */
33 static void scan_buffer(const uint8_t *buf, int buf_size,
34  int *pos_p, int *nb_vop, int *pos_vop2) {
35  uint32_t startcode;
36  const uint8_t *end = buf + buf_size, *pos = buf;
37 
38  while (pos < end) {
39  startcode = -1;
40  pos = avpriv_find_start_code(pos, end, &startcode);
41 
42  if (startcode == USER_DATA_STARTCODE && pos_p) {
43  /* check if the (DivX) userdata string ends with 'p' (packed) */
44  for (int i = 0; i < 255 && pos + i + 1 < end; i++) {
45  if (pos[i] == 'p' && pos[i + 1] == '\0') {
46  *pos_p = pos + i - buf;
47  break;
48  }
49  }
50  } else if (startcode == VOP_STARTCODE && nb_vop) {
51  *nb_vop += 1;
52  if (*nb_vop == 2 && pos_vop2) {
53  *pos_vop2 = pos - buf - 4; /* subtract 4 bytes startcode */
54  }
55  }
56  }
57 }
58 
60 {
62  int pos_p = -1, nb_vop = 0, pos_vop2 = -1, ret = 0;
63 
65  if (ret < 0)
66  return ret;
67 
68  scan_buffer(pkt->data, pkt->size, &pos_p, &nb_vop, &pos_vop2);
69  av_log(ctx, AV_LOG_DEBUG, "Found %d VOP startcode(s) in this packet.\n", nb_vop);
70 
71  if (pos_vop2 >= 0) {
72  if (s->b_frame_ref) {
74  "Missing one N-VOP packet, discarding one B-frame.\n");
75  av_buffer_unref(&s->b_frame_ref);
76  }
77  /* store a reference to the packed B-frame's data in the BSFContext */
78  s->b_frame_ref = av_buffer_ref(pkt->buf);
79  if (!s->b_frame_ref) {
80  ret = AVERROR(ENOMEM);
81  goto fail;
82  }
83  s->b_frame_ref->data = pkt->data + pos_vop2;
84  s->b_frame_ref->size = pkt->size - pos_vop2;
85  }
86 
87  if (nb_vop > 2) {
89  "Found %d VOP headers in one packet, only unpacking one.\n", nb_vop);
90  }
91 
92  if (nb_vop == 1 && s->b_frame_ref) {
93  AVBufferRef *tmp = pkt->buf;
94 
95  /* make tmp accurately reflect the packet's data */
96  tmp->data = pkt->data;
97  tmp->size = pkt->size;
98 
99  /* replace data in packet with stored data */
100  pkt->buf = s->b_frame_ref;
101  pkt->data = s->b_frame_ref->data;
102  pkt->size = s->b_frame_ref->size;
103 
104  /* store reference to data into BSFContext */
105  s->b_frame_ref = tmp;
106 
107  if (s->b_frame_ref->size <= MAX_NVOP_SIZE) {
108  /* N-VOP - discard stored data */
109  av_log(ctx, AV_LOG_DEBUG, "Skipping N-VOP.\n");
110  av_buffer_unref(&s->b_frame_ref);
111  }
112  } else if (nb_vop >= 2) {
113  /* use first frame of the packet */
114  pkt->size = pos_vop2;
115  } else if (pos_p >= 0) {
117  if (ret < 0)
118  goto fail;
119  av_log(ctx, AV_LOG_DEBUG, "Updating DivX userdata (remove trailing 'p').\n");
120  /* remove 'p' (packed) from the end of the (DivX) userdata string */
121  pkt->data[pos_p] = '\0';
122  } else {
123  /* use packet as is */
124  }
125 
126 fail:
127  if (ret < 0)
129 
130  return ret;
131 }
132 
134 {
135  if (ctx->par_in->extradata) {
136  int pos_p_ext = -1;
137  scan_buffer(ctx->par_in->extradata, ctx->par_in->extradata_size, &pos_p_ext, NULL, NULL);
138  if (pos_p_ext >= 0) {
140  "Updating DivX userdata (remove trailing 'p') in extradata.\n");
141  ctx->par_out->extradata[pos_p_ext] = '\0';
142  }
143  }
144 
145  return 0;
146 }
147 
149 {
151  av_buffer_unref(&ctx->b_frame_ref);
152 }
153 
154 static const enum AVCodecID codec_ids[] = {
156 };
157 
159  .name = "mpeg4_unpack_bframes",
160  .priv_data_size = sizeof(UnpackBFramesBSFContext),
165  .codec_ids = codec_ids,
166 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:424
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
bsf_internal.h
AVBitStreamFilter::name
const char * name
Definition: bsf.h:91
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:62
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
avpriv_find_start_code
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
AVBSFContext
The bitstream filter state.
Definition: bsf.h:47
UnpackBFramesBSFContext
Definition: mpeg4_unpack_bframes_bsf.c:27
init
static int init
Definition: av_tx.c:47
bsf.h
fail
#define fail()
Definition: checkasm.h:127
mpeg4_unpack_bframes_filter
static int mpeg4_unpack_bframes_filter(AVBSFContext *ctx, AVPacket *pkt)
Definition: mpeg4_unpack_bframes_bsf.c:59
pkt
AVPacket * pkt
Definition: movenc.c:59
s
#define s(width, name)
Definition: cbs_vp9.c:257
codec_ids
static enum AVCodecID codec_ids[]
Definition: mpeg4_unpack_bframes_bsf.c:154
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AVPacket::buf
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:356
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:593
NULL
#define NULL
Definition: coverity.c:32
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:47
AVPacket::size
int size
Definition: packet.h:374
scan_buffer
static void scan_buffer(const uint8_t *buf, int buf_size, int *pos_p, int *nb_vop, int *pos_vop2)
Definition: mpeg4_unpack_bframes_bsf.c:33
mpeg4_unpack_bframes_init
static int mpeg4_unpack_bframes_init(AVBSFContext *ctx)
Definition: mpeg4_unpack_bframes_bsf.c:133
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:48
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
USER_DATA_STARTCODE
#define USER_DATA_STARTCODE
Definition: mpeg4video.h:62
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:62
ret
ret
Definition: filter_design.txt:187
UnpackBFramesBSFContext::b_frame_ref
AVBufferRef * b_frame_ref
Definition: mpeg4_unpack_bframes_bsf.c:28
pos
unsigned int pos
Definition: spdifenc.c:412
AVBitStreamFilter
Definition: bsf.h:90
mpeg4video.h
av_packet_make_writable
int av_packet_make_writable(AVPacket *pkt)
Create a writable reference for the data described by a given packet, avoiding data copy if possible.
Definition: avpacket.c:506
MAX_NVOP_SIZE
#define MAX_NVOP_SIZE
Definition: mpeg4video.h:72
ff_mpeg4_unpack_bframes_bsf
const AVBitStreamFilter ff_mpeg4_unpack_bframes_bsf
Definition: mpeg4_unpack_bframes_bsf.c:158
VOP_STARTCODE
#define VOP_STARTCODE
Definition: mpeg4video.h:65
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
AVPacket
This structure stores compressed data.
Definition: packet.h:350
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
ff_bsf_get_packet_ref
int ff_bsf_get_packet_ref(AVBSFContext *ctx, AVPacket *pkt)
Called by bitstream filters to get packet for filtering.
Definition: bsf.c:252
mpeg4_unpack_bframes_close_flush
static void mpeg4_unpack_bframes_close_flush(AVBSFContext *bsfc)
Definition: mpeg4_unpack_bframes_bsf.c:148
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1228