| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * MJPEG decoder | ||
| 3 | * Copyright (c) 2000, 2001 Fabrice Bellard | ||
| 4 | * Copyright (c) 2003 Alex Beregszaszi | ||
| 5 | * Copyright (c) 2003-2004 Michael Niedermayer | ||
| 6 | * | ||
| 7 | * This file is part of FFmpeg. | ||
| 8 | * | ||
| 9 | * FFmpeg is free software; you can redistribute it and/or | ||
| 10 | * modify it under the terms of the GNU Lesser General Public | ||
| 11 | * License as published by the Free Software Foundation; either | ||
| 12 | * version 2.1 of the License, or (at your option) any later version. | ||
| 13 | * | ||
| 14 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 17 | * Lesser General Public License for more details. | ||
| 18 | * | ||
| 19 | * You should have received a copy of the GNU Lesser General Public | ||
| 20 | * License along with FFmpeg; if not, write to the Free Software | ||
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 22 | */ | ||
| 23 | |||
| 24 | /** | ||
| 25 | * @file | ||
| 26 | * MJPEG decoder. | ||
| 27 | */ | ||
| 28 | |||
| 29 | #ifndef AVCODEC_MJPEGDEC_H | ||
| 30 | #define AVCODEC_MJPEGDEC_H | ||
| 31 | |||
| 32 | #include "libavutil/log.h" | ||
| 33 | #include "libavutil/mem_internal.h" | ||
| 34 | #include "libavutil/pixdesc.h" | ||
| 35 | #include "libavutil/stereo3d.h" | ||
| 36 | |||
| 37 | #include "avcodec.h" | ||
| 38 | #include "blockdsp.h" | ||
| 39 | #include "bytestream.h" | ||
| 40 | #include "exif.h" | ||
| 41 | #include "get_bits.h" | ||
| 42 | #include "hpeldsp.h" | ||
| 43 | #include "idctdsp.h" | ||
| 44 | |||
| 45 | #undef near /* This file uses struct member 'near' which in windows.h is defined as empty. */ | ||
| 46 | |||
| 47 | #define MAX_COMPONENTS 4 | ||
| 48 | |||
| 49 | typedef struct ICCEntry { | ||
| 50 | uint8_t *data; | ||
| 51 | int length; | ||
| 52 | } ICCEntry; | ||
| 53 | |||
| 54 | struct JLSState; | ||
| 55 | |||
| 56 | typedef struct MJpegDecodeContext { | ||
| 57 | AVClass *class; | ||
| 58 | AVCodecContext *avctx; | ||
| 59 | GetBitContext gb; | ||
| 60 | GetByteContext gB; | ||
| 61 | int buf_size; | ||
| 62 | |||
| 63 | int buffer_size; | ||
| 64 | uint8_t *buffer; | ||
| 65 | |||
| 66 | uint16_t quant_matrixes[4][64]; | ||
| 67 | VLC vlcs[3][4]; | ||
| 68 | int qscale[4]; ///< quantizer scale calculated from quant_matrixes | ||
| 69 | |||
| 70 | int orig_height; /* size given at codec init */ | ||
| 71 | int first_picture; /* true if decoding first picture */ | ||
| 72 | int interlaced; /* true if interlaced */ | ||
| 73 | int bottom_field; /* true if bottom field */ | ||
| 74 | int lossless; | ||
| 75 | int ls; | ||
| 76 | int progressive; | ||
| 77 | int bayer; /* true if it's a bayer-encoded JPEG embedded in a DNG */ | ||
| 78 | int rgb; | ||
| 79 | uint8_t upscale_h[4]; | ||
| 80 | uint8_t upscale_v[4]; | ||
| 81 | int rct; /* standard rct */ | ||
| 82 | int pegasus_rct; /* pegasus reversible colorspace transform */ | ||
| 83 | int bits; /* bits per component */ | ||
| 84 | int colr; | ||
| 85 | int xfrm; | ||
| 86 | int adobe_transform; | ||
| 87 | |||
| 88 | /* SOS fields */ | ||
| 89 | int nb_components_sos; | ||
| 90 | int Ss; | ||
| 91 | int Se; | ||
| 92 | int Ah; | ||
| 93 | int Al; | ||
| 94 | |||
| 95 | int maxval; | ||
| 96 | int near; ///< near lossless bound (si 0 for lossless) | ||
| 97 | int t1,t2,t3; | ||
| 98 | int reset; ///< context halfing interval ?rename | ||
| 99 | |||
| 100 | int width, height; | ||
| 101 | int mb_width, mb_height; | ||
| 102 | int nb_components; | ||
| 103 | int block_stride[MAX_COMPONENTS]; | ||
| 104 | int component_id[MAX_COMPONENTS]; | ||
| 105 | int h_count[MAX_COMPONENTS]; /* horizontal and vertical count for each component */ | ||
| 106 | int v_count[MAX_COMPONENTS]; | ||
| 107 | int comp_index[MAX_COMPONENTS]; | ||
| 108 | int dc_index[MAX_COMPONENTS]; | ||
| 109 | int ac_index[MAX_COMPONENTS]; | ||
| 110 | int nb_blocks[MAX_COMPONENTS]; | ||
| 111 | int h_scount[MAX_COMPONENTS]; | ||
| 112 | int v_scount[MAX_COMPONENTS]; | ||
| 113 | int quant_sindex[MAX_COMPONENTS]; | ||
| 114 | int h_max, v_max; /* maximum h and v counts */ | ||
| 115 | int quant_index[4]; /* quant table index for each component */ | ||
| 116 | int last_dc[MAX_COMPONENTS]; /* last DEQUANTIZED dc (XXX: am I right to do that ?) */ | ||
| 117 | AVFrame *picture; /* picture structure */ | ||
| 118 | AVFrame *picture_ptr; /* pointer to picture structure */ | ||
| 119 | int got_picture; ///< we found a SOF and picture is valid, too. | ||
| 120 | int linesize[MAX_COMPONENTS]; ///< linesize << interlaced | ||
| 121 | DECLARE_ALIGNED(32, int16_t, block)[64]; | ||
| 122 | int16_t (*blocks[MAX_COMPONENTS])[64]; ///< intermediate sums (progressive mode) | ||
| 123 | uint8_t *last_nnz[MAX_COMPONENTS]; | ||
| 124 | uint64_t coefs_finished[MAX_COMPONENTS]; ///< bitmask of which coefs have been completely decoded (progressive mode) | ||
| 125 | int palette_index; | ||
| 126 | int force_pal8; | ||
| 127 | uint8_t permutated_scantable[64]; | ||
| 128 | BlockDSPContext bdsp; | ||
| 129 | IDCTDSPContext idsp; | ||
| 130 | op_pixels_func copy_block; ///< only set and used by mxpeg | ||
| 131 | |||
| 132 | int restart_interval; | ||
| 133 | int restart_count; | ||
| 134 | |||
| 135 | int cs_itu601; | ||
| 136 | int interlace_polarity; | ||
| 137 | int multiscope; | ||
| 138 | |||
| 139 | int mjpb_skiptosod; | ||
| 140 | |||
| 141 | int cur_scan; /* current scan, used by JPEG-LS */ | ||
| 142 | int flipped; /* true if picture is flipped */ | ||
| 143 | |||
| 144 | uint16_t (*ljpeg_buffer)[4]; | ||
| 145 | unsigned int ljpeg_buffer_size; | ||
| 146 | |||
| 147 | #if FF_API_MJPEG_EXTERN_HUFF | ||
| 148 | int extern_huff; | ||
| 149 | #endif | ||
| 150 | AVExifMetadata exif_metadata; | ||
| 151 | |||
| 152 | AVStereo3D *stereo3d; ///!< stereoscopic information (cached, since it is read before frame allocation) | ||
| 153 | |||
| 154 | const AVPixFmtDescriptor *pix_desc; | ||
| 155 | |||
| 156 | ICCEntry *iccentries; | ||
| 157 | int iccnum; | ||
| 158 | int iccread; | ||
| 159 | |||
| 160 | AVFrame *smv_frame; | ||
| 161 | int smv_frames_per_jpeg; | ||
| 162 | int smv_next_frame; | ||
| 163 | |||
| 164 | // Raw stream data for hwaccel use. | ||
| 165 | const uint8_t *raw_image_buffer; | ||
| 166 | size_t raw_image_buffer_size; | ||
| 167 | |||
| 168 | uint8_t raw_huffman_lengths[2][4][16]; | ||
| 169 | uint8_t raw_huffman_values[2][4][256]; | ||
| 170 | |||
| 171 | enum AVPixelFormat hwaccel_sw_pix_fmt; | ||
| 172 | enum AVPixelFormat hwaccel_pix_fmt; | ||
| 173 | void *hwaccel_picture_private; | ||
| 174 | struct JLSState *jls_state; | ||
| 175 | |||
| 176 | const uint8_t *mb_bitmask; | ||
| 177 | size_t mb_bitmask_size; | ||
| 178 | const AVFrame *reference; | ||
| 179 | } MJpegDecodeContext; | ||
| 180 | |||
| 181 | int ff_mjpeg_build_vlc(VLC *vlc, const uint8_t *bits_table, | ||
| 182 | const uint8_t *val_table, int is_ac, void *logctx); | ||
| 183 | int ff_mjpeg_decode_init(AVCodecContext *avctx); | ||
| 184 | int ff_mjpeg_decode_end(AVCodecContext *avctx); | ||
| 185 | int ff_mjpeg_decode_frame(AVCodecContext *avctx, | ||
| 186 | AVFrame *frame, int *got_frame, | ||
| 187 | AVPacket *avpkt); | ||
| 188 | int ff_mjpeg_decode_frame_from_buf(AVCodecContext *avctx, | ||
| 189 | AVFrame *frame, int *got_frame, | ||
| 190 | const AVPacket *avpkt, const uint8_t *buf, int buf_size); | ||
| 191 | int ff_mjpeg_decode_dqt(MJpegDecodeContext *s); | ||
| 192 | int ff_mjpeg_decode_dht(MJpegDecodeContext *s); | ||
| 193 | int ff_mjpeg_decode_sof(MJpegDecodeContext *s); | ||
| 194 | int ff_mjpeg_decode_sos(MJpegDecodeContext *s); | ||
| 195 | int ff_mjpeg_find_marker(const uint8_t **buf_ptr, const uint8_t *buf_end); | ||
| 196 | int ff_mjpeg_unescape_sos(MJpegDecodeContext *s); | ||
| 197 | |||
| 198 | 4679677 | static inline int ff_mjpeg_should_restart(MJpegDecodeContext *s) | |
| 199 | { | ||
| 200 | 4679677 | int restart = 0; | |
| 201 |
2/2✓ Branch 0 taken 60554 times.
✓ Branch 1 taken 4619123 times.
|
4679677 | if (s->restart_interval) { |
| 202 |
2/2✓ Branch 0 taken 3554 times.
✓ Branch 1 taken 57000 times.
|
60554 | if (s->restart_count <= 0) { |
| 203 | 3554 | s->restart_count = s->restart_interval; | |
| 204 | 3554 | restart = 1; | |
| 205 | } | ||
| 206 | 60554 | s->restart_count--; | |
| 207 | } else { | ||
| 208 |
2/2✓ Branch 0 taken 2425 times.
✓ Branch 1 taken 4616698 times.
|
4619123 | if (s->restart_count < 0) { |
| 209 | 2425 | s->restart_count = 0; | |
| 210 | 2425 | restart = 1; | |
| 211 | } | ||
| 212 | } | ||
| 213 | 4679677 | return restart; | |
| 214 | } | ||
| 215 | |||
| 216 | 4623437 | static inline int ff_mjpeg_handle_restart(MJpegDecodeContext *s, int *restart) | |
| 217 | { | ||
| 218 | 4623437 | *restart = ff_mjpeg_should_restart(s); | |
| 219 |
2/2✓ Branch 0 taken 5905 times.
✓ Branch 1 taken 4617532 times.
|
4623437 | if (*restart) { |
| 220 | 5905 | int ret = ff_mjpeg_unescape_sos(s); | |
| 221 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5905 times.
|
5905 | if (ret < 0) |
| 222 | ✗ | return ret; | |
| 223 | } | ||
| 224 | 4623437 | return 0; | |
| 225 | } | ||
| 226 | |||
| 227 | #endif /* AVCODEC_MJPEGDEC_H */ | ||
| 228 |