FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rtjpeg.c
Go to the documentation of this file.
1 /*
2  * RTJpeg decoding functions
3  * Copyright (c) 2006 Reimar Doeffinger
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 #include "libavutil/common.h"
22 #include "get_bits.h"
23 #include "rtjpeg.h"
24 
25 #define PUT_COEFF(c) \
26  i = scan[coeff--]; \
27  block[i] = (c) * quant[i];
28 
29 /// aligns the bitstream to the given power of two
30 #define ALIGN(a) \
31  n = (-get_bits_count(gb)) & (a - 1); \
32  if (n) {skip_bits(gb, n);}
33 
34 /**
35  * @brief read one block from stream
36  * @param gb contains stream data
37  * @param block where data is written to
38  * @param scan array containing the mapping stream address -> block position
39  * @param quant quantization factors
40  * @return 0 means the block is not coded, < 0 means an error occurred.
41  *
42  * Note: GetBitContext is used to make the code simpler, since all data is
43  * aligned this could be done faster in a different way, e.g. as it is done
44  * in MPlayer libmpcodecs/native/rtjpegn.c.
45  */
46 static inline int get_block(GetBitContext *gb, int16_t *block, const uint8_t *scan,
47  const uint32_t *quant) {
48  int coeff, i, n;
49  int8_t ac;
50  uint8_t dc = get_bits(gb, 8);
51 
52  // block not coded
53  if (dc == 255)
54  return 0;
55 
56  // number of non-zero coefficients
57  coeff = get_bits(gb, 6);
58  if (get_bits_left(gb) < (coeff << 1))
59  return AVERROR_INVALIDDATA;
60 
61  // normally we would only need to clear the (63 - coeff) last values,
62  // but since we do not know where they are we just clear the whole block
63  memset(block, 0, 64 * sizeof(int16_t));
64 
65  // 2 bits per coefficient
66  while (coeff) {
67  ac = get_sbits(gb, 2);
68  if (ac == -2)
69  break; // continue with more bits
70  PUT_COEFF(ac);
71  }
72 
73  // 4 bits per coefficient
74  ALIGN(4);
75  if (get_bits_left(gb) < (coeff << 2))
76  return AVERROR_INVALIDDATA;
77  while (coeff) {
78  ac = get_sbits(gb, 4);
79  if (ac == -8)
80  break; // continue with more bits
81  PUT_COEFF(ac);
82  }
83 
84  // 8 bits per coefficient
85  ALIGN(8);
86  if (get_bits_left(gb) < (coeff << 3))
87  return AVERROR_INVALIDDATA;
88  while (coeff) {
89  ac = get_sbits(gb, 8);
90  PUT_COEFF(ac);
91  }
92 
93  PUT_COEFF(dc);
94  return 1;
95 }
96 
97 /**
98  * @brief decode one rtjpeg YUV420 frame
99  * @param c context, must be initialized via ff_rtjpeg_decode_init
100  * @param f AVFrame to place decoded frame into. If parts of the frame
101  * are not coded they are left unchanged, so consider initializing it
102  * @param buf buffer containing input data
103  * @param buf_size length of input data in bytes
104  * @return number of bytes consumed from the input buffer
105  */
107  const uint8_t *buf, int buf_size) {
108  GetBitContext gb;
109  int w = c->w / 16, h = c->h / 16;
110  int x, y, ret;
111  uint8_t *y1 = f->data[0], *y2 = f->data[0] + 8 * f->linesize[0];
112  uint8_t *u = f->data[1], *v = f->data[2];
113 
114  if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
115  return ret;
116 
117  for (y = 0; y < h; y++) {
118  for (x = 0; x < w; x++) {
119 #define BLOCK(quant, dst, stride) do { \
120  int res = get_block(&gb, block, c->scan, quant); \
121  if (res < 0) \
122  return res; \
123  if (res > 0) \
124  c->idsp.idct_put(dst, stride, block); \
125 } while (0)
126  int16_t *block = c->block;
127  BLOCK(c->lquant, y1, f->linesize[0]);
128  y1 += 8;
129  BLOCK(c->lquant, y1, f->linesize[0]);
130  y1 += 8;
131  BLOCK(c->lquant, y2, f->linesize[0]);
132  y2 += 8;
133  BLOCK(c->lquant, y2, f->linesize[0]);
134  y2 += 8;
135  BLOCK(c->cquant, u, f->linesize[1]);
136  u += 8;
137  BLOCK(c->cquant, v, f->linesize[2]);
138  v += 8;
139  }
140  y1 += 2 * 8 * (f->linesize[0] - w);
141  y2 += 2 * 8 * (f->linesize[0] - w);
142  u += 8 * (f->linesize[1] - w);
143  v += 8 * (f->linesize[2] - w);
144  }
145  return get_bits_count(&gb) / 8;
146 }
147 
148 /**
149  * @brief initialize an RTJpegContext, may be called multiple times
150  * @param c context to initialize
151  * @param width width of image, will be rounded down to the nearest multiple
152  * of 16 for decoding
153  * @param height height of image, will be rounded down to the nearest multiple
154  * of 16 for decoding
155  * @param lquant luma quantization table to use
156  * @param cquant chroma quantization table to use
157  */
159  const uint32_t *lquant, const uint32_t *cquant) {
160  int i;
161  for (i = 0; i < 64; i++) {
162  int p = c->idsp.idct_permutation[i];
163  c->lquant[p] = lquant[i];
164  c->cquant[p] = cquant[i];
165  }
166  c->w = width;
167  c->h = height;
168 }
169 
171 {
172  int i;
173 
174  ff_idctdsp_init(&c->idsp, avctx);
175 
176  for (i = 0; i < 64; i++) {
177  int z = ff_zigzag_direct[i];
178  z = ((z << 3) | (z >> 3)) & 63; // rtjpeg uses a transposed variant
179 
180  // permute the scan and quantization tables for the chosen idct
181  c->scan[i] = c->idsp.idct_permutation[z];
182  }
183 }
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:187
static int get_block(GetBitContext *gb, int16_t *block, const uint8_t *scan, const uint32_t *quant)
read one block from stream
Definition: rtjpeg.c:46
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
uint32_t cquant[64]
Definition: rtjpeg.h:38
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:246
static int16_t block[64]
Definition: dct.c:115
uint8_t
#define BLOCK(quant, dst, stride)
int ff_rtjpeg_decode_frame_yuv420(RTJpegContext *c, AVFrame *f, const uint8_t *buf, int buf_size)
decode one rtjpeg YUV420 frame
Definition: rtjpeg.c:106
void ff_rtjpeg_init(RTJpegContext *c, AVCodecContext *avctx)
Definition: rtjpeg.c:170
int16_t block[64]
Definition: rtjpeg.h:39
#define height
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:199
bitstream reader API header.
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:587
#define width
IDCTDSPContext idsp
Definition: rtjpeg.h:35
void ff_rtjpeg_decode_init(RTJpegContext *c, int width, int height, const uint32_t *lquant, const uint32_t *cquant)
initialize an RTJpegContext, may be called multiple times
Definition: rtjpeg.c:158
int n
Definition: avisynth_c.h:684
uint8_t idct_permutation[64]
IDCT input permutation.
Definition: idctdsp.h:96
uint8_t scan[64]
Definition: rtjpeg.h:36
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:218
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:456
uint32_t lquant[64]
Definition: rtjpeg.h:37
main external API structure.
Definition: avcodec.h:1732
#define ALIGN(a)
aligns the bitstream to the given power of two
Definition: rtjpeg.c:30
void * buf
Definition: avisynth_c.h:690
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
#define u(width,...)
const uint8_t * quant
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:201
common internal and external API header
static double c[64]
#define PUT_COEFF(c)
Definition: rtjpeg.c:25
av_cold void ff_idctdsp_init(IDCTDSPContext *c, AVCodecContext *avctx)
Definition: idctdsp.c:241
static const double coeff[2][5]
Definition: vf_owdenoise.c:72
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> dc