Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 
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  av_freep(&s->mxm_bitmask);
59 
60  return 0;
61 }
62 
64 {
65  MXpegDecodeContext *s = avctx->priv_data;
66 
67  s->picture[0] = av_frame_alloc();
68  s->picture[1] = av_frame_alloc();
69  if (!s->picture[0] || !s->picture[1]) {
70  mxpeg_decode_end(avctx);
71  return AVERROR(ENOMEM);
72  }
73 
74  s->jpg.picture_ptr = s->picture[0];
75  return ff_mjpeg_decode_init(avctx);
76 }
77 
79  const uint8_t *buf_ptr, int buf_size)
80 {
81  int len;
82  if (buf_size < 2)
83  return 0;
84  len = AV_RB16(buf_ptr);
85  skip_bits(&s->jpg.gb, 8*FFMIN(len,buf_size));
86 
87  return 0;
88 }
89 
91  const uint8_t *buf_ptr, int buf_size)
92 {
93  unsigned bitmask_size, mb_count;
94  int i;
95 
96  s->mb_width = AV_RL16(buf_ptr+4);
97  s->mb_height = AV_RL16(buf_ptr+6);
98  mb_count = s->mb_width * s->mb_height;
99 
100  bitmask_size = (mb_count + 7) >> 3;
101  if (bitmask_size > buf_size - 12) {
103  "MXM bitmask is not complete\n");
104  return AVERROR(EINVAL);
105  }
106 
107  if (s->bitmask_size != bitmask_size) {
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 
124  s->bitmask_size = bitmask_size;
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  while (buf_ptr < buf_end) {
201  start_code = ff_mjpeg_find_marker(jpg, &buf_ptr, buf_end,
202  &unescaped_buf_ptr, &unescaped_buf_size);
203  if (start_code < 0)
204  goto the_end;
205  {
206  init_get_bits(&jpg->gb, unescaped_buf_ptr, unescaped_buf_size*8);
207 
208  if (start_code >= APP0 && start_code <= APP15) {
209  mxpeg_decode_app(s, unescaped_buf_ptr, unescaped_buf_size);
210  }
211 
212  switch (start_code) {
213  case SOI:
214  if (jpg->got_picture) //emulating EOI
215  goto the_end;
216  break;
217  case EOI:
218  goto the_end;
219  case DQT:
220  ret = ff_mjpeg_decode_dqt(jpg);
221  if (ret < 0) {
222  av_log(avctx, AV_LOG_ERROR,
223  "quantization table decode error\n");
224  return ret;
225  }
226  break;
227  case DHT:
228  ret = ff_mjpeg_decode_dht(jpg);
229  if (ret < 0) {
230  av_log(avctx, AV_LOG_ERROR,
231  "huffman table decode error\n");
232  return ret;
233  }
234  break;
235  case COM:
236  ret = mxpeg_decode_com(s, unescaped_buf_ptr,
237  unescaped_buf_size);
238  if (ret < 0)
239  return ret;
240  break;
241  case SOF0:
242  s->got_sof_data = 0;
243  ret = ff_mjpeg_decode_sof(jpg);
244  if (ret < 0) {
245  av_log(avctx, AV_LOG_ERROR,
246  "SOF data decode error\n");
247  return ret;
248  }
249  if (jpg->interlaced) {
250  av_log(avctx, AV_LOG_ERROR,
251  "Interlaced mode not supported in MxPEG\n");
252  return AVERROR(EINVAL);
253  }
254  s->got_sof_data = 1;
255  break;
256  case SOS:
257  if (!s->got_sof_data) {
258  av_log(avctx, AV_LOG_WARNING,
259  "Can not process SOS without SOF data, skipping\n");
260  break;
261  }
262  if (!jpg->got_picture) {
263  if (jpg->first_picture) {
264  av_log(avctx, AV_LOG_WARNING,
265  "First picture has no SOF, skipping\n");
266  break;
267  }
268  if (!s->got_mxm_bitmask){
269  av_log(avctx, AV_LOG_WARNING,
270  "Non-key frame has no MXM, skipping\n");
271  break;
272  }
273  /* use stored SOF data to allocate current picture */
275  if (ff_get_buffer(avctx, jpg->picture_ptr,
276  AV_GET_BUFFER_FLAG_REF) < 0) {
277  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
278  return AVERROR(ENOMEM);
279  }
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  ff_get_buffer(avctx, reference_ptr,
296  AV_GET_BUFFER_FLAG_REF) < 0) {
297  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
298  return AVERROR(ENOMEM);
299  }
300 
301  ret = ff_mjpeg_decode_sos(jpg, s->mxm_bitmask, reference_ptr);
302  if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
303  return ret;
304  } else {
305  ret = ff_mjpeg_decode_sos(jpg, NULL, NULL);
306  if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
307  return ret;
308  }
309 
310  break;
311  }
312 
313  buf_ptr += (get_bits_count(&jpg->gb)+7) >> 3;
314  }
315 
316  }
317 
318 the_end:
319  if (jpg->got_picture) {
320  int ret = av_frame_ref(data, jpg->picture_ptr);
321  if (ret < 0)
322  return ret;
323  *got_frame = 1;
324 
325  s->picture_index ^= 1;
326  jpg->picture_ptr = s->picture[s->picture_index];
327 
328  if (!s->has_complete_frame) {
329  if (!s->got_mxm_bitmask)
330  s->has_complete_frame = 1;
331  else
332  *got_frame = 0;
333  }
334  }
335 
336  return buf_ptr - buf;
337 }
338 
340  .name = "mxpeg",
341  .long_name = NULL_IF_CONFIG_SMALL("Mobotix MxPEG video"),
342  .type = AVMEDIA_TYPE_VIDEO,
343  .id = AV_CODEC_ID_MXPEG,
344  .priv_data_size = sizeof(MXpegDecodeContext),
348  .capabilities = CODEC_CAP_DR1,
349 };
uint8_t * completion_bitmask
Definition: mxpegdec.c:41
Definition: mjpeg.h:116
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
uint8_t * mxm_bitmask
Definition: mxpegdec.c:38
int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
Definition: mjpegdec.c:134
int size
Definition: avcodec.h:974
Definition: mjpeg.h:75
#define AV_RL16
Definition: intreadwrite.h:42
static int mxpeg_check_dimensions(MXpegDecodeContext *s, MJpegDecodeContext *jpg, AVFrame *reference_ptr)
Definition: mxpegdec.c:157
int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
Definition: mjpegdec.c:167
AVCodec.
Definition: avcodec.h:2812
MJPEG encoder and decoder.
static int mxpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: mxpegdec.c:182
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
uint8_t
#define av_cold
Definition: attributes.h:66
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
AVCodec ff_mxpeg_decoder
Definition: mxpegdec.c:339
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:188
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
AVFrame * picture_ptr
Definition: mjpegdec.h:90
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
#define MAX_COMPONENTS
Definition: mjpegdec.h:41
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:194
Definition: mjpeg.h:78
Definition: mjpeg.h:84
AVFrame * picture[2]
Definition: mxpegdec.c:34
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
static int mxpeg_decode_mxm(MXpegDecodeContext *s, const uint8_t *buf_ptr, int buf_size)
Definition: mxpegdec.c:90
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
Definition: mjpeg.h:44
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:69
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition: mjpegdec.c:1678
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:145
Definition: mjpeg.h:61
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
unsigned mb_width
Definition: mxpegdec.c:42
int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
Definition: mjpegdec.c:221
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2422
#define FFMIN(a, b)
Definition: common.h:57
Definition: mjpeg.h:77
#define AV_EF_EXPLODE
Definition: avcodec.h:2433
GetBitContext gb
Definition: mjpegdec.h:46
Definition: mjpeg.h:76
static av_cold int mxpeg_decode_init(AVCodecContext *avctx)
Definition: mxpegdec.c:63
NULL
Definition: eval.c:55
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
int has_complete_frame
Definition: mxpegdec.c:40
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:263
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:375
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:283
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
static int mxpeg_decode_com(MXpegDecodeContext *s, const uint8_t *buf_ptr, int buf_size)
Definition: mxpegdec.c:142
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition: mjpegdec.c:85
int ff_mjpeg_decode_sos(MJpegDecodeContext *s, const uint8_t *mb_bitmask, const AVFrame *reference)
Definition: mjpegdec.c:1022
unsigned mb_height
Definition: mxpegdec.c:42
common internal api header.
static av_cold int mxpeg_decode_end(AVCodecContext *avctx)
Definition: mxpegdec.c:45
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
AVCodecContext * avctx
Definition: mjpegdec.h:45
void * priv_data
Definition: avcodec.h:1092
static int mxpeg_decode_app(MXpegDecodeContext *s, const uint8_t *buf_ptr, int buf_size)
Definition: mxpegdec.c:78
int got_picture
we found a SOF and picture is valid, too.
Definition: mjpegdec.h:91
int len
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:191
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:1391
MJPEG decoder.
This structure stores compressed data.
Definition: avcodec.h:950
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition: avcodec.h:850
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
Predicted.
Definition: avutil.h:254
Definition: mjpeg.h:99