FFmpeg  4.2.3
cbs_jpeg.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "cbs.h"
20 #include "cbs_internal.h"
21 #include "cbs_jpeg.h"
22 
23 
24 #define HEADER(name) do { \
25  ff_cbs_trace_header(ctx, name); \
26  } while (0)
27 
28 #define CHECK(call) do { \
29  err = (call); \
30  if (err < 0) \
31  return err; \
32  } while (0)
33 
34 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
35 
36 #define u(width, name, range_min, range_max) \
37  xu(width, name, range_min, range_max, 0)
38 #define us(width, name, sub, range_min, range_max) \
39  xu(width, name, range_min, range_max, 1, sub)
40 
41 
42 #define READ
43 #define READWRITE read
44 #define RWContext GetBitContext
45 #define FUNC(name) cbs_jpeg_read_ ## name
46 
47 #define xu(width, name, range_min, range_max, subs, ...) do { \
48  uint32_t value; \
49  CHECK(ff_cbs_read_unsigned(ctx, rw, width, #name, \
50  SUBSCRIPTS(subs, __VA_ARGS__), \
51  &value, range_min, range_max)); \
52  current->name = value; \
53  } while (0)
54 
56 
57 #undef READ
58 #undef READWRITE
59 #undef RWContext
60 #undef FUNC
61 #undef xu
62 
63 #define WRITE
64 #define READWRITE write
65 #define RWContext PutBitContext
66 #define FUNC(name) cbs_jpeg_write_ ## name
67 
68 #define xu(width, name, range_min, range_max, subs, ...) do { \
69  uint32_t value = current->name; \
70  CHECK(ff_cbs_write_unsigned(ctx, rw, width, #name, \
71  SUBSCRIPTS(subs, __VA_ARGS__), \
72  value, range_min, range_max)); \
73  } while (0)
74 
75 
77 
78 #undef WRITE
79 #undef READWRITE
80 #undef RWContext
81 #undef FUNC
82 #undef xu
83 
84 
85 static void cbs_jpeg_free_application_data(void *unit, uint8_t *content)
86 {
88  av_buffer_unref(&ad->Ap_ref);
89  av_freep(&content);
90 }
91 
92 static void cbs_jpeg_free_comment(void *unit, uint8_t *content)
93 {
95  av_buffer_unref(&comment->Cm_ref);
96  av_freep(&content);
97 }
98 
99 static void cbs_jpeg_free_scan(void *unit, uint8_t *content)
100 {
101  JPEGRawScan *scan = (JPEGRawScan*)content;
102  av_buffer_unref(&scan->data_ref);
103  av_freep(&content);
104 }
105 
108  int header)
109 {
110  AVBufferRef *data_ref;
111  uint8_t *data;
112  size_t data_size;
113  int unit, start, end, marker, next_start, next_marker;
114  int err, i, j, length;
115 
116  if (frag->data_size < 4) {
117  // Definitely too short to be meaningful.
118  return AVERROR_INVALIDDATA;
119  }
120 
121  for (i = 0; i + 1 < frag->data_size && frag->data[i] != 0xff; i++);
122  if (i > 0) {
123  av_log(ctx->log_ctx, AV_LOG_WARNING, "Discarding %d bytes at "
124  "beginning of image.\n", i);
125  }
126  for (++i; i + 1 < frag->data_size && frag->data[i] == 0xff; i++);
127  if (i + 1 >= frag->data_size && frag->data[i]) {
128  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid JPEG image: "
129  "no SOI marker found.\n");
130  return AVERROR_INVALIDDATA;
131  }
132  marker = frag->data[i];
133  if (marker != JPEG_MARKER_SOI) {
134  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid JPEG image: first "
135  "marker is %02x, should be SOI.\n", marker);
136  return AVERROR_INVALIDDATA;
137  }
138  for (++i; i + 1 < frag->data_size && frag->data[i] == 0xff; i++);
139  if (i + 1 >= frag->data_size) {
140  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid JPEG image: "
141  "no image content found.\n");
142  return AVERROR_INVALIDDATA;
143  }
144  marker = frag->data[i];
145  start = i + 1;
146 
147  for (unit = 0;; unit++) {
148  if (marker == JPEG_MARKER_EOI) {
149  break;
150  } else if (marker == JPEG_MARKER_SOS) {
151  next_marker = -1;
152  for (i = start; i + 1 < frag->data_size; i++) {
153  if (frag->data[i] != 0xff)
154  continue;
155  end = i;
156  for (++i; i + 1 < frag->data_size &&
157  frag->data[i] == 0xff; i++);
158  if (i + 1 < frag->data_size) {
159  if (frag->data[i] == 0x00)
160  continue;
161  next_marker = frag->data[i];
162  next_start = i + 1;
163  }
164  break;
165  }
166  } else {
167  i = start;
168  if (i + 2 > frag->data_size) {
169  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid JPEG image: "
170  "truncated at %02x marker.\n", marker);
171  return AVERROR_INVALIDDATA;
172  }
173  length = AV_RB16(frag->data + i);
174  if (i + length > frag->data_size) {
175  av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid JPEG image: "
176  "truncated at %02x marker segment.\n", marker);
177  return AVERROR_INVALIDDATA;
178  }
179  end = start + length;
180 
181  i = end;
182  if (frag->data[i] != 0xff) {
183  next_marker = -1;
184  } else {
185  for (++i; i + 1 < frag->data_size &&
186  frag->data[i] == 0xff; i++);
187  if (i + 1 >= frag->data_size) {
188  next_marker = -1;
189  } else {
190  next_marker = frag->data[i];
191  next_start = i + 1;
192  }
193  }
194  }
195 
196  if (marker == JPEG_MARKER_SOS) {
197  length = AV_RB16(frag->data + start);
198 
199  if (length > end - start)
200  return AVERROR_INVALIDDATA;
201 
202  data_ref = NULL;
203  data = av_malloc(end - start +
205  if (!data)
206  return AVERROR(ENOMEM);
207 
208  memcpy(data, frag->data + start, length);
209  for (i = start + length, j = length; i < end; i++, j++) {
210  if (frag->data[i] == 0xff) {
211  while (frag->data[i] == 0xff)
212  ++i;
213  data[j] = 0xff;
214  } else {
215  data[j] = frag->data[i];
216  }
217  }
218  data_size = j;
219 
220  memset(data + data_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
221 
222  } else {
223  data = frag->data + start;
224  data_size = end - start;
225  data_ref = frag->data_ref;
226  }
227 
228  err = ff_cbs_insert_unit_data(ctx, frag, unit, marker,
229  data, data_size, data_ref);
230  if (err < 0) {
231  if (!data_ref)
232  av_freep(&data);
233  return err;
234  }
235 
236  if (next_marker == -1)
237  break;
238  marker = next_marker;
239  start = next_start;
240  }
241 
242  return 0;
243 }
244 
246  CodedBitstreamUnit *unit)
247 {
248  GetBitContext gbc;
249  int err;
250 
251  err = init_get_bits(&gbc, unit->data, 8 * unit->data_size);
252  if (err < 0)
253  return err;
254 
255  if (unit->type >= JPEG_MARKER_SOF0 &&
256  unit->type <= JPEG_MARKER_SOF3) {
257  err = ff_cbs_alloc_unit_content(ctx, unit,
258  sizeof(JPEGRawFrameHeader),
259  NULL);
260  if (err < 0)
261  return err;
262 
263  err = cbs_jpeg_read_frame_header(ctx, &gbc, unit->content);
264  if (err < 0)
265  return err;
266 
267  } else if (unit->type >= JPEG_MARKER_APPN &&
268  unit->type <= JPEG_MARKER_APPN + 15) {
269  err = ff_cbs_alloc_unit_content(ctx, unit,
270  sizeof(JPEGRawApplicationData),
272  if (err < 0)
273  return err;
274 
275  err = cbs_jpeg_read_application_data(ctx, &gbc, unit->content);
276  if (err < 0)
277  return err;
278 
279  } else if (unit->type == JPEG_MARKER_SOS) {
280  JPEGRawScan *scan;
281  int pos;
282 
283  err = ff_cbs_alloc_unit_content(ctx, unit,
284  sizeof(JPEGRawScan),
286  if (err < 0)
287  return err;
288  scan = unit->content;
289 
290  err = cbs_jpeg_read_scan_header(ctx, &gbc, &scan->header);
291  if (err < 0)
292  return err;
293 
294  pos = get_bits_count(&gbc);
295  av_assert0(pos % 8 == 0);
296  if (pos > 0) {
297  scan->data_size = unit->data_size - pos / 8;
298  scan->data_ref = av_buffer_ref(unit->data_ref);
299  if (!scan->data_ref)
300  return AVERROR(ENOMEM);
301  scan->data = unit->data + pos / 8;
302  }
303 
304  } else {
305  switch (unit->type) {
306 #define SEGMENT(marker, type, func, free) \
307  case JPEG_MARKER_ ## marker: \
308  { \
309  err = ff_cbs_alloc_unit_content(ctx, unit, \
310  sizeof(type), free); \
311  if (err < 0) \
312  return err; \
313  err = cbs_jpeg_read_ ## func(ctx, &gbc, unit->content); \
314  if (err < 0) \
315  return err; \
316  } \
317  break
321 #undef SEGMENT
322  default:
323  return AVERROR(ENOSYS);
324  }
325  }
326 
327  return 0;
328 }
329 
331  CodedBitstreamUnit *unit,
332  PutBitContext *pbc)
333 {
334  JPEGRawScan *scan = unit->content;
335  int i, err;
336 
337  err = cbs_jpeg_write_scan_header(ctx, pbc, &scan->header);
338  if (err < 0)
339  return err;
340 
341  if (scan->data) {
342  if (scan->data_size * 8 > put_bits_left(pbc))
343  return AVERROR(ENOSPC);
344 
345  for (i = 0; i < scan->data_size; i++)
346  put_bits(pbc, 8, scan->data[i]);
347  }
348 
349  return 0;
350 }
351 
353  CodedBitstreamUnit *unit,
354  PutBitContext *pbc)
355 {
356  int err;
357 
358  if (unit->type >= JPEG_MARKER_SOF0 &&
359  unit->type <= JPEG_MARKER_SOF3) {
360  err = cbs_jpeg_write_frame_header(ctx, pbc, unit->content);
361  } else if (unit->type >= JPEG_MARKER_APPN &&
362  unit->type <= JPEG_MARKER_APPN + 15) {
363  err = cbs_jpeg_write_application_data(ctx, pbc, unit->content);
364  } else {
365  switch (unit->type) {
366 #define SEGMENT(marker, func) \
367  case JPEG_MARKER_ ## marker: \
368  err = cbs_jpeg_write_ ## func(ctx, pbc, unit->content); \
369  break;
370  SEGMENT(DQT, dqt);
371  SEGMENT(DHT, dht);
372  SEGMENT(COM, comment);
373  default:
374  return AVERROR_PATCHWELCOME;
375  }
376  }
377 
378  return err;
379 }
380 
382  CodedBitstreamUnit *unit,
383  PutBitContext *pbc)
384 {
385  if (unit->type == JPEG_MARKER_SOS)
386  return cbs_jpeg_write_scan (ctx, unit, pbc);
387  else
388  return cbs_jpeg_write_segment(ctx, unit, pbc);
389 }
390 
393 {
394  const CodedBitstreamUnit *unit;
395  uint8_t *data;
396  size_t size, dp, sp;
397  int i;
398 
399  size = 4; // SOI + EOI.
400  for (i = 0; i < frag->nb_units; i++) {
401  unit = &frag->units[i];
402  size += 2 + unit->data_size;
403  if (unit->type == JPEG_MARKER_SOS) {
404  for (sp = 0; sp < unit->data_size; sp++) {
405  if (unit->data[sp] == 0xff)
406  ++size;
407  }
408  }
409  }
410 
412  if (!frag->data_ref)
413  return AVERROR(ENOMEM);
414  data = frag->data_ref->data;
415 
416  dp = 0;
417 
418  data[dp++] = 0xff;
419  data[dp++] = JPEG_MARKER_SOI;
420 
421  for (i = 0; i < frag->nb_units; i++) {
422  unit = &frag->units[i];
423 
424  data[dp++] = 0xff;
425  data[dp++] = unit->type;
426 
427  if (unit->type != JPEG_MARKER_SOS) {
428  memcpy(data + dp, unit->data, unit->data_size);
429  dp += unit->data_size;
430  } else {
431  sp = AV_RB16(unit->data);
432  av_assert0(sp <= unit->data_size);
433  memcpy(data + dp, unit->data, sp);
434  dp += sp;
435 
436  for (; sp < unit->data_size; sp++) {
437  if (unit->data[sp] == 0xff) {
438  data[dp++] = 0xff;
439  data[dp++] = 0x00;
440  } else {
441  data[dp++] = unit->data[sp];
442  }
443  }
444  }
445  }
446 
447  data[dp++] = 0xff;
448  data[dp++] = JPEG_MARKER_EOI;
449 
450  av_assert0(dp == size);
451 
452  memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
453  frag->data = data;
454  frag->data_size = size;
455 
456  return 0;
457 }
458 
461 
462  .split_fragment = &cbs_jpeg_split_fragment,
463  .read_unit = &cbs_jpeg_read_unit,
464  .write_unit = &cbs_jpeg_write_unit,
465  .assemble_fragment = &cbs_jpeg_assemble_fragment,
466 };
#define NULL
Definition: coverity.c:32
int nb_units
Number of units in this fragment.
Definition: cbs.h:147
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:125
int size
static int FUNC() dqt(CodedBitstreamContext *ctx, RWContext *rw, JPEGRawQuantisationTableSpecification *current)
Definition: mjpeg.h:111
Definition: mjpeg.h:73
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
CodedBitstreamUnitType type
Codec-specific type of this unit.
Definition: cbs.h:68
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size, void(*free)(void *opaque, uint8_t *data))
Definition: cbs.c:644
static void cbs_jpeg_free_application_data(void *unit, uint8_t *content)
Definition: cbs_jpeg.c:85
AVBufferRef * data_ref
Definition: cbs_jpeg.h:84
static int cbs_jpeg_read_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
Definition: cbs_jpeg.c:245
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
size_t data_size
Definition: cbs_jpeg.h:83
static int cbs_jpeg_write_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_jpeg.c:381
uint8_t
#define av_malloc(s)
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
const CodedBitstreamType ff_cbs_type_jpeg
Definition: cbs_jpeg.c:459
const char data[16]
Definition: mxf.c:91
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
#define sp
Definition: regdef.h:63
static int FUNC() dht(CodedBitstreamContext *ctx, RWContext *rw, JPEGRawHuffmanTableSpecification *current)
Coded bitstream unit structure.
Definition: cbs.h:64
AVBufferRef * Ap_ref
Definition: cbs_jpeg.h:113
void * content
Pointer to the decomposed form of this unit.
Definition: cbs.h:101
static const uint8_t header[24]
Definition: sdr2.c:67
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units_allocated.
Definition: cbs.h:162
#define SEGMENT(marker, type, func, free)
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
Definition: cbs.h:75
int ff_cbs_insert_unit_data(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int position, CodedBitstreamUnitType type, uint8_t *data, size_t data_size, AVBufferRef *data_buf)
Insert a new unit into a fragment with the given data bitstream.
Definition: cbs.c:759
#define av_log(a,...)
static int FUNC() comment(CodedBitstreamContext *ctx, RWContext *rw, JPEGRawComment *current)
size_t data_size
The number of bytes in the bitstream.
Definition: cbs.h:129
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int put_bits_left(PutBitContext *s)
Definition: put_bits.h:93
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
JPEGRawScanHeader header
Definition: cbs_jpeg.h:81
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
Definition: cbs.h:173
Definition: mjpeg.h:56
AVFormatContext * ctx
Definition: movenc.c:48
uint8_t * data
Pointer to the bitstream form of this fragment.
Definition: cbs.h:122
static int cbs_jpeg_write_segment(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_jpeg.c:352
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
Coded bitstream fragment structure, combining one or more units.
Definition: cbs.h:116
uint8_t * data
The data buffer.
Definition: buffer.h:89
static void cbs_jpeg_free_scan(void *unit, uint8_t *content)
Definition: cbs_jpeg.c:99
Context structure for coded bitstream operations.
Definition: cbs.h:168
static int cbs_jpeg_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
Definition: cbs_jpeg.c:391
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
enum AVCodecID codec_id
Definition: cbs_internal.h:29
A reference to a data buffer.
Definition: buffer.h:81
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:92
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:790
static void cbs_jpeg_free_comment(void *unit, uint8_t *content)
Definition: cbs_jpeg.c:92
uint8_t * data
Definition: cbs_jpeg.h:82
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:766
const char int length
Definition: avisynth_c.h:860
AVBufferRef * data_ref
A reference to the buffer containing data.
Definition: cbs.h:139
static int cbs_jpeg_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
Definition: cbs_jpeg.c:106
AVBufferRef * Cm_ref
Definition: cbs_jpeg.h:119
static int cbs_jpeg_write_scan(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
Definition: cbs_jpeg.c:330
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).
Definition: cbs.h:80