FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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  mxpeg_decode_end(avctx);
72  return AVERROR(ENOMEM);
73  }
74 
75  s->jpg.picture_ptr = s->picture[0];
76  return ff_mjpeg_decode_init(avctx);
77 }
78 
80  const uint8_t *buf_ptr, int buf_size)
81 {
82  int len;
83  if (buf_size < 2)
84  return 0;
85  len = AV_RB16(buf_ptr);
86  skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
87 
88  return 0;
89 }
90 
92  const uint8_t *buf_ptr, int buf_size)
93 {
94  unsigned bitmask_size, mb_count;
95  int i;
96 
97  s->mb_width = AV_RL16(buf_ptr+4);
98  s->mb_height = AV_RL16(buf_ptr+6);
99  mb_count = s->mb_width * s->mb_height;
100 
101  bitmask_size = (mb_count + 7) >> 3;
102  if (bitmask_size > buf_size - 12) {
104  "MXM bitmask is not complete\n");
105  return AVERROR(EINVAL);
106  }
107 
108  if (s->bitmask_size != bitmask_size) {
109  s->bitmask_size = 0;
110  av_freep(&s->mxm_bitmask);
111  s->mxm_bitmask = av_malloc(bitmask_size);
112  if (!s->mxm_bitmask) {
114  "MXM bitmask memory allocation error\n");
115  return AVERROR(ENOMEM);
116  }
117 
119  s->completion_bitmask = av_mallocz(bitmask_size);
120  if (!s->completion_bitmask) {
122  "Completion bitmask memory allocation error\n");
123  return AVERROR(ENOMEM);
124  }
125 
126  s->bitmask_size = bitmask_size;
127  }
128 
129  memcpy(s->mxm_bitmask, buf_ptr + 12, bitmask_size);
130  s->got_mxm_bitmask = 1;
131 
132  if (!s->has_complete_frame) {
133  uint8_t completion_check = 0xFF;
134  for (i = 0; i < bitmask_size; ++i) {
135  s->completion_bitmask[i] |= s->mxm_bitmask[i];
136  completion_check &= s->completion_bitmask[i];
137  }
138  s->has_complete_frame = !(completion_check ^ 0xFF);
139  }
140 
141  return 0;
142 }
143 
145  const uint8_t *buf_ptr, int buf_size)
146 {
147  int len, ret = 0;
148  if (buf_size < 2)
149  return 0;
150  len = AV_RB16(buf_ptr);
151  if (len > 14 && len <= buf_size && !strncmp(buf_ptr + 2, "MXM", 3)) {
152  ret = mxpeg_decode_mxm(s, buf_ptr + 2, len - 2);
153  }
154  skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
155 
156  return ret;
157 }
158 
160  AVFrame *reference_ptr)
161 {
162  if ((jpg->width + 0x0F)>>4 != s->mb_width ||
163  (jpg->height + 0x0F)>>4 != s->mb_height) {
164  av_log(jpg->avctx, AV_LOG_ERROR,
165  "Picture dimensions stored in SOF and MXM mismatch\n");
166  return AVERROR(EINVAL);
167  }
168 
169  if (reference_ptr->data[0]) {
170  int i;
171  for (i = 0; i < MAX_COMPONENTS; ++i) {
172  if ( (!reference_ptr->data[i] ^ !jpg->picture_ptr->data[i]) ||
173  reference_ptr->linesize[i] != jpg->picture_ptr->linesize[i]) {
174  av_log(jpg->avctx, AV_LOG_ERROR,
175  "Dimensions of current and reference picture mismatch\n");
176  return AVERROR(EINVAL);
177  }
178  }
179  }
180 
181  return 0;
182 }
183 
185  void *data, int *got_frame,
186  AVPacket *avpkt)
187 {
188  const uint8_t *buf = avpkt->data;
189  int buf_size = avpkt->size;
190  MXpegDecodeContext *s = avctx->priv_data;
191  MJpegDecodeContext *jpg = &s->jpg;
192  const uint8_t *buf_end, *buf_ptr;
193  const uint8_t *unescaped_buf_ptr;
194  int unescaped_buf_size;
195  int start_code;
196  int ret;
197 
198  buf_ptr = buf;
199  buf_end = buf + buf_size;
200  jpg->got_picture = 0;
201  s->got_mxm_bitmask = 0;
202  while (buf_ptr < buf_end) {
203  start_code = ff_mjpeg_find_marker(jpg, &buf_ptr, buf_end,
204  &unescaped_buf_ptr, &unescaped_buf_size);
205  if (start_code < 0)
206  goto the_end;
207  {
208  init_get_bits(&jpg->gb, unescaped_buf_ptr, unescaped_buf_size*8);
209 
210  if (start_code >= APP0 && start_code <= APP15) {
211  mxpeg_decode_app(s, unescaped_buf_ptr, unescaped_buf_size);
212  }
213 
214  switch (start_code) {
215  case SOI:
216  if (jpg->got_picture) //emulating EOI
217  goto the_end;
218  break;
219  case EOI:
220  goto the_end;
221  case DQT:
222  ret = ff_mjpeg_decode_dqt(jpg);
223  if (ret < 0) {
224  av_log(avctx, AV_LOG_ERROR,
225  "quantization table decode error\n");
226  return ret;
227  }
228  break;
229  case DHT:
230  ret = ff_mjpeg_decode_dht(jpg);
231  if (ret < 0) {
232  av_log(avctx, AV_LOG_ERROR,
233  "huffman table decode error\n");
234  return ret;
235  }
236  break;
237  case COM:
238  ret = mxpeg_decode_com(s, unescaped_buf_ptr,
239  unescaped_buf_size);
240  if (ret < 0)
241  return ret;
242  break;
243  case SOF0:
244  s->got_sof_data = 0;
245  ret = ff_mjpeg_decode_sof(jpg);
246  if (ret < 0) {
247  av_log(avctx, AV_LOG_ERROR,
248  "SOF data decode error\n");
249  return ret;
250  }
251  if (jpg->interlaced) {
252  av_log(avctx, AV_LOG_ERROR,
253  "Interlaced mode not supported in MxPEG\n");
254  return AVERROR(EINVAL);
255  }
256  s->got_sof_data = 1;
257  break;
258  case SOS:
259  if (!s->got_sof_data) {
260  av_log(avctx, AV_LOG_WARNING,
261  "Can not process SOS without SOF data, skipping\n");
262  break;
263  }
264  if (!jpg->got_picture) {
265  if (jpg->first_picture) {
266  av_log(avctx, AV_LOG_WARNING,
267  "First picture has no SOF, skipping\n");
268  break;
269  }
270  if (!s->got_mxm_bitmask){
271  av_log(avctx, AV_LOG_WARNING,
272  "Non-key frame has no MXM, skipping\n");
273  break;
274  }
275  /* use stored SOF data to allocate current picture */
277  if ((ret = ff_get_buffer(avctx, jpg->picture_ptr,
279  return ret;
281  jpg->picture_ptr->key_frame = 0;
282  jpg->got_picture = 1;
283  } else {
285  jpg->picture_ptr->key_frame = 1;
286  }
287 
288  if (s->got_mxm_bitmask) {
289  AVFrame *reference_ptr = s->picture[s->picture_index ^ 1];
290  if (mxpeg_check_dimensions(s, jpg, reference_ptr) < 0)
291  break;
292 
293  /* allocate dummy reference picture if needed */
294  if (!reference_ptr->data[0] &&
295  (ret = ff_get_buffer(avctx, reference_ptr,
297  return ret;
298 
299  ret = ff_mjpeg_decode_sos(jpg, s->mxm_bitmask, s->bitmask_size, reference_ptr);
300  if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
301  return ret;
302  } else {
303  ret = ff_mjpeg_decode_sos(jpg, NULL, 0, NULL);
304  if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
305  return ret;
306  }
307 
308  break;
309  }
310 
311  buf_ptr += (get_bits_count(&jpg->gb)+7) >> 3;
312  }
313 
314  }
315 
316 the_end:
317  if (jpg->got_picture) {
318  int ret = av_frame_ref(data, jpg->picture_ptr);
319  if (ret < 0)
320  return ret;
321  *got_frame = 1;
322 
323  s->picture_index ^= 1;
324  jpg->picture_ptr = s->picture[s->picture_index];
325 
326  if (!s->has_complete_frame) {
327  if (!s->got_mxm_bitmask)
328  s->has_complete_frame = 1;
329  else
330  *got_frame = 0;
331  }
332  }
333 
334  return buf_ptr - buf;
335 }
336 
338  .name = "mxpeg",
339  .long_name = NULL_IF_CONFIG_SMALL("Mobotix MxPEG video"),
340  .type = AVMEDIA_TYPE_VIDEO,
341  .id = AV_CODEC_ID_MXPEG,
342  .priv_data_size = sizeof(MXpegDecodeContext),
344  .close = mxpeg_decode_end,
346  .capabilities = AV_CODEC_CAP_DR1,
347  .max_lowres = 3,
348  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
349 };
uint8_t * completion_bitmask
Definition: mxpegdec.c:41
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
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:182
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:167
int size
Definition: avcodec.h:1658
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
static int mxpeg_check_dimensions(MXpegDecodeContext *s, MJpegDecodeContext *jpg, AVFrame *reference_ptr)
Definition: mxpegdec.c:159
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:87
int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
Definition: mjpegdec.c:208
AVCodec.
Definition: avcodec.h:3681
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:87
MJPEG encoder and decoder.
static int mxpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: mxpegdec.c:184
#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:82
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:150
AVCodec ff_mxpeg_decoder
Definition: mxpegdec.c:337
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:388
AVFrame * picture_ptr
Definition: mjpegdec.h:97
uint8_t * data
Definition: avcodec.h:1657
#define MAX_COMPONENTS
Definition: mjpegdec.h:42
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:199
#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:176
static int mxpeg_decode_mxm(MXpegDecodeContext *s, const uint8_t *buf_ptr, int buf_size)
Definition: mxpegdec.c:91
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition: mjpegdec.c:2495
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:179
const char * name
Name of the codec implementation.
Definition: avcodec.h:3688
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:267
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:261
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:3020
#define FFMIN(a, b)
Definition: common.h:96
static const uint8_t start_code[]
Definition: h264dec.c:530
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: avcodec.h:3031
GetBitContext gb
Definition: mjpegdec.h:47
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:218
int has_complete_frame
Definition: mxpegdec.c:40
main external API structure.
Definition: avcodec.h:1732
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:953
void * buf
Definition: avisynth_c.h:690
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:306
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:425
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:498
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:201
static int mxpeg_decode_com(MXpegDecodeContext *s, const uint8_t *buf_ptr, int buf_size)
Definition: mxpegdec.c:144
int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask, int mb_bitmask_size, const AVFrame *reference)
Definition: mjpegdec.c:1468
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition: mjpegdec.c:110
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:46
void * priv_data
Definition: avcodec.h:1774
static int mxpeg_decode_app(MXpegDecodeContext *s, const uint8_t *buf_ptr, int buf_size)
Definition: mxpegdec.c:79
int got_picture
we found a SOF and picture is valid, too.
Definition: mjpegdec.h:98
int len
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:256
#define av_freep(p)
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: ffmpeg.c:2257
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:1958
MJPEG decoder.
This structure stores compressed data.
Definition: avcodec.h:1634
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:1389
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:994
Predicted.
Definition: avutil.h:275