FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "avcodec.h"
23 #include "mpeg4video.h"
24 
25 typedef struct UnpackBFramesBSFContext {
30 
31 /* search next start code */
32 static unsigned int find_startcode(const uint8_t *buf, int buf_size, int *pos)
33 {
34  unsigned int startcode = 0xFF;
35 
36  for (; *pos < buf_size;) {
37  startcode = ((startcode << 8) | buf[*pos]) & 0xFFFFFFFF;
38  *pos +=1;
39  if ((startcode & 0xFFFFFF00) != 0x100)
40  continue; /* no startcode */
41  return startcode;
42  }
43 
44  return 0;
45 }
46 
47 /* determine the position of the packed marker in the userdata,
48  * the number of VOPs and the position of the second VOP */
49 static void scan_buffer(const uint8_t *buf, int buf_size,
50  int *pos_p, int *nb_vop, int *pos_vop2) {
51  unsigned int startcode;
52  int pos, i;
53 
54  for (pos = 0; pos < buf_size;) {
55  startcode = find_startcode(buf, buf_size, &pos);
56 
57  if (startcode == USER_DATA_STARTCODE && pos_p) {
58  /* check if the (DivX) userdata string ends with 'p' (packed) */
59  for (i = 0; i < 255 && pos + i + 1 < buf_size; i++) {
60  if (buf[pos + i] == 'p' && buf[pos + i + 1] == '\0') {
61  *pos_p = pos + i;
62  break;
63  }
64  }
65  } else if (startcode == VOP_STARTCODE && nb_vop) {
66  *nb_vop += 1;
67  if (*nb_vop == 2 && pos_vop2) {
68  *pos_vop2 = pos - 4; /* subtract 4 bytes startcode */
69  }
70  }
71  }
72 }
73 
74 /* allocate new buffer and copy size bytes from src */
75 static uint8_t *create_new_buffer(const uint8_t *src, int size) {
77 
78  if (dst) {
79  memcpy(dst, src, size);
80  memset(dst + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
81  }
82 
83  return dst;
84 }
85 
87  AVCodecContext *avctx, const char *args,
88  uint8_t **poutbuf, int *poutbuf_size,
89  const uint8_t *buf, int buf_size,
90  int keyframe)
91 {
93  int pos_p = -1, nb_vop = 0, pos_vop2 = -1, ret = 0;
94 
95  if (avctx->codec_id != AV_CODEC_ID_MPEG4) {
96  av_log(avctx, AV_LOG_ERROR,
97  "The mpeg4_unpack_bframes bitstream filter is only useful for mpeg4.\n");
98  return AVERROR(EINVAL);
99  }
100 
101  if (!ctx->updated_extradata && avctx->extradata) {
102  int pos_p_ext = -1;
103  scan_buffer(avctx->extradata, avctx->extradata_size, &pos_p_ext, NULL, NULL);
104  if (pos_p_ext >= 0) {
105  av_log(avctx, AV_LOG_DEBUG,
106  "Updating DivX userdata (remove trailing 'p') in extradata.\n");
107  avctx->extradata[pos_p_ext] = '\0';
108  }
109  ctx->updated_extradata = 1;
110  }
111 
112  scan_buffer(buf, buf_size, &pos_p, &nb_vop, &pos_vop2);
113  av_log(avctx, AV_LOG_DEBUG, "Found %d VOP startcode(s) in this packet.\n", nb_vop);
114 
115  if (pos_vop2 >= 0) {
116  if (ctx->b_frame_buf) {
117  av_log(avctx, AV_LOG_WARNING,
118  "Missing one N-VOP packet, discarding one B-frame.\n");
119  av_freep(&ctx->b_frame_buf);
120  ctx->b_frame_buf_size = 0;
121  }
122  /* store the packed B-frame in the BSFContext */
123  ctx->b_frame_buf_size = buf_size - pos_vop2;
124  ctx->b_frame_buf = create_new_buffer(buf + pos_vop2, ctx->b_frame_buf_size);
125  if (!ctx->b_frame_buf) {
126  ctx->b_frame_buf_size = 0;
127  return AVERROR(ENOMEM);
128  }
129  }
130 
131  if (nb_vop > 2) {
132  av_log(avctx, AV_LOG_WARNING,
133  "Found %d VOP headers in one packet, only unpacking one.\n", nb_vop);
134  }
135 
136  if (nb_vop == 1 && ctx->b_frame_buf) {
137  /* use frame from BSFContext */
138  *poutbuf = ctx->b_frame_buf;
139  *poutbuf_size = ctx->b_frame_buf_size;
140  /* the output buffer is distinct from the input buffer */
141  ret = 1;
142  if (buf_size <= MAX_NVOP_SIZE) {
143  /* N-VOP */
144  av_log(avctx, AV_LOG_DEBUG, "Skipping N-VOP.\n");
145  ctx->b_frame_buf = NULL;
146  ctx->b_frame_buf_size = 0;
147  } else {
148  /* copy packet into BSFContext */
149  ctx->b_frame_buf_size = buf_size;
150  ctx->b_frame_buf = create_new_buffer(buf , buf_size);
151  if (!ctx->b_frame_buf) {
152  ctx->b_frame_buf_size = 0;
153  av_freep(poutbuf);
154  *poutbuf_size = 0;
155  return AVERROR(ENOMEM);
156  }
157  }
158  } else if (nb_vop >= 2) {
159  /* use first frame of the packet */
160  *poutbuf = (uint8_t *) buf;
161  *poutbuf_size = pos_vop2;
162  } else if (pos_p >= 0) {
163  av_log(avctx, AV_LOG_DEBUG, "Updating DivX userdata (remove trailing 'p').\n");
164  *poutbuf_size = buf_size;
165  *poutbuf = create_new_buffer(buf, buf_size);
166  if (!*poutbuf) {
167  *poutbuf_size = 0;
168  return AVERROR(ENOMEM);
169  }
170  /* remove 'p' (packed) from the end of the (DivX) userdata string */
171  (*poutbuf)[pos_p] = '\0';
172  /* the output buffer is distinct from the input buffer */
173  ret = 1;
174  } else {
175  /* copy packet */
176  *poutbuf = (uint8_t *) buf;
177  *poutbuf_size = buf_size;
178  }
179 
180  return ret;
181 }
182 
184 {
186  av_freep(&ctx->b_frame_buf);
187 }
188 
190  .name = "mpeg4_unpack_bframes",
191  .priv_data_size = sizeof(UnpackBFramesBSFContext),
194 };
#define NULL
Definition: coverity.c:32
AVFormatContext * ctx
Definition: movenc-test.c:48
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define USER_DATA_STARTCODE
Definition: mpeg4video.h:57
uint8_t
#define av_malloc(s)
#define VOP_STARTCODE
Definition: mpeg4video.h:60
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1647
static int mpeg4_unpack_bframes_filter(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe)
static void filter(int16_t *output, ptrdiff_t out_stride, int16_t *low, ptrdiff_t low_stride, int16_t *high, ptrdiff_t high_stride, int len, uint8_t clip)
Definition: cfhd.c:82
const char * name
Definition: avcodec.h:5152
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
AVBitStreamFilter ff_mpeg4_unpack_bframes_bsf
static void mpeg4_unpack_bframes_close(AVBitStreamFilterContext *bsfc)
static unsigned int find_startcode(const uint8_t *buf, int buf_size, int *pos)
static uint8_t * create_new_buffer(const uint8_t *src, int size)
#define src
Definition: vp9dsp.c:530
Libavcodec external API header.
enum AVCodecID codec_id
Definition: avcodec.h:1549
main external API structure.
Definition: avcodec.h:1532
void * buf
Definition: avisynth_c.h:553
int extradata_size
Definition: avcodec.h:1648
static void scan_buffer(const uint8_t *buf, int buf_size, int *pos_p, int *nb_vop, int *pos_vop2)
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:635
#define av_freep(p)
#define MAX_NVOP_SIZE
Definition: mpeg4video.h:63