FFmpeg
mxpegdec.c
Go to the documentation of this file.
1 /*
2  * MxPEG decoder
3  * Copyright (c) 2011 Anatoly Nenashev
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 
23 /**
24  * @file
25  * MxPEG decoder
26  */
27 
28 #include "internal.h"
29 #include "mjpeg.h"
30 #include "mjpegdec.h"
31 
32 typedef struct MXpegDecodeContext {
34  AVFrame *picture[2]; /* pictures array */
35  int picture_index; /* index of current picture */
36  int got_sof_data; /* true if SOF data successfully parsed */
37  int got_mxm_bitmask; /* true if MXM bitmask available */
38  uint8_t *mxm_bitmask; /* bitmask buffer */
39  unsigned bitmask_size; /* size of bitmask */
40  int has_complete_frame; /* true if has complete frame */
41  uint8_t *completion_bitmask; /* completion bitmask of macroblocks */
42  unsigned mb_width, mb_height; /* size of picture in MB's from MXM header */
44 
46 {
47  MXpegDecodeContext *s = avctx->priv_data;
48  MJpegDecodeContext *jpg = &s->jpg;
49  int i;
50 
51  jpg->picture_ptr = NULL;
52  ff_mjpeg_decode_end(avctx);
53 
54  for (i = 0; i < 2; ++i)
55  av_frame_free(&s->picture[i]);
56 
57  s->bitmask_size = 0;
58  av_freep(&s->mxm_bitmask);
60 
61  return 0;
62 }
63 
65 {
66  MXpegDecodeContext *s = avctx->priv_data;
67 
68  s->picture[0] = av_frame_alloc();
69  s->picture[1] = av_frame_alloc();
70  if (!s->picture[0] || !s->picture[1])
71  return AVERROR(ENOMEM);
72 
73  s->jpg.picture_ptr = s->picture[0];
74  return ff_mjpeg_decode_init(avctx);
75 }
76 
78  const uint8_t *buf_ptr, int buf_size)
79 {
80  int len;
81  if (buf_size < 2)
82  return 0;
83  len = AV_RB16(buf_ptr);
84  skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
85 
86  return 0;
87 }
88 
90  const uint8_t *buf_ptr, int buf_size)
91 {
92  unsigned bitmask_size, mb_count;
93  int i;
94 
95  s->mb_width = AV_RL16(buf_ptr+4);
96  s->mb_height = AV_RL16(buf_ptr+6);
97  mb_count = s->mb_width * s->mb_height;
98 
99  bitmask_size = (mb_count + 7) >> 3;
100  if (bitmask_size > buf_size - 12) {
102  "MXM bitmask is not complete\n");
103  return AVERROR(EINVAL);
104  }
105 
106  if (s->bitmask_size != bitmask_size) {
107  s->bitmask_size = 0;
108  av_freep(&s->mxm_bitmask);
109  s->mxm_bitmask = av_malloc(bitmask_size);
110  if (!s->mxm_bitmask) {
112  "MXM bitmask memory allocation error\n");
113  return AVERROR(ENOMEM);
114  }
115 
117  s->completion_bitmask = av_mallocz(bitmask_size);
118  if (!s->completion_bitmask) {
120  "Completion bitmask memory allocation error\n");
121  return AVERROR(ENOMEM);
122  }
123 
125  }
126 
127  memcpy(s->mxm_bitmask, buf_ptr + 12, bitmask_size);
128  s->got_mxm_bitmask = 1;
129 
130  if (!s->has_complete_frame) {
131  uint8_t completion_check = 0xFF;
132  for (i = 0; i < bitmask_size; ++i) {
133  s->completion_bitmask[i] |= s->mxm_bitmask[i];
134  completion_check &= s->completion_bitmask[i];
135  }
136  s->has_complete_frame = !(completion_check ^ 0xFF);
137  }
138 
139  return 0;
140 }
141 
143  const uint8_t *buf_ptr, int buf_size)
144 {
145  int len, ret = 0;
146  if (buf_size < 2)
147  return 0;
148  len = AV_RB16(buf_ptr);
149  if (len > 14 && len <= buf_size && !strncmp(buf_ptr + 2, "MXM", 3)) {
150  ret = mxpeg_decode_mxm(s, buf_ptr + 2, len - 2);
151  }
152  skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
153 
154  return ret;
155 }
156 
158  AVFrame *reference_ptr)
159 {
160  if ((jpg->width + 0x0F)>>4 != s->mb_width ||
161  (jpg->height + 0x0F)>>4 != s->mb_height) {
162  av_log(jpg->avctx, AV_LOG_ERROR,
163  "Picture dimensions stored in SOF and MXM mismatch\n");
164  return AVERROR(EINVAL);
165  }
166 
167  if (reference_ptr->data[0]) {
168  int i;
169  for (i = 0; i < MAX_COMPONENTS; ++i) {
170  if ( (!reference_ptr->data[i] ^ !jpg->picture_ptr->data[i]) ||
171  reference_ptr->linesize[i] != jpg->picture_ptr->linesize[i]) {
172  av_log(jpg->avctx, AV_LOG_ERROR,
173  "Dimensions of current and reference picture mismatch\n");
174  return AVERROR(EINVAL);
175  }
176  }
177  }
178 
179  return 0;
180 }
181 
183  void *data, int *got_frame,
184  AVPacket *avpkt)
185 {
186  const uint8_t *buf = avpkt->data;
187  int buf_size = avpkt->size;
188  MXpegDecodeContext *s = avctx->priv_data;
189  MJpegDecodeContext *jpg = &s->jpg;
190  const uint8_t *buf_end, *buf_ptr;
191  const uint8_t *unescaped_buf_ptr;
192  int unescaped_buf_size;
193  int start_code;
194  int ret;
195 
196  buf_ptr = buf;
197  buf_end = buf + buf_size;
198  jpg->got_picture = 0;
199  s->got_mxm_bitmask = 0;
200  s->got_sof_data = !!s->got_sof_data;
201  while (buf_ptr < buf_end) {
202  start_code = ff_mjpeg_find_marker(jpg, &buf_ptr, buf_end,
203  &unescaped_buf_ptr, &unescaped_buf_size);
204  if (start_code < 0)
205  goto the_end;
206  {
207  init_get_bits(&jpg->gb, unescaped_buf_ptr, unescaped_buf_size*8);
208 
209  if (start_code >= APP0 && start_code <= APP15) {
210  mxpeg_decode_app(s, unescaped_buf_ptr, unescaped_buf_size);
211  }
212 
213  switch (start_code) {
214  case SOI:
215  if (jpg->got_picture) //emulating EOI
216  goto the_end;
217  break;
218  case EOI:
219  goto the_end;
220  case DQT:
221  ret = ff_mjpeg_decode_dqt(jpg);
222  if (ret < 0) {
223  av_log(avctx, AV_LOG_ERROR,
224  "quantization table decode error\n");
225  return ret;
226  }
227  break;
228  case DHT:
229  ret = ff_mjpeg_decode_dht(jpg);
230  if (ret < 0) {
231  av_log(avctx, AV_LOG_ERROR,
232  "huffman table decode error\n");
233  return ret;
234  }
235  break;
236  case COM:
237  ret = mxpeg_decode_com(s, unescaped_buf_ptr,
238  unescaped_buf_size);
239  if (ret < 0)
240  return ret;
241  break;
242  case SOF0:
243  if (s->got_sof_data > 1) {
244  av_log(avctx, AV_LOG_ERROR,
245  "Multiple SOF in a frame\n");
246  return AVERROR_INVALIDDATA;
247  }
248  s->got_sof_data = 0;
249  ret = ff_mjpeg_decode_sof(jpg);
250  if (ret < 0) {
251  av_log(avctx, AV_LOG_ERROR,
252  "SOF data decode error\n");
253  return ret;
254  }
255  if (jpg->interlaced) {
256  av_log(avctx, AV_LOG_ERROR,
257  "Interlaced mode not supported in MxPEG\n");
258  return AVERROR(EINVAL);
259  }
260  s->got_sof_data ++;
261  break;
262  case SOS:
263  if (!s->got_sof_data) {
264  av_log(avctx, AV_LOG_WARNING,
265  "Can not process SOS without SOF data, skipping\n");
266  break;
267  }
268  if (!jpg->got_picture) {
269  if (jpg->first_picture) {
270  av_log(avctx, AV_LOG_WARNING,
271  "First picture has no SOF, skipping\n");
272  break;
273  }
274  if (!s->got_mxm_bitmask){
275  av_log(avctx, AV_LOG_WARNING,
276  "Non-key frame has no MXM, skipping\n");
277  break;
278  }
279  /* use stored SOF data to allocate current picture */
281  if ((ret = ff_get_buffer(avctx, jpg->picture_ptr,
283  return ret;
285  jpg->picture_ptr->key_frame = 0;
286  jpg->got_picture = 1;
287  } else {
289  jpg->picture_ptr->key_frame = 1;
290  }
291 
292  if (s->got_mxm_bitmask) {
293  AVFrame *reference_ptr = s->picture[s->picture_index ^ 1];
294  if (mxpeg_check_dimensions(s, jpg, reference_ptr) < 0)
295  break;
296 
297  /* allocate dummy reference picture if needed */
298  if (!reference_ptr->data[0] &&
299  (ret = ff_get_buffer(avctx, reference_ptr,
301  return ret;
302 
303  ret = ff_mjpeg_decode_sos(jpg, s->mxm_bitmask, s->bitmask_size, reference_ptr);
304  if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
305  return ret;
306  } else {
307  ret = ff_mjpeg_decode_sos(jpg, NULL, 0, NULL);
308  if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
309  return ret;
310  }
311 
312  break;
313  }
314 
315  buf_ptr += (get_bits_count(&jpg->gb)+7) >> 3;
316  }
317 
318  }
319 
320 the_end:
321  if (jpg->got_picture) {
322  int ret = av_frame_ref(data, jpg->picture_ptr);
323  if (ret < 0)
324  return ret;
325  *got_frame = 1;
326 
327  s->picture_index ^= 1;
328  jpg->picture_ptr = s->picture[s->picture_index];
329 
330  if (!s->has_complete_frame) {
331  if (!s->got_mxm_bitmask)
332  s->has_complete_frame = 1;
333  else
334  *got_frame = 0;
335  }
336  }
337 
338  return buf_ptr - buf;
339 }
340 
342  .name = "mxpeg",
343  .long_name = NULL_IF_CONFIG_SMALL("Mobotix MxPEG video"),
344  .type = AVMEDIA_TYPE_VIDEO,
345  .id = AV_CODEC_ID_MXPEG,
346  .priv_data_size = sizeof(MXpegDecodeContext),
348  .close = mxpeg_decode_end,
350  .capabilities = AV_CODEC_CAP_DR1,
351  .max_lowres = 3,
353 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
uint8_t * completion_bitmask
Definition: mxpegdec.c:41
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
Definition: mjpeg.h:71
Definition: mjpeg.h:111
Definition: mjpeg.h:73
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
uint8_t * mxm_bitmask
Definition: mxpegdec.c:38
int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
Definition: mjpegdec.c:227
int size
Definition: packet.h:364
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
static int mxpeg_check_dimensions(MXpegDecodeContext *s, MJpegDecodeContext *jpg, AVFrame *reference_ptr)
Definition: mxpegdec.c:157
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:91
int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
Definition: mjpegdec.c:268
AVCodec.
Definition: codec.h:190
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:91
MJPEG encoder and decoder.
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static int mxpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: mxpegdec.c:182
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
Definition: mjpeg.h:72
uint8_t
#define av_cold
Definition: attributes.h:88
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
AVCodec ff_mxpeg_decoder
Definition: mxpegdec.c:341
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:443
AVFrame * picture_ptr
Definition: mjpegdec.h:104
uint8_t * data
Definition: packet.h:363
#define MAX_COMPONENTS
Definition: mjpegdec.h:45
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
#define av_log(a,...)
AVFrame * picture[2]
Definition: mxpegdec.c:34
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
static int mxpeg_decode_mxm(MXpegDecodeContext *s, const uint8_t *buf_ptr, int buf_size)
Definition: mxpegdec.c:89
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition: mjpegdec.c:2911
unsigned bitmask_size
Definition: mxpegdec.c:39
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:115
const char * name
Name of the codec implementation.
Definition: codec.h:197
Definition: mjpeg.h:39
Definition: mjpeg.h:70
unsigned mb_width
Definition: mxpegdec.c:42
Definition: mjpeg.h:79
Definition: mjpeg.h:56
int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
Definition: mjpegdec.c:329
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:397
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1660
#define FFMIN(a, b)
Definition: common.h:96
#define s(width, name)
Definition: cbs_vp9.c:257
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:1671
GetBitContext gb
Definition: mjpegdec.h:50
static av_cold int mxpeg_decode_init(AVCodecContext *avctx)
Definition: mxpegdec.c:64
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:345
int has_complete_frame
Definition: mxpegdec.c:40
main external API structure.
Definition: avcodec.h:531
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1876
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
MJpegDecodeContext jpg
Definition: mxpegdec.c:33
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:328
static int mxpeg_decode_com(MXpegDecodeContext *s, const uint8_t *buf_ptr, int buf_size)
Definition: mxpegdec.c:142
int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask, int mb_bitmask_size, const AVFrame *reference)
Definition: mjpegdec.c:1651
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition: mjpegdec.c:150
Definition: mjpeg.h:94
unsigned mb_height
Definition: mxpegdec.c:42
common internal api header.
static av_cold int mxpeg_decode_end(AVCodecContext *avctx)
Definition: mxpegdec.c:45
AVCodecContext * avctx
Definition: mjpegdec.h:49
void * priv_data
Definition: avcodec.h:558
static int mxpeg_decode_app(MXpegDecodeContext *s, const uint8_t *buf_ptr, int buf_size)
Definition: mxpegdec.c:77
int got_picture
we found a SOF and picture is valid, too.
Definition: mjpegdec.h:105
int len
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:392
#define av_freep(p)
static const uint8_t start_code[]
int ff_mjpeg_find_marker(MJpegDecodeContext *s, const uint8_t **buf_ptr, const uint8_t *buf_end, const uint8_t **unescaped_buf_ptr, int *unescaped_buf_size)
Definition: mjpegdec.c:2223
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
MJPEG decoder.
This structure stores compressed data.
Definition: packet.h:340
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:514
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
int i
Definition: input.c:407
Predicted.
Definition: avutil.h:275