FFmpeg
Loading...
Searching...
No Matches
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 "libavutil/mem.h"
29#include "codec_internal.h"
30#include "decode.h"
31#include "hpeldsp.h"
32#include "mjpeg.h"
33#include "mjpegdec.h"
34
35typedef struct MXpegDecodeContext {
37 AVFrame *picture[2]; /* pictures array */
38 int picture_index; /* index of current picture */
39 int got_sof_data; /* true if SOF data successfully parsed */
40 int got_mxm_bitmask; /* true if MXM bitmask available */
41 uint8_t *mxm_bitmask; /* bitmask buffer */
42 unsigned bitmask_size; /* size of bitmask */
43 int has_complete_frame; /* true if has complete frame */
44 uint8_t *completion_bitmask; /* completion bitmask of macroblocks */
45 unsigned mb_width, mb_height; /* size of picture in MB's from MXM header */
47
49{
51 int i;
52
54
55 for (i = 0; i < 2; ++i)
56 av_frame_free(&s->picture[i]);
57
58 s->bitmask_size = 0;
59 av_freep(&s->mxm_bitmask);
60 av_freep(&s->completion_bitmask);
61
62 return 0;
63}
64
66{
68 HpelDSPContext hdsp;
69
70 ff_hpeldsp_init(&hdsp, avctx->flags);
71 s->jpg.copy_block = hdsp.put_pixels_tab[1][0];
72
73 s->picture[0] = av_frame_alloc();
74 s->picture[1] = av_frame_alloc();
75 if (!s->picture[0] || !s->picture[1])
76 return AVERROR(ENOMEM);
77
78 s->jpg.picture_ptr = s->picture[0];
79 return ff_mjpeg_decode_init(avctx);
80}
81
83 const uint8_t *buf_ptr, int buf_size)
84{
85 int len;
86 if (buf_size < 2)
87 return 0;
88 len = AV_RB16(buf_ptr);
89 bytestream2_skipu(&s->jpg.gB, FFMIN(len, buf_size));
90
91 return 0;
92}
93
95 const uint8_t *buf_ptr, int buf_size)
96{
97 unsigned bitmask_size, mb_count;
98 int i;
99
100 s->mb_width = AV_RL16(buf_ptr+4);
101 s->mb_height = AV_RL16(buf_ptr+6);
102 mb_count = s->mb_width * s->mb_height;
103
104 bitmask_size = (mb_count + 7) >> 3;
105 if (bitmask_size > buf_size - 12) {
106 av_log(s->jpg.avctx, AV_LOG_ERROR,
107 "MXM bitmask is not complete\n");
108 return AVERROR(EINVAL);
109 }
110
111 if (s->bitmask_size != bitmask_size) {
112 s->bitmask_size = 0;
113 av_freep(&s->mxm_bitmask);
114 s->mxm_bitmask = av_malloc(bitmask_size);
115 if (!s->mxm_bitmask) {
116 av_log(s->jpg.avctx, AV_LOG_ERROR,
117 "MXM bitmask memory allocation error\n");
118 return AVERROR(ENOMEM);
119 }
120
121 av_freep(&s->completion_bitmask);
122 s->completion_bitmask = av_mallocz(bitmask_size);
123 if (!s->completion_bitmask) {
124 av_log(s->jpg.avctx, AV_LOG_ERROR,
125 "Completion bitmask memory allocation error\n");
126 return AVERROR(ENOMEM);
127 }
128
129 s->bitmask_size = bitmask_size;
130 }
131
132 memcpy(s->mxm_bitmask, buf_ptr + 12, bitmask_size);
133 s->got_mxm_bitmask = 1;
134
135 if (!s->has_complete_frame) {
136 uint8_t completion_check = 0xFF;
137 for (i = 0; i < bitmask_size; ++i) {
138 s->completion_bitmask[i] |= s->mxm_bitmask[i];
139 completion_check &= s->completion_bitmask[i];
140 }
141 s->has_complete_frame = !(completion_check ^ 0xFF);
142 }
143
144 return 0;
145}
146
148 const uint8_t *buf_ptr, int buf_size)
149{
150 int len, ret = 0;
151 if (buf_size < 2)
152 return 0;
153 len = AV_RB16(buf_ptr);
154 if (len > 14 && len <= buf_size && !strncmp(buf_ptr + 2, "MXM", 3)) {
155 ret = mxpeg_decode_mxm(s, buf_ptr + 2, len - 2);
156 }
157 bytestream2_skipu(&s->jpg.gB, FFMIN(len, buf_size));
158
159 return ret;
160}
161
163 AVFrame *reference_ptr)
164{
165 if ((jpg->width + 0x0F)>>4 != s->mb_width ||
166 (jpg->height + 0x0F)>>4 != s->mb_height) {
168 "Picture dimensions stored in SOF and MXM mismatch\n");
169 return AVERROR(EINVAL);
170 }
171
172 if (reference_ptr->data[0]) {
173 int i;
174 for (i = 0; i < MAX_COMPONENTS; ++i) {
175 if ( (!reference_ptr->data[i] ^ !jpg->picture_ptr->data[i]) ||
176 reference_ptr->linesize[i] != jpg->picture_ptr->linesize[i]) {
178 "Dimensions of current and reference picture mismatch\n");
179 return AVERROR(EINVAL);
180 }
181 }
182 if (reference_ptr->width != jpg->picture_ptr->width ||
183 reference_ptr->height != jpg->picture_ptr->height ||
184 reference_ptr->format != jpg->picture_ptr->format) {
185 av_log(jpg->avctx, AV_LOG_ERROR, "Reference mismatching\n");
186 return AVERROR_INVALIDDATA;
187 }
188 }
189
190 return 0;
191}
192
193static int mxpeg_decode_frame(AVCodecContext *avctx, AVFrame *rframe,
194 int *got_frame, AVPacket *avpkt)
195{
196 const uint8_t *buf = avpkt->data;
197 int buf_size = avpkt->size;
199 MJpegDecodeContext *jpg = &s->jpg;
200 const uint8_t *buf_end, *buf_ptr;
201 int start_code;
202 int ret;
203
204 if (avctx->skip_frame == AVDISCARD_ALL)
206
207 buf_ptr = buf;
208 buf_end = buf + buf_size;
209 jpg->got_picture = 0;
210 s->got_mxm_bitmask = 0;
211 s->got_sof_data = !!s->got_sof_data;
212 while (buf_ptr < buf_end) {
213 start_code = ff_mjpeg_find_marker(&buf_ptr, buf_end);
214 if (start_code < 0)
215 goto the_end;
216
217 int bytes_left = buf_end - buf_ptr;
218 bytestream2_init(&jpg->gB, buf_ptr, bytes_left);
219
220 if (start_code >= APP0 && start_code <= APP15) {
221 mxpeg_decode_app(s, buf_ptr, bytes_left);
222 }
223
224 switch (start_code) {
225 case SOI:
226 if (jpg->got_picture) //emulating EOI
227 goto the_end;
228 break;
229 case EOI:
230 goto the_end;
231 case DQT:
232 ret = ff_mjpeg_decode_dqt(jpg);
233 if (ret < 0) {
234 av_log(avctx, AV_LOG_ERROR,
235 "quantization table decode error\n");
236 return ret;
237 }
238 break;
239 case DHT:
240 ret = ff_mjpeg_decode_dht(jpg);
241 if (ret < 0) {
242 av_log(avctx, AV_LOG_ERROR,
243 "huffman table decode error\n");
244 return ret;
245 }
246 break;
247 case COM:
248 ret = mxpeg_decode_com(s, buf_ptr, bytes_left);
249 if (ret < 0)
250 return ret;
251 break;
252 case SOF0:
253 if (s->got_sof_data > 1) {
254 av_log(avctx, AV_LOG_ERROR,
255 "Multiple SOF in a frame\n");
256 return AVERROR_INVALIDDATA;
257 }
258 ret = ff_mjpeg_decode_sof(jpg);
259 if (ret < 0) {
260 av_log(avctx, AV_LOG_ERROR,
261 "SOF data decode error\n");
262 s->got_sof_data = 0;
263 return ret;
264 }
265 if (jpg->interlaced) {
266 av_log(avctx, AV_LOG_ERROR,
267 "Interlaced mode not supported in MxPEG\n");
268 s->got_sof_data = 0;
269 return AVERROR(EINVAL);
270 }
271 s->got_sof_data ++;
272 break;
273 case SOS:
274 if (!s->got_sof_data) {
275 av_log(avctx, AV_LOG_WARNING,
276 "Can not process SOS without SOF data, skipping\n");
277 break;
278 }
279 if (!jpg->got_picture) {
280 if (jpg->first_picture) {
281 av_log(avctx, AV_LOG_WARNING,
282 "First picture has no SOF, skipping\n");
283 break;
284 }
285 if (!s->got_mxm_bitmask){
286 av_log(avctx, AV_LOG_WARNING,
287 "Non-key frame has no MXM, skipping\n");
288 break;
289 }
290 /* use stored SOF data to allocate current picture */
292 if ((ret = ff_get_buffer(avctx, jpg->picture_ptr,
294 return ret;
297 jpg->got_picture = 1;
298 } else {
301 }
302
303 if (s->got_mxm_bitmask) {
304 AVFrame *reference_ptr = s->picture[s->picture_index ^ 1];
305 if (mxpeg_check_dimensions(s, jpg, reference_ptr) < 0)
306 break;
307
308 /* allocate dummy reference picture if needed */
309 if (!reference_ptr->data[0] &&
310 (ret = ff_get_buffer(avctx, reference_ptr,
312 return ret;
313
314 jpg->mb_bitmask = s->mxm_bitmask;
315 jpg->mb_bitmask_size = s->bitmask_size;
316 jpg->reference = reference_ptr;
317 ret = ff_mjpeg_decode_sos(jpg);
318 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
319 return ret;
320 } else {
321 jpg->mb_bitmask = NULL;
322 jpg->reference = NULL;
323 ret = ff_mjpeg_decode_sos(jpg);
324 if (ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
325 return ret;
326 }
327
328 break;
329 }
330
331 buf_ptr += bytestream2_tell(&jpg->gB);
332 }
333
334the_end:
335 if (jpg->got_picture) {
336 int ret = av_frame_ref(rframe, jpg->picture_ptr);
337 if (ret < 0)
338 return ret;
339 *got_frame = 1;
340
341 s->picture_index ^= 1;
342 jpg->picture_ptr = s->picture[s->picture_index];
343
344 if (!s->has_complete_frame) {
345 if (!s->got_mxm_bitmask)
346 s->has_complete_frame = 1;
347 else
348 *got_frame = 0;
349 }
350 }
351
352 return buf_ptr - buf;
353}
354
356 .p.name = "mxpeg",
357 CODEC_LONG_NAME("Mobotix MxPEG video"),
358 .p.type = AVMEDIA_TYPE_VIDEO,
359 .p.id = AV_CODEC_ID_MXPEG,
360 .priv_data_size = sizeof(MXpegDecodeContext),
364 .p.capabilities = AV_CODEC_CAP_DR1,
365 .p.max_lowres = 3,
366 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
367};
const FFCodec ff_mxpeg_decoder
Definition mxpegdec.c:355
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition bytestream.h:174
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
static av_always_inline int bytestream2_tell(const GetByteContext *g)
Definition bytestream.h:192
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define NULL
Definition coverity.c:32
#define EOI
End Of Image marker.
Definition cpia.c:43
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition defs.h:51
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_GET_BUFFER_FLAG_REF
The decoder will keep a reference to the frame and may reuse it later.
Definition avcodec.h:415
@ AV_CODEC_ID_MXPEG
Definition codec_id.h:196
@ AVDISCARD_ALL
discard all
Definition defs.h:232
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition frame.h:687
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:496
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:278
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
Half-pel DSP functions.
#define AV_RL16(p)
#define AV_RB16(p)
av_cold void ff_hpeldsp_init(HpelDSPContext *c, int flags)
Definition hpeldsp.c:337
#define av_cold
Definition attributes.h:117
#define FFMIN(a, b)
Definition macros.h:49
Memory handling functions.
MJPEG encoder and decoder.
@ APP15
Definition mjpeg.h:94
@ DQT
Definition mjpeg.h:73
@ DHT
Definition mjpeg.h:56
@ SOS
Definition mjpeg.h:72
@ APP0
Definition mjpeg.h:79
@ SOI
Definition mjpeg.h:70
@ COM
Definition mjpeg.h:111
@ SOF0
Definition mjpeg.h:39
av_cold int ff_mjpeg_decode_init(AVCodecContext *avctx)
Definition mjpegdec.c:124
int ff_mjpeg_decode_sos(MJpegDecodeContext *s)
Definition mjpegdec.c:1680
int ff_mjpeg_decode_dqt(MJpegDecodeContext *s)
Definition mjpegdec.c:208
av_cold int ff_mjpeg_decode_end(AVCodecContext *avctx)
Definition mjpegdec.c:2900
int ff_mjpeg_decode_sof(MJpegDecodeContext *s)
Definition mjpegdec.c:310
int ff_mjpeg_find_marker(const uint8_t **pbuf_ptr, const uint8_t *buf_end)
Definition mjpegdec.c:2195
int ff_mjpeg_decode_dht(MJpegDecodeContext *s)
Definition mjpegdec.c:251
MJPEG decoder.
#define MAX_COMPONENTS
Definition mjpegdec.h:47
static av_cold int mxpeg_decode_init(AVCodecContext *avctx)
Definition mxpegdec.c:65
static int mxpeg_check_dimensions(MXpegDecodeContext *s, MJpegDecodeContext *jpg, AVFrame *reference_ptr)
Definition mxpegdec.c:162
static int mxpeg_decode_com(MXpegDecodeContext *s, const uint8_t *buf_ptr, int buf_size)
Definition mxpegdec.c:147
static int mxpeg_decode_frame(AVCodecContext *avctx, AVFrame *rframe, int *got_frame, AVPacket *avpkt)
Definition mxpegdec.c:193
static int mxpeg_decode_mxm(MXpegDecodeContext *s, const uint8_t *buf_ptr, int buf_size)
Definition mxpegdec.c:94
static int mxpeg_decode_app(MXpegDecodeContext *s, const uint8_t *buf_ptr, int buf_size)
Definition mxpegdec.c:82
static av_cold int mxpeg_decode_end(AVCodecContext *avctx)
Definition mxpegdec.c:48
#define av_malloc(s)
Definition ops_static.c:52
main external API structure.
Definition avcodec.h:443
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
void * priv_data
Definition avcodec.h:470
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition avcodec.h:1667
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition avcodec.h:1416
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int width
Definition frame.h:544
int height
Definition frame.h:544
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition frame.h:716
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:559
enum AVPictureType pict_type
Picture type of the frame.
Definition frame.h:564
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
Half-pel DSP context.
Definition hpeldsp.h:46
op_pixels_func put_pixels_tab[4][4]
Halfpel motion compensation with rounding (a+b+1)>>1.
Definition hpeldsp.h:57
const AVFrame * reference
Definition mjpegdec.h:180
AVFrame * picture_ptr
Definition mjpegdec.h:118
const uint8_t * mb_bitmask
Definition mjpegdec.h:178
GetByteContext gB
Definition mjpegdec.h:60
int got_picture
we found a SOF and picture is valid, too.
Definition mjpegdec.h:119
size_t mb_bitmask_size
Definition mjpegdec.h:179
AVCodecContext * avctx
Definition mjpegdec.h:58
MJpegDecodeContext jpg
Definition mxpegdec.c:36
uint8_t * mxm_bitmask
Definition mxpegdec.c:41
AVFrame * picture[2]
Definition mxpegdec.c:37
unsigned bitmask_size
Definition mxpegdec.c:42
unsigned mb_width
Definition mxpegdec.c:45
uint8_t * completion_bitmask
Definition mxpegdec.c:44
unsigned mb_height
Definition mxpegdec.c:45
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static const uint8_t start_code[]
int len