24 #define HEADER(name) do { \
25 ff_cbs_trace_header(ctx, name); \
28 #define CHECK(call) do { \
34 #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((int[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
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)
43 #define READWRITE read
44 #define RWContext GetBitContext
45 #define FUNC(name) cbs_jpeg_read_ ## name
47 #define xu(width, name, range_min, range_max, subs, ...) do { \
48 uint32_t value = range_min; \
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; \
64 #define READWRITE write
65 #define RWContext PutBitContext
66 #define FUNC(name) cbs_jpeg_write_ ## name
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)); \
113 int unit,
start,
end, marker, next_start, next_marker;
121 for (i = 0; i + 1 < frag->
data_size && frag->
data[i] != 0xff; i++);
124 "beginning of image.\n", i);
126 for (++i; i + 1 < frag->
data_size && frag->
data[i] == 0xff; i++);
129 "no SOI marker found.\n");
132 marker = frag->
data[i];
135 "marker is %02x, should be SOI.\n", marker);
138 for (++i; i + 1 < frag->
data_size && frag->
data[i] == 0xff; i++);
141 "no image content found.\n");
144 marker = frag->
data[i];
147 for (unit = 0;; unit++) {
151 for (i = start; i + 1 < frag->
data_size; i++) {
152 if (frag->
data[i] != 0xff)
156 frag->
data[i] == 0xff; i++);
160 if (frag->
data[i] == 0x00)
162 next_marker = frag->
data[i];
171 "truncated at %02x marker.\n", marker);
177 "truncated at %02x marker segment.\n", marker);
183 if (frag->
data[i] != 0xff) {
187 frag->
data[i] == 0xff; i++);
191 next_marker = frag->
data[i];
206 memcpy(data, frag->
data + start, length);
207 for (i = start + length, j = length; i <
end; i++, j++) {
208 if (frag->
data[i] == 0xff) {
209 while (frag->
data[i] == 0xff)
213 data[j] = frag->
data[i];
222 data_size = end -
start;
227 data, data_size, data_ref);
234 if (next_marker == -1)
236 marker = next_marker;
261 err = cbs_jpeg_read_frame_header(ctx, &gbc, unit->
content);
273 err = cbs_jpeg_read_application_data(ctx, &gbc, unit->
content);
288 err = cbs_jpeg_read_scan_header(ctx, &gbc, &scan->
header);
303 switch (unit->
type) {
304 #define SEGMENT(marker, type, func, free) \
305 case JPEG_MARKER_ ## marker: \
307 err = ff_cbs_alloc_unit_content(ctx, unit, \
308 sizeof(type), free); \
311 err = cbs_jpeg_read_ ## func(ctx, &gbc, unit->content); \
335 err = cbs_jpeg_write_scan_header(ctx, pbc, &scan->
header);
358 err = cbs_jpeg_write_frame_header(ctx, pbc, unit->
content);
361 err = cbs_jpeg_write_application_data(ctx, pbc, unit->
content);
363 switch (unit->
type) {
364 #define SEGMENT(marker, func) \
365 case JPEG_MARKER_ ## marker: \
366 err = cbs_jpeg_write_ ## func(ctx, pbc, unit->content); \
390 reallocate_and_try_again:
394 "sufficiently large write buffer (last attempt "
410 goto reallocate_and_try_again;
443 for (i = 0; i < frag->
nb_units; i++) {
444 unit = &frag->
units[i];
447 for (sp = 0; sp < unit->
data_size; sp++) {
448 if (unit->
data[sp] == 0xff)
464 for (i = 0; i < frag->
nb_units; i++) {
465 unit = &frag->
units[i];
468 data[dp++] = unit->
type;
476 memcpy(data + dp, unit->
data, sp);
480 if (unit->
data[sp] == 0xff) {
484 data[dp++] = unit->
data[
sp];
int nb_units
Number of units in this fragment.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
ptrdiff_t const GLvoid * data
static int FUNC() dqt(CodedBitstreamContext *ctx, RWContext *rw, JPEGRawQuantisationTableSpecification *current)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define AV_LOG_WARNING
Something somehow does not look correct.
CodedBitstreamUnitType type
Codec-specific type of this unit.
int ff_cbs_alloc_unit_content(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size, void(*free)(void *opaque, uint8_t *data))
static void cbs_jpeg_free_application_data(void *unit, uint8_t *content)
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
static int cbs_jpeg_read_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static void cbs_jpeg_close(CodedBitstreamContext *ctx)
static av_cold int end(AVCodecContext *avctx)
const CodedBitstreamType ff_cbs_type_jpeg
size_t data_bit_padding
The number of bits which should be ignored in the final byte.
static int get_bits_count(const GetBitContext *s)
static int FUNC() dht(CodedBitstreamContext *ctx, RWContext *rw, JPEGRawHuffmanTableSpecification *current)
int ff_cbs_alloc_unit_data(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, size_t size)
Allocate a new internal data buffer of the given size in the unit.
Coded bitstream unit structure.
void * content
Pointer to the decomposed form of this unit.
static const uint8_t header[24]
CodedBitstreamUnit * units
Pointer to an array of units of length nb_units.
#define SEGMENT(marker, type, func, free)
uint8_t * data
Pointer to the directly-parsable bitstream form of this unit.
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.
static int FUNC() comment(CodedBitstreamContext *ctx, RWContext *rw, JPEGRawComment *current)
size_t data_size
The number of bytes in the bitstream.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static int put_bits_left(PutBitContext *s)
static int put_bits_count(PutBitContext *s)
void * log_ctx
Logging context to be passed to all av_log() calls associated with this context.
uint8_t * data
Pointer to the bitstream form of this fragment.
static int cbs_jpeg_write_unit(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit)
static int cbs_jpeg_write_segment(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Coded bitstream fragment structure, combining one or more units.
uint8_t * data
The data buffer.
static void cbs_jpeg_free_scan(void *unit, uint8_t *content)
Context structure for coded bitstream operations.
static int cbs_jpeg_assemble_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
void * priv_data
Internal codec-specific data.
A reference to a data buffer.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
AVBufferRef * data_ref
A reference to the buffer containing data.
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
static void cbs_jpeg_free_comment(void *unit, uint8_t *content)
AVBufferRef * data_ref
A reference to the buffer containing data.
static int cbs_jpeg_split_fragment(CodedBitstreamContext *ctx, CodedBitstreamFragment *frag, int header)
static int cbs_jpeg_write_scan(CodedBitstreamContext *ctx, CodedBitstreamUnit *unit, PutBitContext *pbc)
size_t data_size
The number of bytes in the bitstream (including any padding bits in the final byte).