libavcodec/mpeg4videodec.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * MPEG-4 decoder | ||
| 3 | * Copyright (c) 2000,2001 Fabrice Bellard | ||
| 4 | * Copyright (c) 2002-2010 Michael Niedermayer <michaelni@gmx.at> | ||
| 5 | * | ||
| 6 | * This file is part of FFmpeg. | ||
| 7 | * | ||
| 8 | * FFmpeg is free software; you can redistribute it and/or | ||
| 9 | * modify it under the terms of the GNU Lesser General Public | ||
| 10 | * License as published by the Free Software Foundation; either | ||
| 11 | * version 2.1 of the License, or (at your option) any later version. | ||
| 12 | * | ||
| 13 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 16 | * Lesser General Public License for more details. | ||
| 17 | * | ||
| 18 | * You should have received a copy of the GNU Lesser General Public | ||
| 19 | * License along with FFmpeg; if not, write to the Free Software | ||
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 21 | */ | ||
| 22 | |||
| 23 | #define UNCHECKED_BITSTREAM_READER 1 | ||
| 24 | |||
| 25 | #include "config_components.h" | ||
| 26 | |||
| 27 | #include "libavutil/avassert.h" | ||
| 28 | #include "libavutil/internal.h" | ||
| 29 | #include "libavutil/pixdesc.h" | ||
| 30 | #include "libavutil/opt.h" | ||
| 31 | #include "libavutil/thread.h" | ||
| 32 | #include "codec_internal.h" | ||
| 33 | #include "error_resilience.h" | ||
| 34 | #include "hwconfig.h" | ||
| 35 | #include "idctdsp.h" | ||
| 36 | #include "mpegutils.h" | ||
| 37 | #include "mpegvideo.h" | ||
| 38 | #include "mpegvideodata.h" | ||
| 39 | #include "mpegvideodec.h" | ||
| 40 | #include "mpegvideo_unquantize.h" | ||
| 41 | #include "mpeg4video.h" | ||
| 42 | #include "mpeg4videodata.h" | ||
| 43 | #include "mpeg4videodec.h" | ||
| 44 | #include "mpeg4videodefs.h" | ||
| 45 | #include "h263.h" | ||
| 46 | #include "h263data.h" | ||
| 47 | #include "h263dec.h" | ||
| 48 | #include "internal.h" | ||
| 49 | #include "profiles.h" | ||
| 50 | #include "qpeldsp.h" | ||
| 51 | #include "threadprogress.h" | ||
| 52 | #include "unary.h" | ||
| 53 | |||
| 54 | #if 0 //3IV1 is quite rare and it slows things down a tiny bit | ||
| 55 | #define IS_3IV1 (s->codec_tag == AV_RL32("3IV1")) | ||
| 56 | #else | ||
| 57 | #define IS_3IV1 0 | ||
| 58 | #endif | ||
| 59 | |||
| 60 | /* The defines below define the number of bits that are read at once for | ||
| 61 | * reading vlc values. Changing these may improve speed and data cache needs | ||
| 62 | * be aware though that decreasing them may need the number of stages that is | ||
| 63 | * passed to get_vlc* to be increased. */ | ||
| 64 | #define SPRITE_TRAJ_VLC_BITS 6 | ||
| 65 | #define DC_VLC_BITS 9 | ||
| 66 | #define MB_TYPE_B_VLC_BITS 4 | ||
| 67 | #define STUDIO_INTRA_BITS 9 | ||
| 68 | |||
| 69 | static VLCElem dc_lum[512], dc_chrom[512]; | ||
| 70 | static VLCElem sprite_trajectory[128]; | ||
| 71 | static VLCElem mb_type_b_vlc[16]; | ||
| 72 | static const VLCElem *studio_intra_tab[12]; | ||
| 73 | static VLCElem studio_luma_dc[528]; | ||
| 74 | static VLCElem studio_chroma_dc[528]; | ||
| 75 | |||
| 76 | static const uint8_t mpeg4_block_count[4] = { 0, 6, 8, 12 }; | ||
| 77 | |||
| 78 | static const int16_t mb_type_b_map[4] = { | ||
| 79 | MB_TYPE_DIRECT2 | MB_TYPE_BIDIR_MV, | ||
| 80 | MB_TYPE_BIDIR_MV | MB_TYPE_16x16, | ||
| 81 | MB_TYPE_BACKWARD_MV | MB_TYPE_16x16, | ||
| 82 | MB_TYPE_FORWARD_MV | MB_TYPE_16x16, | ||
| 83 | }; | ||
| 84 | |||
| 85 | 1618776 | static inline Mpeg4DecContext *h263_to_mpeg4(H263DecContext *h) | |
| 86 | { | ||
| 87 | av_assert2(h->c.codec_id == AV_CODEC_ID_MPEG4 && h->c.avctx->priv_data == h); | ||
| 88 | 1618776 | return (Mpeg4DecContext*)h; | |
| 89 | } | ||
| 90 | |||
| 91 | ✗ | static void gmc1_motion(MpegEncContext *s, const Mpeg4DecContext *ctx, | |
| 92 | uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, | ||
| 93 | uint8_t *const *ref_picture) | ||
| 94 | { | ||
| 95 | const uint8_t *ptr; | ||
| 96 | int src_x, src_y, motion_x, motion_y; | ||
| 97 | ptrdiff_t offset, linesize, uvlinesize; | ||
| 98 | ✗ | int emu = 0; | |
| 99 | |||
| 100 | ✗ | motion_x = ctx->sprite_offset[0][0]; | |
| 101 | ✗ | motion_y = ctx->sprite_offset[0][1]; | |
| 102 | ✗ | src_x = s->mb_x * 16 + (motion_x >> (ctx->sprite_warping_accuracy + 1)); | |
| 103 | ✗ | src_y = s->mb_y * 16 + (motion_y >> (ctx->sprite_warping_accuracy + 1)); | |
| 104 | ✗ | motion_x *= 1 << (3 - ctx->sprite_warping_accuracy); | |
| 105 | ✗ | motion_y *= 1 << (3 - ctx->sprite_warping_accuracy); | |
| 106 | ✗ | src_x = av_clip(src_x, -16, s->width); | |
| 107 | ✗ | if (src_x == s->width) | |
| 108 | ✗ | motion_x = 0; | |
| 109 | ✗ | src_y = av_clip(src_y, -16, s->height); | |
| 110 | ✗ | if (src_y == s->height) | |
| 111 | ✗ | motion_y = 0; | |
| 112 | |||
| 113 | ✗ | linesize = s->linesize; | |
| 114 | ✗ | uvlinesize = s->uvlinesize; | |
| 115 | |||
| 116 | ✗ | ptr = ref_picture[0] + src_y * linesize + src_x; | |
| 117 | |||
| 118 | ✗ | if ((unsigned)src_x >= FFMAX(s->h_edge_pos - 17, 0) || | |
| 119 | ✗ | (unsigned)src_y >= FFMAX(s->v_edge_pos - 17, 0)) { | |
| 120 | ✗ | s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr, | |
| 121 | linesize, linesize, | ||
| 122 | 17, 17, | ||
| 123 | src_x, src_y, | ||
| 124 | s->h_edge_pos, s->v_edge_pos); | ||
| 125 | ✗ | ptr = s->sc.edge_emu_buffer; | |
| 126 | } | ||
| 127 | |||
| 128 | ✗ | if ((motion_x | motion_y) & 7) { | |
| 129 | ✗ | ctx->mdsp.gmc1(dest_y, ptr, linesize, 16, | |
| 130 | ✗ | motion_x & 15, motion_y & 15, 128 - s->no_rounding); | |
| 131 | ✗ | ctx->mdsp.gmc1(dest_y + 8, ptr + 8, linesize, 16, | |
| 132 | ✗ | motion_x & 15, motion_y & 15, 128 - s->no_rounding); | |
| 133 | } else { | ||
| 134 | int dxy; | ||
| 135 | |||
| 136 | ✗ | dxy = ((motion_x >> 3) & 1) | ((motion_y >> 2) & 2); | |
| 137 | ✗ | if (s->no_rounding) { | |
| 138 | ✗ | s->hdsp.put_no_rnd_pixels_tab[0][dxy](dest_y, ptr, linesize, 16); | |
| 139 | } else { | ||
| 140 | ✗ | s->hdsp.put_pixels_tab[0][dxy](dest_y, ptr, linesize, 16); | |
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY) | ||
| 145 | return; | ||
| 146 | |||
| 147 | ✗ | motion_x = ctx->sprite_offset[1][0]; | |
| 148 | ✗ | motion_y = ctx->sprite_offset[1][1]; | |
| 149 | ✗ | src_x = s->mb_x * 8 + (motion_x >> (ctx->sprite_warping_accuracy + 1)); | |
| 150 | ✗ | src_y = s->mb_y * 8 + (motion_y >> (ctx->sprite_warping_accuracy + 1)); | |
| 151 | ✗ | motion_x *= 1 << (3 - ctx->sprite_warping_accuracy); | |
| 152 | ✗ | motion_y *= 1 << (3 - ctx->sprite_warping_accuracy); | |
| 153 | ✗ | src_x = av_clip(src_x, -8, s->width >> 1); | |
| 154 | ✗ | if (src_x == s->width >> 1) | |
| 155 | ✗ | motion_x = 0; | |
| 156 | ✗ | src_y = av_clip(src_y, -8, s->height >> 1); | |
| 157 | ✗ | if (src_y == s->height >> 1) | |
| 158 | ✗ | motion_y = 0; | |
| 159 | |||
| 160 | ✗ | offset = (src_y * uvlinesize) + src_x; | |
| 161 | ✗ | ptr = ref_picture[1] + offset; | |
| 162 | ✗ | if ((unsigned)src_x >= FFMAX((s->h_edge_pos >> 1) - 9, 0) || | |
| 163 | ✗ | (unsigned)src_y >= FFMAX((s->v_edge_pos >> 1) - 9, 0)) { | |
| 164 | ✗ | s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr, | |
| 165 | uvlinesize, uvlinesize, | ||
| 166 | 9, 9, | ||
| 167 | src_x, src_y, | ||
| 168 | ✗ | s->h_edge_pos >> 1, s->v_edge_pos >> 1); | |
| 169 | ✗ | ptr = s->sc.edge_emu_buffer; | |
| 170 | ✗ | emu = 1; | |
| 171 | } | ||
| 172 | ✗ | ctx->mdsp.gmc1(dest_cb, ptr, uvlinesize, 8, | |
| 173 | ✗ | motion_x & 15, motion_y & 15, 128 - s->no_rounding); | |
| 174 | |||
| 175 | ✗ | ptr = ref_picture[2] + offset; | |
| 176 | ✗ | if (emu) { | |
| 177 | ✗ | s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr, | |
| 178 | uvlinesize, uvlinesize, | ||
| 179 | 9, 9, | ||
| 180 | src_x, src_y, | ||
| 181 | ✗ | s->h_edge_pos >> 1, s->v_edge_pos >> 1); | |
| 182 | ✗ | ptr = s->sc.edge_emu_buffer; | |
| 183 | } | ||
| 184 | ✗ | ctx->mdsp.gmc1(dest_cr, ptr, uvlinesize, 8, | |
| 185 | ✗ | motion_x & 15, motion_y & 15, 128 - s->no_rounding); | |
| 186 | } | ||
| 187 | |||
| 188 | ✗ | static void gmc_motion(MpegEncContext *s, const Mpeg4DecContext *ctx, | |
| 189 | uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, | ||
| 190 | uint8_t *const *ref_picture) | ||
| 191 | { | ||
| 192 | const uint8_t *ptr; | ||
| 193 | int linesize, uvlinesize; | ||
| 194 | ✗ | const int a = ctx->sprite_warping_accuracy; | |
| 195 | int ox, oy; | ||
| 196 | |||
| 197 | ✗ | linesize = s->linesize; | |
| 198 | ✗ | uvlinesize = s->uvlinesize; | |
| 199 | |||
| 200 | ✗ | ptr = ref_picture[0]; | |
| 201 | |||
| 202 | ✗ | ox = ctx->sprite_offset[0][0] + ctx->sprite_delta[0][0] * s->mb_x * 16 + | |
| 203 | ✗ | ctx->sprite_delta[0][1] * s->mb_y * 16; | |
| 204 | ✗ | oy = ctx->sprite_offset[0][1] + ctx->sprite_delta[1][0] * s->mb_x * 16 + | |
| 205 | ✗ | ctx->sprite_delta[1][1] * s->mb_y * 16; | |
| 206 | |||
| 207 | ✗ | ctx->mdsp.gmc(dest_y, ptr, linesize, 16, | |
| 208 | ox, oy, | ||
| 209 | ✗ | ctx->sprite_delta[0][0], ctx->sprite_delta[0][1], | |
| 210 | ✗ | ctx->sprite_delta[1][0], ctx->sprite_delta[1][1], | |
| 211 | ✗ | a + 1, (1 << (2 * a + 1)) - s->no_rounding, | |
| 212 | s->h_edge_pos, s->v_edge_pos); | ||
| 213 | ✗ | ctx->mdsp.gmc(dest_y + 8, ptr, linesize, 16, | |
| 214 | ✗ | ox + ctx->sprite_delta[0][0] * 8, | |
| 215 | ✗ | oy + ctx->sprite_delta[1][0] * 8, | |
| 216 | ✗ | ctx->sprite_delta[0][0], ctx->sprite_delta[0][1], | |
| 217 | ✗ | ctx->sprite_delta[1][0], ctx->sprite_delta[1][1], | |
| 218 | ✗ | a + 1, (1 << (2 * a + 1)) - s->no_rounding, | |
| 219 | s->h_edge_pos, s->v_edge_pos); | ||
| 220 | |||
| 221 | if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY) | ||
| 222 | return; | ||
| 223 | |||
| 224 | ✗ | ox = ctx->sprite_offset[1][0] + ctx->sprite_delta[0][0] * s->mb_x * 8 + | |
| 225 | ✗ | ctx->sprite_delta[0][1] * s->mb_y * 8; | |
| 226 | ✗ | oy = ctx->sprite_offset[1][1] + ctx->sprite_delta[1][0] * s->mb_x * 8 + | |
| 227 | ✗ | ctx->sprite_delta[1][1] * s->mb_y * 8; | |
| 228 | |||
| 229 | ✗ | ptr = ref_picture[1]; | |
| 230 | ✗ | ctx->mdsp.gmc(dest_cb, ptr, uvlinesize, 8, | |
| 231 | ox, oy, | ||
| 232 | ✗ | ctx->sprite_delta[0][0], ctx->sprite_delta[0][1], | |
| 233 | ✗ | ctx->sprite_delta[1][0], ctx->sprite_delta[1][1], | |
| 234 | ✗ | a + 1, (1 << (2 * a + 1)) - s->no_rounding, | |
| 235 | ✗ | (s->h_edge_pos + 1) >> 1, (s->v_edge_pos + 1) >> 1); | |
| 236 | |||
| 237 | ✗ | ptr = ref_picture[2]; | |
| 238 | ✗ | ctx->mdsp.gmc(dest_cr, ptr, uvlinesize, 8, | |
| 239 | ox, oy, | ||
| 240 | ✗ | ctx->sprite_delta[0][0], ctx->sprite_delta[0][1], | |
| 241 | ✗ | ctx->sprite_delta[1][0], ctx->sprite_delta[1][1], | |
| 242 | ✗ | a + 1, (1 << (2 * a + 1)) - s->no_rounding, | |
| 243 | ✗ | (s->h_edge_pos + 1) >> 1, (s->v_edge_pos + 1) >> 1); | |
| 244 | } | ||
| 245 | |||
| 246 | ✗ | void ff_mpeg4_mcsel_motion(MpegEncContext *s, | |
| 247 | uint8_t *dest_y, uint8_t *dest_cb, uint8_t *dest_cr, | ||
| 248 | uint8_t *const *ref_picture) | ||
| 249 | { | ||
| 250 | ✗ | const Mpeg4DecContext *const ctx = (Mpeg4DecContext*)s; | |
| 251 | |||
| 252 | ✗ | if (ctx->real_sprite_warping_points == 1) { | |
| 253 | ✗ | gmc1_motion(s, ctx, dest_y, dest_cb, dest_cr, | |
| 254 | ref_picture); | ||
| 255 | } else { | ||
| 256 | ✗ | gmc_motion(s, ctx, dest_y, dest_cb, dest_cr, | |
| 257 | ref_picture); | ||
| 258 | } | ||
| 259 | ✗ | } | |
| 260 | |||
| 261 | 1350 | void ff_mpeg4_decode_studio(MpegEncContext *s, uint8_t *dest_y, uint8_t *dest_cb, | |
| 262 | uint8_t *dest_cr, int block_size, int uvlinesize, | ||
| 263 | int dct_linesize, int dct_offset) | ||
| 264 | { | ||
| 265 | 1350 | Mpeg4DecContext *const ctx = (Mpeg4DecContext*)s; | |
| 266 | 1350 | const int act_block_size = block_size * 2; | |
| 267 | |||
| 268 | 1350 | if (ctx->dpcm_direction == 0) { | |
| 269 | 667 | s->idsp.idct_put(dest_y, dct_linesize, (int16_t*)ctx->block32[0]); | |
| 270 | 667 | s->idsp.idct_put(dest_y + act_block_size, dct_linesize, (int16_t*)ctx->block32[1]); | |
| 271 | 667 | s->idsp.idct_put(dest_y + dct_offset, dct_linesize, (int16_t*)ctx->block32[2]); | |
| 272 | 667 | s->idsp.idct_put(dest_y + dct_offset + act_block_size, dct_linesize, (int16_t*)ctx->block32[3]); | |
| 273 | |||
| 274 | 667 | dct_linesize = uvlinesize << s->interlaced_dct; | |
| 275 | 667 | dct_offset = s->interlaced_dct ? uvlinesize : uvlinesize*block_size; | |
| 276 | |||
| 277 | 667 | s->idsp.idct_put(dest_cb, dct_linesize, (int16_t*)ctx->block32[4]); | |
| 278 | 667 | s->idsp.idct_put(dest_cr, dct_linesize, (int16_t*)ctx->block32[5]); | |
| 279 | 667 | s->idsp.idct_put(dest_cb + dct_offset, dct_linesize, (int16_t*)ctx->block32[6]); | |
| 280 | 667 | s->idsp.idct_put(dest_cr + dct_offset, dct_linesize, (int16_t*)ctx->block32[7]); | |
| 281 | 667 | if (!s->chroma_x_shift){ //Chroma444 | |
| 282 | ✗ | s->idsp.idct_put(dest_cb + act_block_size, dct_linesize, (int16_t*)ctx->block32[8]); | |
| 283 | ✗ | s->idsp.idct_put(dest_cr + act_block_size, dct_linesize, (int16_t*)ctx->block32[9]); | |
| 284 | ✗ | s->idsp.idct_put(dest_cb + act_block_size + dct_offset, dct_linesize, (int16_t*)ctx->block32[10]); | |
| 285 | ✗ | s->idsp.idct_put(dest_cr + act_block_size + dct_offset, dct_linesize, (int16_t*)ctx->block32[11]); | |
| 286 | } | ||
| 287 | 683 | } else if (ctx->dpcm_direction == 1) { | |
| 288 | 683 | uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr}; | |
| 289 | 683 | int linesize[3] = {dct_linesize, uvlinesize, uvlinesize}; | |
| 290 | 2732 | for (int i = 0; i < 3; i++) { | |
| 291 | 2049 | const uint16_t *src = ctx->dpcm_macroblock[i]; | |
| 292 | 2049 | int vsub = i ? s->chroma_y_shift : 0; | |
| 293 | 2049 | int hsub = i ? s->chroma_x_shift : 0; | |
| 294 | 2049 | int lowres = s->avctx->lowres; | |
| 295 | 2049 | int step = 1 << lowres; | |
| 296 | 34833 | for (int h = 0; h < (16 >> (vsub + lowres)); h++){ | |
| 297 | 382480 | for (int w = 0, idx = 0; w < (16 >> (hsub + lowres)); w++, idx += step) | |
| 298 | 349696 | dest_pcm[i][w] = src[idx]; | |
| 299 | 32784 | dest_pcm[i] += linesize[i] / 2; | |
| 300 | 32784 | src += (16 >> hsub) * step; | |
| 301 | } | ||
| 302 | } | ||
| 303 | } else { | ||
| 304 | ✗ | uint16_t *dest_pcm[3] = {(uint16_t*)dest_y, (uint16_t*)dest_cb, (uint16_t*)dest_cr}; | |
| 305 | ✗ | int linesize[3] = {dct_linesize, uvlinesize, uvlinesize}; | |
| 306 | av_assert2(ctx->dpcm_direction == -1); | ||
| 307 | ✗ | for (int i = 0; i < 3; i++) { | |
| 308 | ✗ | const uint16_t *src = ctx->dpcm_macroblock[i]; | |
| 309 | ✗ | int vsub = i ? s->chroma_y_shift : 0; | |
| 310 | ✗ | int hsub = i ? s->chroma_x_shift : 0; | |
| 311 | ✗ | int lowres = s->avctx->lowres; | |
| 312 | ✗ | int step = 1 << lowres; | |
| 313 | ✗ | dest_pcm[i] += (linesize[i] / 2) * ((16 >> vsub + lowres) - 1); | |
| 314 | ✗ | for (int h = (16 >> (vsub + lowres)) - 1; h >= 0; h--){ | |
| 315 | ✗ | for (int w = (16 >> (hsub + lowres)) - 1, idx = 0; w >= 0; w--, idx += step) | |
| 316 | ✗ | dest_pcm[i][w] = src[idx]; | |
| 317 | ✗ | src += step * (16 >> hsub); | |
| 318 | ✗ | dest_pcm[i] -= linesize[i] / 2; | |
| 319 | } | ||
| 320 | } | ||
| 321 | } | ||
| 322 | 1350 | } | |
| 323 | |||
| 324 | /** | ||
| 325 | * Predict the ac. | ||
| 326 | * @param n block index (0-3 are luma, 4-5 are chroma) | ||
| 327 | * @param dir the ac prediction direction | ||
| 328 | */ | ||
| 329 | 1392912 | void ff_mpeg4_pred_ac(H263DecContext *const h, int16_t *block, int n, int dir) | |
| 330 | { | ||
| 331 | int i; | ||
| 332 | int16_t *ac_val, *ac_val1; | ||
| 333 | 1392912 | int8_t *const qscale_table = h->c.cur_pic.qscale_table; | |
| 334 | |||
| 335 | /* find prediction */ | ||
| 336 | 1392912 | ac_val = &h->c.ac_val[0][0] + h->c.block_index[n] * 16; | |
| 337 | 1392912 | ac_val1 = ac_val; | |
| 338 | 1392912 | if (h->c.ac_pred) { | |
| 339 | 30132 | if (dir == 0) { | |
| 340 | 20809 | const int xy = h->c.mb_x - 1 + h->c.mb_y * h->c.mb_stride; | |
| 341 | /* left prediction */ | ||
| 342 | 20809 | ac_val -= 16; | |
| 343 | |||
| 344 | 20809 | if (h->c.mb_x == 0 || h->c.qscale == qscale_table[xy] || | |
| 345 | ✗ | n == 1 || n == 3) { | |
| 346 | /* same qscale */ | ||
| 347 | 166472 | for (i = 1; i < 8; i++) | |
| 348 | 145663 | block[h->c.idsp.idct_permutation[i << 3]] += ac_val[i]; | |
| 349 | } else { | ||
| 350 | /* different qscale, we must rescale */ | ||
| 351 | ✗ | for (i = 1; i < 8; i++) | |
| 352 | ✗ | block[h->c.idsp.idct_permutation[i << 3]] += ROUNDED_DIV(ac_val[i] * qscale_table[xy], h->c.qscale); | |
| 353 | } | ||
| 354 | } else { | ||
| 355 | 9323 | const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride - h->c.mb_stride; | |
| 356 | /* top prediction */ | ||
| 357 | 9323 | ac_val -= 16 * h->c.block_wrap[n]; | |
| 358 | |||
| 359 | 9323 | if (h->c.mb_y == 0 || h->c.qscale == qscale_table[xy] || | |
| 360 | 1 | n == 2 || n == 3) { | |
| 361 | /* same qscale */ | ||
| 362 | 74584 | for (i = 1; i < 8; i++) | |
| 363 | 65261 | block[h->c.idsp.idct_permutation[i]] += ac_val[i + 8]; | |
| 364 | } else { | ||
| 365 | /* different qscale, we must rescale */ | ||
| 366 | ✗ | for (i = 1; i < 8; i++) | |
| 367 | ✗ | block[h->c.idsp.idct_permutation[i]] += ROUNDED_DIV(ac_val[i + 8] * qscale_table[xy], h->c.qscale); | |
| 368 | } | ||
| 369 | } | ||
| 370 | } | ||
| 371 | /* left copy */ | ||
| 372 | 11143296 | for (i = 1; i < 8; i++) | |
| 373 | 9750384 | ac_val1[i] = block[h->c.idsp.idct_permutation[i << 3]]; | |
| 374 | |||
| 375 | /* top copy */ | ||
| 376 | 11143296 | for (i = 1; i < 8; i++) | |
| 377 | 9750384 | ac_val1[8 + i] = block[h->c.idsp.idct_permutation[i]]; | |
| 378 | 1392912 | } | |
| 379 | |||
| 380 | /** | ||
| 381 | * check if the next stuff is a resync marker or the end. | ||
| 382 | * @return 0 if not | ||
| 383 | */ | ||
| 384 | 1599116 | static inline int mpeg4_is_resync(Mpeg4DecContext *ctx) | |
| 385 | { | ||
| 386 | 1599116 | H263DecContext *const h = &ctx->h; | |
| 387 | 1599116 | int bits_count = get_bits_count(&h->gb); | |
| 388 | 1599116 | int v = show_bits(&h->gb, 16); | |
| 389 | |||
| 390 | 1599116 | if (h->c.workaround_bugs & FF_BUG_NO_PADDING && !ctx->resync_marker) | |
| 391 | ✗ | return 0; | |
| 392 | |||
| 393 | 1599116 | while (v <= 0xFF) { | |
| 394 | 5423 | if (h->c.pict_type == AV_PICTURE_TYPE_B || | |
| 395 | 4448 | (v >> (8 - h->c.pict_type) != 1) || h->partitioned_frame) | |
| 396 | break; | ||
| 397 | ✗ | skip_bits(&h->gb, 8 + h->c.pict_type); | |
| 398 | ✗ | bits_count += 8 + h->c.pict_type; | |
| 399 | ✗ | v = show_bits(&h->gb, 16); | |
| 400 | } | ||
| 401 | |||
| 402 | 1599116 | if (bits_count + 8 >= h->gb.size_in_bits) { | |
| 403 | 16660 | v >>= 8; | |
| 404 | 16660 | v |= 0x7F >> (7 - (bits_count & 7)); | |
| 405 | |||
| 406 | 16660 | if (v == 0x7F) | |
| 407 | 13264 | return h->c.mb_num; | |
| 408 | } else { | ||
| 409 | static const uint16_t mpeg4_resync_prefix[8] = { | ||
| 410 | 0x7F00, 0x7E00, 0x7C00, 0x7800, 0x7000, 0x6000, 0x4000, 0x0000 | ||
| 411 | }; | ||
| 412 | |||
| 413 | 1582456 | if (v == mpeg4_resync_prefix[bits_count & 7]) { | |
| 414 | int len, mb_num; | ||
| 415 | 11338 | int mb_num_bits = av_log2(h->c.mb_num - 1) + 1; | |
| 416 | 11338 | GetBitContext gb = h->gb; | |
| 417 | |||
| 418 | 11338 | skip_bits(&h->gb, 1); | |
| 419 | 11338 | align_get_bits(&h->gb); | |
| 420 | |||
| 421 | 195291 | for (len = 0; len < 32; len++) | |
| 422 | 195291 | if (get_bits1(&h->gb)) | |
| 423 | 11338 | break; | |
| 424 | |||
| 425 | 11338 | mb_num = get_bits(&h->gb, mb_num_bits); | |
| 426 | 11338 | if (!mb_num || mb_num > h->c.mb_num || get_bits_count(&h->gb) + 6 > h->gb.size_in_bits) | |
| 427 | ✗ | mb_num= -1; | |
| 428 | |||
| 429 | 11338 | h->gb = gb; | |
| 430 | |||
| 431 | 11338 | if (len >= ff_mpeg4_get_video_packet_prefix_length(h->c.pict_type, ctx->f_code, ctx->b_code)) | |
| 432 | 11337 | return mb_num; | |
| 433 | } | ||
| 434 | } | ||
| 435 | 1574515 | return 0; | |
| 436 | } | ||
| 437 | |||
| 438 | ✗ | static int mpeg4_decode_sprite_trajectory(Mpeg4DecContext *ctx, GetBitContext *gb) | |
| 439 | { | ||
| 440 | ✗ | MpegEncContext *s = &ctx->h.c; | |
| 441 | ✗ | int a = 2 << ctx->sprite_warping_accuracy; | |
| 442 | ✗ | int rho = 3 - ctx->sprite_warping_accuracy; | |
| 443 | ✗ | int r = 16 / a; | |
| 444 | ✗ | int alpha = 1; | |
| 445 | ✗ | int beta = 0; | |
| 446 | ✗ | int w = s->width; | |
| 447 | ✗ | int h = s->height; | |
| 448 | int min_ab, i, w2, h2, w3, h3; | ||
| 449 | int sprite_ref[4][2]; | ||
| 450 | int virtual_ref[2][2]; | ||
| 451 | int64_t sprite_offset[2][2]; | ||
| 452 | int64_t sprite_delta[2][2]; | ||
| 453 | |||
| 454 | // only true for rectangle shapes | ||
| 455 | ✗ | const int vop_ref[4][2] = { { 0, 0 }, { s->width, 0 }, | |
| 456 | ✗ | { 0, s->height }, { s->width, s->height } }; | |
| 457 | ✗ | int d[4][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 } }; | |
| 458 | |||
| 459 | ✗ | if (w <= 0 || h <= 0) | |
| 460 | ✗ | return AVERROR_INVALIDDATA; | |
| 461 | |||
| 462 | ✗ | for (i = 0; i < ctx->num_sprite_warping_points; i++) { | |
| 463 | int length; | ||
| 464 | ✗ | int x = 0, y = 0; | |
| 465 | |||
| 466 | ✗ | length = get_vlc2(gb, sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 2); | |
| 467 | ✗ | if (length > 0) | |
| 468 | ✗ | x = get_xbits(gb, length); | |
| 469 | |||
| 470 | ✗ | if (!(ctx->divx_version == 500 && ctx->divx_build == 413)) | |
| 471 | ✗ | check_marker(s->avctx, gb, "before sprite_trajectory"); | |
| 472 | |||
| 473 | ✗ | length = get_vlc2(gb, sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 2); | |
| 474 | ✗ | if (length > 0) | |
| 475 | ✗ | y = get_xbits(gb, length); | |
| 476 | |||
| 477 | ✗ | check_marker(s->avctx, gb, "after sprite_trajectory"); | |
| 478 | ✗ | ctx->sprite_traj[i][0] = d[i][0] = x; | |
| 479 | ✗ | ctx->sprite_traj[i][1] = d[i][1] = y; | |
| 480 | } | ||
| 481 | ✗ | for (; i < 4; i++) | |
| 482 | ✗ | ctx->sprite_traj[i][0] = ctx->sprite_traj[i][1] = 0; | |
| 483 | |||
| 484 | ✗ | while ((1 << alpha) < w) | |
| 485 | ✗ | alpha++; | |
| 486 | ✗ | while ((1 << beta) < h) | |
| 487 | ✗ | beta++; /* typo in the MPEG-4 std for the definition of w' and h' */ | |
| 488 | ✗ | w2 = 1 << alpha; | |
| 489 | ✗ | h2 = 1 << beta; | |
| 490 | |||
| 491 | // Note, the 4th point isn't used for GMC | ||
| 492 | ✗ | if (ctx->divx_version == 500 && ctx->divx_build == 413) { | |
| 493 | ✗ | sprite_ref[0][0] = a * vop_ref[0][0] + d[0][0]; | |
| 494 | ✗ | sprite_ref[0][1] = a * vop_ref[0][1] + d[0][1]; | |
| 495 | ✗ | sprite_ref[1][0] = a * vop_ref[1][0] + d[0][0] + d[1][0]; | |
| 496 | ✗ | sprite_ref[1][1] = a * vop_ref[1][1] + d[0][1] + d[1][1]; | |
| 497 | ✗ | sprite_ref[2][0] = a * vop_ref[2][0] + d[0][0] + d[2][0]; | |
| 498 | ✗ | sprite_ref[2][1] = a * vop_ref[2][1] + d[0][1] + d[2][1]; | |
| 499 | } else { | ||
| 500 | ✗ | sprite_ref[0][0] = (a >> 1) * (2 * vop_ref[0][0] + d[0][0]); | |
| 501 | ✗ | sprite_ref[0][1] = (a >> 1) * (2 * vop_ref[0][1] + d[0][1]); | |
| 502 | ✗ | sprite_ref[1][0] = (a >> 1) * (2 * vop_ref[1][0] + d[0][0] + d[1][0]); | |
| 503 | ✗ | sprite_ref[1][1] = (a >> 1) * (2 * vop_ref[1][1] + d[0][1] + d[1][1]); | |
| 504 | ✗ | sprite_ref[2][0] = (a >> 1) * (2 * vop_ref[2][0] + d[0][0] + d[2][0]); | |
| 505 | ✗ | sprite_ref[2][1] = (a >> 1) * (2 * vop_ref[2][1] + d[0][1] + d[2][1]); | |
| 506 | } | ||
| 507 | /* sprite_ref[3][0] = (a >> 1) * (2 * vop_ref[3][0] + d[0][0] + d[1][0] + d[2][0] + d[3][0]); | ||
| 508 | * sprite_ref[3][1] = (a >> 1) * (2 * vop_ref[3][1] + d[0][1] + d[1][1] + d[2][1] + d[3][1]); */ | ||
| 509 | |||
| 510 | /* This is mostly identical to the MPEG-4 std (and is totally unreadable | ||
| 511 | * because of that...). Perhaps it should be reordered to be more readable. | ||
| 512 | * The idea behind this virtual_ref mess is to be able to use shifts later | ||
| 513 | * per pixel instead of divides so the distance between points is converted | ||
| 514 | * from w&h based to w2&h2 based which are of the 2^x form. */ | ||
| 515 | ✗ | virtual_ref[0][0] = 16 * (vop_ref[0][0] + w2) + | |
| 516 | ✗ | ROUNDED_DIV(((w - w2) * | |
| 517 | (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) + | ||
| 518 | w2 * (r * sprite_ref[1][0] - 16LL * vop_ref[1][0])), w); | ||
| 519 | ✗ | virtual_ref[0][1] = 16 * vop_ref[0][1] + | |
| 520 | ✗ | ROUNDED_DIV(((w - w2) * | |
| 521 | (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) + | ||
| 522 | w2 * (r * sprite_ref[1][1] - 16LL * vop_ref[1][1])), w); | ||
| 523 | ✗ | virtual_ref[1][0] = 16 * vop_ref[0][0] + | |
| 524 | ✗ | ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][0] - 16LL * vop_ref[0][0]) + | |
| 525 | h2 * (r * sprite_ref[2][0] - 16LL * vop_ref[2][0])), h); | ||
| 526 | ✗ | virtual_ref[1][1] = 16 * (vop_ref[0][1] + h2) + | |
| 527 | ✗ | ROUNDED_DIV(((h - h2) * (r * sprite_ref[0][1] - 16LL * vop_ref[0][1]) + | |
| 528 | h2 * (r * sprite_ref[2][1] - 16LL * vop_ref[2][1])), h); | ||
| 529 | |||
| 530 | ✗ | switch (ctx->num_sprite_warping_points) { | |
| 531 | ✗ | case 0: | |
| 532 | ✗ | sprite_offset[0][0] = | |
| 533 | ✗ | sprite_offset[0][1] = | |
| 534 | ✗ | sprite_offset[1][0] = | |
| 535 | ✗ | sprite_offset[1][1] = 0; | |
| 536 | ✗ | sprite_delta[0][0] = a; | |
| 537 | ✗ | sprite_delta[0][1] = | |
| 538 | ✗ | sprite_delta[1][0] = 0; | |
| 539 | ✗ | sprite_delta[1][1] = a; | |
| 540 | ✗ | ctx->sprite_shift[0] = | |
| 541 | ✗ | ctx->sprite_shift[1] = 0; | |
| 542 | ✗ | break; | |
| 543 | ✗ | case 1: // GMC only | |
| 544 | ✗ | sprite_offset[0][0] = sprite_ref[0][0] - a * vop_ref[0][0]; | |
| 545 | ✗ | sprite_offset[0][1] = sprite_ref[0][1] - a * vop_ref[0][1]; | |
| 546 | ✗ | sprite_offset[1][0] = ((sprite_ref[0][0] >> 1) | (sprite_ref[0][0] & 1)) - | |
| 547 | ✗ | a * (vop_ref[0][0] / 2); | |
| 548 | ✗ | sprite_offset[1][1] = ((sprite_ref[0][1] >> 1) | (sprite_ref[0][1] & 1)) - | |
| 549 | ✗ | a * (vop_ref[0][1] / 2); | |
| 550 | ✗ | sprite_delta[0][0] = a; | |
| 551 | ✗ | sprite_delta[0][1] = | |
| 552 | ✗ | sprite_delta[1][0] = 0; | |
| 553 | ✗ | sprite_delta[1][1] = a; | |
| 554 | ✗ | ctx->sprite_shift[0] = | |
| 555 | ✗ | ctx->sprite_shift[1] = 0; | |
| 556 | ✗ | break; | |
| 557 | ✗ | case 2: | |
| 558 | ✗ | sprite_offset[0][0] = ((int64_t) sprite_ref[0][0] * (1 << alpha + rho)) + | |
| 559 | ✗ | ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) * | |
| 560 | ✗ | ((int64_t) -vop_ref[0][0]) + | |
| 561 | ✗ | ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) * | |
| 562 | ✗ | ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1)); | |
| 563 | ✗ | sprite_offset[0][1] = ((int64_t) sprite_ref[0][1] * (1 << alpha + rho)) + | |
| 564 | ✗ | ((int64_t) -r * sprite_ref[0][1] + virtual_ref[0][1]) * | |
| 565 | ✗ | ((int64_t) -vop_ref[0][0]) + | |
| 566 | ✗ | ((int64_t) -r * sprite_ref[0][0] + virtual_ref[0][0]) * | |
| 567 | ✗ | ((int64_t) -vop_ref[0][1]) + (1 << (alpha + rho - 1)); | |
| 568 | ✗ | sprite_offset[1][0] = (((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * | |
| 569 | ✗ | ((int64_t)-2 * vop_ref[0][0] + 1) + | |
| 570 | ✗ | ((int64_t) r * sprite_ref[0][1] - virtual_ref[0][1]) * | |
| 571 | ✗ | ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r * | |
| 572 | ✗ | (int64_t) sprite_ref[0][0] - 16 * w2 + (1 << (alpha + rho + 1))); | |
| 573 | ✗ | sprite_offset[1][1] = (((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * | |
| 574 | ✗ | ((int64_t)-2 * vop_ref[0][0] + 1) + | |
| 575 | ✗ | ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * | |
| 576 | ✗ | ((int64_t)-2 * vop_ref[0][1] + 1) + 2 * w2 * r * | |
| 577 | ✗ | (int64_t) sprite_ref[0][1] - 16 * w2 + (1 << (alpha + rho + 1))); | |
| 578 | ✗ | sprite_delta[0][0] = (-r * sprite_ref[0][0] + virtual_ref[0][0]); | |
| 579 | ✗ | sprite_delta[0][1] = (+r * sprite_ref[0][1] - virtual_ref[0][1]); | |
| 580 | ✗ | sprite_delta[1][0] = (-r * sprite_ref[0][1] + virtual_ref[0][1]); | |
| 581 | ✗ | sprite_delta[1][1] = (-r * sprite_ref[0][0] + virtual_ref[0][0]); | |
| 582 | |||
| 583 | ✗ | ctx->sprite_shift[0] = alpha + rho; | |
| 584 | ✗ | ctx->sprite_shift[1] = alpha + rho + 2; | |
| 585 | ✗ | break; | |
| 586 | ✗ | case 3: | |
| 587 | ✗ | min_ab = FFMIN(alpha, beta); | |
| 588 | ✗ | w3 = w2 >> min_ab; | |
| 589 | ✗ | h3 = h2 >> min_ab; | |
| 590 | ✗ | sprite_offset[0][0] = ((int64_t)sprite_ref[0][0] * (1 << (alpha + beta + rho - min_ab))) + | |
| 591 | ✗ | ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-vop_ref[0][0]) + | |
| 592 | ✗ | ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-vop_ref[0][1]) + | |
| 593 | ✗ | ((int64_t)1 << (alpha + beta + rho - min_ab - 1)); | |
| 594 | ✗ | sprite_offset[0][1] = ((int64_t)sprite_ref[0][1] * (1 << (alpha + beta + rho - min_ab))) + | |
| 595 | ✗ | ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-vop_ref[0][0]) + | |
| 596 | ✗ | ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-vop_ref[0][1]) + | |
| 597 | ✗ | ((int64_t)1 << (alpha + beta + rho - min_ab - 1)); | |
| 598 | ✗ | sprite_offset[1][0] = ((int64_t)-r * sprite_ref[0][0] + virtual_ref[0][0]) * h3 * (-2 * vop_ref[0][0] + 1) + | |
| 599 | ✗ | ((int64_t)-r * sprite_ref[0][0] + virtual_ref[1][0]) * w3 * (-2 * vop_ref[0][1] + 1) + | |
| 600 | ✗ | (int64_t)2 * w2 * h3 * r * sprite_ref[0][0] - 16 * w2 * h3 + | |
| 601 | ✗ | ((int64_t)1 << (alpha + beta + rho - min_ab + 1)); | |
| 602 | ✗ | sprite_offset[1][1] = ((int64_t)-r * sprite_ref[0][1] + virtual_ref[0][1]) * h3 * (-2 * vop_ref[0][0] + 1) + | |
| 603 | ✗ | ((int64_t)-r * sprite_ref[0][1] + virtual_ref[1][1]) * w3 * (-2 * vop_ref[0][1] + 1) + | |
| 604 | ✗ | (int64_t)2 * w2 * h3 * r * sprite_ref[0][1] - 16 * w2 * h3 + | |
| 605 | ✗ | ((int64_t)1 << (alpha + beta + rho - min_ab + 1)); | |
| 606 | ✗ | sprite_delta[0][0] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[0][0]) * h3; | |
| 607 | ✗ | sprite_delta[0][1] = (-r * (int64_t)sprite_ref[0][0] + virtual_ref[1][0]) * w3; | |
| 608 | ✗ | sprite_delta[1][0] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[0][1]) * h3; | |
| 609 | ✗ | sprite_delta[1][1] = (-r * (int64_t)sprite_ref[0][1] + virtual_ref[1][1]) * w3; | |
| 610 | |||
| 611 | ✗ | ctx->sprite_shift[0] = alpha + beta + rho - min_ab; | |
| 612 | ✗ | ctx->sprite_shift[1] = alpha + beta + rho - min_ab + 2; | |
| 613 | ✗ | break; | |
| 614 | ✗ | default: | |
| 615 | ✗ | av_unreachable("num_sprite_warping_points outside of 0..3 results in an error" | |
| 616 | "in which num_sprite_warping_points is reset to zero"); | ||
| 617 | } | ||
| 618 | /* try to simplify the situation */ | ||
| 619 | ✗ | if (sprite_delta[0][0] == a << ctx->sprite_shift[0] && | |
| 620 | ✗ | sprite_delta[0][1] == 0 && | |
| 621 | ✗ | sprite_delta[1][0] == 0 && | |
| 622 | ✗ | sprite_delta[1][1] == a << ctx->sprite_shift[0]) { | |
| 623 | ✗ | sprite_offset[0][0] >>= ctx->sprite_shift[0]; | |
| 624 | ✗ | sprite_offset[0][1] >>= ctx->sprite_shift[0]; | |
| 625 | ✗ | sprite_offset[1][0] >>= ctx->sprite_shift[1]; | |
| 626 | ✗ | sprite_offset[1][1] >>= ctx->sprite_shift[1]; | |
| 627 | ✗ | sprite_delta[0][0] = a; | |
| 628 | ✗ | sprite_delta[0][1] = 0; | |
| 629 | ✗ | sprite_delta[1][0] = 0; | |
| 630 | ✗ | sprite_delta[1][1] = a; | |
| 631 | ✗ | ctx->sprite_shift[0] = 0; | |
| 632 | ✗ | ctx->sprite_shift[1] = 0; | |
| 633 | ✗ | ctx->real_sprite_warping_points = 1; | |
| 634 | } else { | ||
| 635 | ✗ | int shift_y = 16 - ctx->sprite_shift[0]; | |
| 636 | ✗ | int shift_c = 16 - ctx->sprite_shift[1]; | |
| 637 | |||
| 638 | ✗ | for (i = 0; i < 2; i++) { | |
| 639 | ✗ | if (shift_c < 0 || shift_y < 0 || | |
| 640 | ✗ | FFABS( sprite_offset[0][i]) >= INT_MAX >> shift_y || | |
| 641 | ✗ | FFABS( sprite_offset[1][i]) >= INT_MAX >> shift_c || | |
| 642 | ✗ | FFABS( sprite_delta[0][i]) >= INT_MAX >> shift_y || | |
| 643 | ✗ | FFABS( sprite_delta[1][i]) >= INT_MAX >> shift_y | |
| 644 | ) { | ||
| 645 | ✗ | avpriv_request_sample(s->avctx, "Too large sprite shift, delta or offset"); | |
| 646 | ✗ | goto overflow; | |
| 647 | } | ||
| 648 | } | ||
| 649 | |||
| 650 | ✗ | for (i = 0; i < 2; i++) { | |
| 651 | ✗ | sprite_offset[0][i] *= 1 << shift_y; | |
| 652 | ✗ | sprite_offset[1][i] *= 1 << shift_c; | |
| 653 | ✗ | sprite_delta[0][i] *= 1 << shift_y; | |
| 654 | ✗ | sprite_delta[1][i] *= 1 << shift_y; | |
| 655 | ✗ | ctx->sprite_shift[i] = 16; | |
| 656 | |||
| 657 | } | ||
| 658 | ✗ | for (i = 0; i < 2; i++) { | |
| 659 | ✗ | int64_t sd[2] = { | |
| 660 | ✗ | sprite_delta[i][0] - a * (1LL<<16), | |
| 661 | ✗ | sprite_delta[i][1] - a * (1LL<<16) | |
| 662 | }; | ||
| 663 | |||
| 664 | ✗ | if (llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL)) >= INT_MAX || | |
| 665 | ✗ | llabs(sprite_offset[0][i] + sprite_delta[i][1] * (h+16LL)) >= INT_MAX || | |
| 666 | ✗ | llabs(sprite_offset[0][i] + sprite_delta[i][0] * (w+16LL) + sprite_delta[i][1] * (h+16LL)) >= INT_MAX || | |
| 667 | ✗ | llabs(sprite_delta[i][0] * (w+16LL)) >= INT_MAX || | |
| 668 | ✗ | llabs(sprite_delta[i][1] * (h+16LL)) >= INT_MAX || | |
| 669 | ✗ | llabs(sd[0]) >= INT_MAX || | |
| 670 | ✗ | llabs(sd[1]) >= INT_MAX || | |
| 671 | ✗ | llabs(sprite_offset[0][i] + sd[0] * (w+16LL)) >= INT_MAX || | |
| 672 | ✗ | llabs(sprite_offset[0][i] + sd[1] * (h+16LL)) >= INT_MAX || | |
| 673 | ✗ | llabs(sprite_offset[0][i] + sd[0] * (w+16LL) + sd[1] * (h+16LL)) >= INT_MAX | |
| 674 | ) { | ||
| 675 | ✗ | avpriv_request_sample(s->avctx, "Overflow on sprite points"); | |
| 676 | ✗ | goto overflow; | |
| 677 | } | ||
| 678 | } | ||
| 679 | ✗ | ctx->real_sprite_warping_points = ctx->num_sprite_warping_points; | |
| 680 | } | ||
| 681 | |||
| 682 | ✗ | for (i = 0; i < 4; i++) { | |
| 683 | ✗ | ctx->sprite_offset[i&1][i>>1] = sprite_offset[i&1][i>>1]; | |
| 684 | ✗ | ctx->sprite_delta [i&1][i>>1] = sprite_delta [i&1][i>>1]; | |
| 685 | } | ||
| 686 | |||
| 687 | ✗ | return 0; | |
| 688 | ✗ | overflow: | |
| 689 | ✗ | memset(ctx->sprite_offset, 0, sizeof(ctx->sprite_offset)); | |
| 690 | ✗ | memset(ctx->sprite_delta, 0, sizeof(ctx->sprite_delta)); | |
| 691 | ✗ | return AVERROR_PATCHWELCOME; | |
| 692 | } | ||
| 693 | |||
| 694 | ✗ | static int decode_new_pred(Mpeg4DecContext *ctx, GetBitContext *gb) { | |
| 695 | ✗ | int len = FFMIN(ctx->time_increment_bits + 3, 15); | |
| 696 | |||
| 697 | ✗ | get_bits(gb, len); | |
| 698 | ✗ | if (get_bits1(gb)) | |
| 699 | ✗ | get_bits(gb, len); | |
| 700 | ✗ | check_marker(ctx->h.c.avctx, gb, "after new_pred"); | |
| 701 | |||
| 702 | ✗ | return 0; | |
| 703 | } | ||
| 704 | |||
| 705 | /** | ||
| 706 | * Decode the next video packet. | ||
| 707 | * @return <0 if something went wrong | ||
| 708 | */ | ||
| 709 | 11140 | int ff_mpeg4_decode_video_packet_header(H263DecContext *const h) | |
| 710 | { | ||
| 711 | 11140 | Mpeg4DecContext *const ctx = h263_to_mpeg4(h); | |
| 712 | |||
| 713 | 11140 | int mb_num_bits = av_log2(h->c.mb_num - 1) + 1; | |
| 714 | 11140 | int header_extension = 0, mb_num, len; | |
| 715 | |||
| 716 | /* is there enough space left for a video packet + header */ | ||
| 717 | 11140 | if (get_bits_count(&h->gb) > h->gb.size_in_bits - 20) | |
| 718 | ✗ | return AVERROR_INVALIDDATA; | |
| 719 | |||
| 720 | 191886 | for (len = 0; len < 32; len++) | |
| 721 | 191886 | if (get_bits1(&h->gb)) | |
| 722 | 11140 | break; | |
| 723 | |||
| 724 | 11140 | if (len != ff_mpeg4_get_video_packet_prefix_length(h->c.pict_type, ctx->f_code, ctx->b_code)) { | |
| 725 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "marker does not match f_code\n"); | |
| 726 | ✗ | return AVERROR_INVALIDDATA; | |
| 727 | } | ||
| 728 | |||
| 729 | 11140 | if (ctx->shape != RECT_SHAPE) { | |
| 730 | ✗ | header_extension = get_bits1(&h->gb); | |
| 731 | // FIXME more stuff here | ||
| 732 | } | ||
| 733 | |||
| 734 | 11140 | mb_num = get_bits(&h->gb, mb_num_bits); | |
| 735 | 11140 | if (mb_num >= h->c.mb_num || !mb_num) { | |
| 736 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 737 | "illegal mb_num in video packet (%d %d) \n", mb_num, h->c.mb_num); | ||
| 738 | ✗ | return AVERROR_INVALIDDATA; | |
| 739 | } | ||
| 740 | |||
| 741 | 11140 | h->c.mb_x = mb_num % h->c.mb_width; | |
| 742 | 11140 | h->c.mb_y = mb_num / h->c.mb_width; | |
| 743 | |||
| 744 | 11140 | if (ctx->shape != BIN_ONLY_SHAPE) { | |
| 745 | 11140 | int qscale = get_bits(&h->gb, ctx->quant_precision); | |
| 746 | 11140 | if (qscale) | |
| 747 | 11140 | h->c.chroma_qscale = h->c.qscale = qscale; | |
| 748 | } | ||
| 749 | |||
| 750 | 11140 | if (ctx->shape == RECT_SHAPE) | |
| 751 | 11140 | header_extension = get_bits1(&h->gb); | |
| 752 | |||
| 753 | 11140 | if (header_extension) { | |
| 754 | ✗ | while (get_bits1(&h->gb) != 0) | |
| 755 | ; | ||
| 756 | |||
| 757 | ✗ | check_marker(h->c.avctx, &h->gb, "before time_increment in video packed header"); | |
| 758 | ✗ | skip_bits(&h->gb, ctx->time_increment_bits); /* time_increment */ | |
| 759 | ✗ | check_marker(h->c.avctx, &h->gb, "before vop_coding_type in video packed header"); | |
| 760 | |||
| 761 | ✗ | skip_bits(&h->gb, 2); /* vop coding type */ | |
| 762 | // FIXME not rect stuff here | ||
| 763 | |||
| 764 | ✗ | if (ctx->shape != BIN_ONLY_SHAPE) { | |
| 765 | ✗ | skip_bits(&h->gb, 3); /* intra dc vlc threshold */ | |
| 766 | // FIXME don't just ignore everything | ||
| 767 | ✗ | if (h->c.pict_type == AV_PICTURE_TYPE_S && | |
| 768 | ✗ | ctx->vol_sprite_usage == GMC_SPRITE) { | |
| 769 | ✗ | if (mpeg4_decode_sprite_trajectory(ctx, &h->gb) < 0) | |
| 770 | ✗ | return AVERROR_INVALIDDATA; | |
| 771 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "untested\n"); | |
| 772 | } | ||
| 773 | |||
| 774 | // FIXME reduced res stuff here | ||
| 775 | |||
| 776 | ✗ | if (h->c.pict_type != AV_PICTURE_TYPE_I) { | |
| 777 | ✗ | int f_code = get_bits(&h->gb, 3); /* fcode_for */ | |
| 778 | ✗ | if (f_code == 0) | |
| 779 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 780 | "Error, video packet header damaged (f_code=0)\n"); | ||
| 781 | } | ||
| 782 | ✗ | if (h->c.pict_type == AV_PICTURE_TYPE_B) { | |
| 783 | ✗ | int b_code = get_bits(&h->gb, 3); | |
| 784 | ✗ | if (b_code == 0) | |
| 785 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 786 | "Error, video packet header damaged (b_code=0)\n"); | ||
| 787 | } | ||
| 788 | } | ||
| 789 | } | ||
| 790 | 11140 | if (ctx->new_pred) | |
| 791 | ✗ | decode_new_pred(ctx, &h->gb); | |
| 792 | |||
| 793 | 11140 | return 0; | |
| 794 | } | ||
| 795 | |||
| 796 | 30 | static void reset_studio_dc_predictors(Mpeg4DecContext *const ctx) | |
| 797 | { | ||
| 798 | 30 | H263DecContext *const h = &ctx->h; | |
| 799 | /* Reset DC Predictors */ | ||
| 800 | 30 | h->last_dc[0] = | |
| 801 | 30 | h->last_dc[1] = | |
| 802 | 30 | h->last_dc[2] = 1 << (h->c.avctx->bits_per_raw_sample + ctx->dct_precision + h->c.intra_dc_precision - 1); | |
| 803 | 30 | } | |
| 804 | |||
| 805 | /** | ||
| 806 | * Decode the next video packet. | ||
| 807 | * @return <0 if something went wrong | ||
| 808 | */ | ||
| 809 | 30 | int ff_mpeg4_decode_studio_slice_header(H263DecContext *const h) | |
| 810 | { | ||
| 811 | 30 | Mpeg4DecContext *const ctx = h263_to_mpeg4(h); | |
| 812 | 30 | GetBitContext *gb = &h->gb; | |
| 813 | unsigned vlc_len; | ||
| 814 | uint16_t mb_num; | ||
| 815 | |||
| 816 | 30 | if (get_bits_left(gb) >= 32 && get_bits_long(gb, 32) == SLICE_STARTCODE) { | |
| 817 | 30 | vlc_len = av_log2(h->c.mb_width * h->c.mb_height) + 1; | |
| 818 | 30 | mb_num = get_bits(gb, vlc_len); | |
| 819 | |||
| 820 | 30 | if (mb_num >= h->c.mb_num) | |
| 821 | ✗ | return AVERROR_INVALIDDATA; | |
| 822 | |||
| 823 | 30 | h->c.mb_x = mb_num % h->c.mb_width; | |
| 824 | 30 | h->c.mb_y = mb_num / h->c.mb_width; | |
| 825 | |||
| 826 | 30 | if (ctx->shape != BIN_ONLY_SHAPE) | |
| 827 | 30 | h->c.qscale = mpeg_get_qscale(&h->gb, h->c.q_scale_type); | |
| 828 | |||
| 829 | 30 | if (get_bits1(gb)) { /* slice_extension_flag */ | |
| 830 | ✗ | skip_bits1(gb); /* intra_slice */ | |
| 831 | ✗ | skip_bits1(gb); /* slice_VOP_id_enable */ | |
| 832 | ✗ | skip_bits(gb, 6); /* slice_VOP_id */ | |
| 833 | ✗ | while (get_bits1(gb)) /* extra_bit_slice */ | |
| 834 | ✗ | skip_bits(gb, 8); /* extra_information_slice */ | |
| 835 | } | ||
| 836 | |||
| 837 | 30 | reset_studio_dc_predictors(ctx); | |
| 838 | } | ||
| 839 | else { | ||
| 840 | ✗ | return AVERROR_INVALIDDATA; | |
| 841 | } | ||
| 842 | |||
| 843 | 30 | return 0; | |
| 844 | } | ||
| 845 | |||
| 846 | /** | ||
| 847 | * Get the average motion vector for a GMC MB. | ||
| 848 | * @param n either 0 for the x component or 1 for y | ||
| 849 | * @return the average MV for a GMC MB | ||
| 850 | */ | ||
| 851 | ✗ | static inline int get_amv(Mpeg4DecContext *ctx, int n) | |
| 852 | { | ||
| 853 | ✗ | MPVContext *const s = &ctx->h.c; | |
| 854 | int x, y, mb_v, sum, dx, dy, shift; | ||
| 855 | ✗ | int len = 1 << (ctx->f_code + 4); | |
| 856 | ✗ | const int a = ctx->sprite_warping_accuracy; | |
| 857 | |||
| 858 | ✗ | if (s->workaround_bugs & FF_BUG_AMV) | |
| 859 | ✗ | len >>= s->quarter_sample; | |
| 860 | |||
| 861 | ✗ | if (ctx->real_sprite_warping_points == 1) { | |
| 862 | ✗ | if (ctx->divx_version == 500 && ctx->divx_build == 413 && a >= s->quarter_sample) | |
| 863 | ✗ | sum = ctx->sprite_offset[0][n] / (1 << (a - s->quarter_sample)); | |
| 864 | else | ||
| 865 | ✗ | sum = RSHIFT(ctx->sprite_offset[0][n] * (1 << s->quarter_sample), a); | |
| 866 | } else { | ||
| 867 | ✗ | dx = ctx->sprite_delta[n][0]; | |
| 868 | ✗ | dy = ctx->sprite_delta[n][1]; | |
| 869 | ✗ | shift = ctx->sprite_shift[0]; | |
| 870 | ✗ | if (n) | |
| 871 | ✗ | dy -= 1 << (shift + a + 1); | |
| 872 | else | ||
| 873 | ✗ | dx -= 1 << (shift + a + 1); | |
| 874 | ✗ | mb_v = ctx->sprite_offset[0][n] + dx * s->mb_x * 16U + dy * s->mb_y * 16U; | |
| 875 | |||
| 876 | ✗ | sum = 0; | |
| 877 | ✗ | for (y = 0; y < 16; y++) { | |
| 878 | int v; | ||
| 879 | |||
| 880 | ✗ | v = mb_v + (unsigned)dy * y; | |
| 881 | // FIXME optimize | ||
| 882 | ✗ | for (x = 0; x < 16; x++) { | |
| 883 | ✗ | sum += v >> shift; | |
| 884 | ✗ | v += dx; | |
| 885 | } | ||
| 886 | } | ||
| 887 | ✗ | sum = RSHIFT(sum, a + 8 - s->quarter_sample); | |
| 888 | } | ||
| 889 | |||
| 890 | ✗ | if (sum < -len) | |
| 891 | ✗ | sum = -len; | |
| 892 | ✗ | else if (sum >= len) | |
| 893 | ✗ | sum = len - 1; | |
| 894 | |||
| 895 | ✗ | return sum; | |
| 896 | } | ||
| 897 | |||
| 898 | /** | ||
| 899 | * Predict the dc. | ||
| 900 | * @param n block index (0-3 are luma, 4-5 are chroma) | ||
| 901 | * @param dir_ptr pointer to an integer where the prediction direction will be stored | ||
| 902 | */ | ||
| 903 | 1162200 | static inline int mpeg4_pred_dc(MpegEncContext *s, int n, int *dir_ptr) | |
| 904 | { | ||
| 905 | 1162200 | const int16_t *const dc_val = s->dc_val + s->block_index[n]; | |
| 906 | 1162200 | const int wrap = s->block_wrap[n]; | |
| 907 | int pred; | ||
| 908 | |||
| 909 | /* find prediction */ | ||
| 910 | |||
| 911 | /* B C | ||
| 912 | * A X | ||
| 913 | */ | ||
| 914 | 1162200 | int a = dc_val[-1]; | |
| 915 | 1162200 | int b = dc_val[-1 - wrap]; | |
| 916 | 1162200 | int c = dc_val[-wrap]; | |
| 917 | |||
| 918 | /* outside slice handling (we can't do that by memset as we need the | ||
| 919 | * dc for error resilience) */ | ||
| 920 | 1162200 | if (s->first_slice_line && n != 3) { | |
| 921 | 127150 | if (n != 2) | |
| 922 | 101720 | b = c = 1024; | |
| 923 | 127150 | if (n != 1 && s->mb_x == s->resync_mb_x) | |
| 924 | 6828 | b = a = 1024; | |
| 925 | } | ||
| 926 | 1162200 | if (s->mb_x == s->resync_mb_x && s->mb_y == s->resync_mb_y + 1) { | |
| 927 | 6336 | if (n == 0 || n == 4 || n == 5) | |
| 928 | 3168 | b = 1024; | |
| 929 | } | ||
| 930 | |||
| 931 | 1162200 | if (abs(a - b) < abs(b - c)) { | |
| 932 | 441889 | pred = c; | |
| 933 | 441889 | *dir_ptr = 1; /* top */ | |
| 934 | } else { | ||
| 935 | 720311 | pred = a; | |
| 936 | 720311 | *dir_ptr = 0; /* left */ | |
| 937 | } | ||
| 938 | 1162200 | return pred; | |
| 939 | } | ||
| 940 | |||
| 941 | 1162200 | static inline int mpeg4_get_level_dc(MpegEncContext *s, int n, int pred, int level) | |
| 942 | { | ||
| 943 | 1162200 | int scale = n < 4 ? s->y_dc_scale : s->c_dc_scale; | |
| 944 | int ret; | ||
| 945 | |||
| 946 | if (IS_3IV1) | ||
| 947 | scale = 8; | ||
| 948 | |||
| 949 | /* we assume pred is positive */ | ||
| 950 | 1162200 | pred = FASTDIV((pred + (scale >> 1)), scale); | |
| 951 | |||
| 952 | 1162200 | level += pred; | |
| 953 | 1162200 | ret = level; | |
| 954 | 1162200 | level *= scale; | |
| 955 | 1162200 | if (level & (~2047)) { | |
| 956 | ✗ | if (s->avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_AGGRESSIVE)) { | |
| 957 | ✗ | if (level < 0) { | |
| 958 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
| 959 | "dc<0 at %dx%d\n", s->mb_x, s->mb_y); | ||
| 960 | ✗ | return AVERROR_INVALIDDATA; | |
| 961 | } | ||
| 962 | ✗ | if (level > 2048 + scale) { | |
| 963 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
| 964 | "dc overflow at %dx%d\n", s->mb_x, s->mb_y); | ||
| 965 | ✗ | return AVERROR_INVALIDDATA; | |
| 966 | } | ||
| 967 | } | ||
| 968 | ✗ | if (level < 0) | |
| 969 | ✗ | level = 0; | |
| 970 | ✗ | else if (!(s->workaround_bugs & FF_BUG_DC_CLIP)) | |
| 971 | ✗ | level = 2047; | |
| 972 | } | ||
| 973 | 1162200 | s->dc_val[s->block_index[n]] = level; | |
| 974 | |||
| 975 | 1162200 | return ret; | |
| 976 | } | ||
| 977 | |||
| 978 | /** | ||
| 979 | * Decode the dc value. | ||
| 980 | * @param n block index (0-3 are luma, 4-5 are chroma) | ||
| 981 | * @param dir_ptr the prediction direction will be stored here | ||
| 982 | * @return the quantized dc | ||
| 983 | */ | ||
| 984 | 1162200 | static inline int mpeg4_decode_dc(H263DecContext *const h, int n, int *dir_ptr) | |
| 985 | { | ||
| 986 | int level, code, pred; | ||
| 987 | |||
| 988 | 1162200 | if (n < 4) | |
| 989 | 774800 | code = get_vlc2(&h->gb, dc_lum, DC_VLC_BITS, 1); | |
| 990 | else | ||
| 991 | 387400 | code = get_vlc2(&h->gb, dc_chrom, DC_VLC_BITS, 1); | |
| 992 | |||
| 993 | 1162200 | if (code < 0) { | |
| 994 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "illegal dc vlc\n"); | |
| 995 | ✗ | return AVERROR_INVALIDDATA; | |
| 996 | } | ||
| 997 | |||
| 998 | 1162200 | if (code == 0) { | |
| 999 | 192419 | level = 0; | |
| 1000 | } else { | ||
| 1001 | if (IS_3IV1) { | ||
| 1002 | if (code == 1) | ||
| 1003 | level = 2 * get_bits1(&h->gb) - 1; | ||
| 1004 | else { | ||
| 1005 | if (get_bits1(&h->gb)) | ||
| 1006 | level = get_bits(&h->gb, code - 1) + (1 << (code - 1)); | ||
| 1007 | else | ||
| 1008 | level = -get_bits(&h->gb, code - 1) - (1 << (code - 1)); | ||
| 1009 | } | ||
| 1010 | } else { | ||
| 1011 | 969781 | level = get_xbits(&h->gb, code); | |
| 1012 | } | ||
| 1013 | |||
| 1014 | 969781 | if (code > 8) { | |
| 1015 | ✗ | if (get_bits1(&h->gb) == 0) { /* marker */ | |
| 1016 | ✗ | if (h->c.avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_COMPLIANT)) { | |
| 1017 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "dc marker bit missing\n"); | |
| 1018 | ✗ | return AVERROR_INVALIDDATA; | |
| 1019 | } | ||
| 1020 | } | ||
| 1021 | } | ||
| 1022 | } | ||
| 1023 | |||
| 1024 | 1162200 | pred = mpeg4_pred_dc(&h->c, n, dir_ptr); | |
| 1025 | 1162200 | return mpeg4_get_level_dc(&h->c, n, pred, level); | |
| 1026 | } | ||
| 1027 | |||
| 1028 | /** | ||
| 1029 | * Decode first partition. | ||
| 1030 | * @return number of MBs decoded or <0 if an error occurred | ||
| 1031 | */ | ||
| 1032 | 1729 | static int mpeg4_decode_partition_a(Mpeg4DecContext *ctx) | |
| 1033 | { | ||
| 1034 | 1729 | H263DecContext *const h = &ctx->h; | |
| 1035 | 1729 | int mb_num = 0; | |
| 1036 | static const int8_t quant_tab[4] = { -1, -2, 1, 2 }; | ||
| 1037 | |||
| 1038 | /* decode first partition */ | ||
| 1039 | 1729 | h->c.first_slice_line = 1; | |
| 1040 | 8345 | for (; h->c.mb_y < h->c.mb_height; h->c.mb_y++) { | |
| 1041 | 7873 | ff_init_block_index(&h->c); | |
| 1042 | 146645 | for (; h->c.mb_x < h->c.mb_width; h->c.mb_x++) { | |
| 1043 | 140029 | const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride; | |
| 1044 | int cbpc; | ||
| 1045 | 140029 | int dir = 0; | |
| 1046 | |||
| 1047 | 140029 | mb_num++; | |
| 1048 | 140029 | ff_update_block_index(&h->c, 8, h->c.avctx->lowres, 1); | |
| 1049 | 140029 | if (h->c.mb_x == h->c.resync_mb_x && h->c.mb_y == h->c.resync_mb_y + 1) | |
| 1050 | 1618 | h->c.first_slice_line = 0; | |
| 1051 | |||
| 1052 | 140029 | if (h->c.pict_type == AV_PICTURE_TYPE_I) { | |
| 1053 | int i; | ||
| 1054 | |||
| 1055 | do { | ||
| 1056 | 17860 | if (show_bits(&h->gb, 19) == DC_MARKER) | |
| 1057 | 439 | return mb_num - 1; | |
| 1058 | |||
| 1059 | 17421 | cbpc = get_vlc2(&h->gb, ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 2); | |
| 1060 | 17421 | if (cbpc < 0) { | |
| 1061 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1062 | "mcbpc corrupted at %d %d\n", h->c.mb_x, h->c.mb_y); | ||
| 1063 | ✗ | return AVERROR_INVALIDDATA; | |
| 1064 | } | ||
| 1065 | 17421 | } while (cbpc == 8); | |
| 1066 | |||
| 1067 | 17421 | h->c.cbp_table[xy] = cbpc & 3; | |
| 1068 | 17421 | h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA; | |
| 1069 | 17421 | h->c.mb_intra = 1; | |
| 1070 | |||
| 1071 | 17421 | if (cbpc & 4) | |
| 1072 | ✗ | ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]); | |
| 1073 | |||
| 1074 | 17421 | h->c.cur_pic.qscale_table[xy] = h->c.qscale; | |
| 1075 | |||
| 1076 | 17421 | h->c.mbintra_table[xy] = 1; | |
| 1077 | 121947 | for (i = 0; i < 6; i++) { | |
| 1078 | int dc_pred_dir; | ||
| 1079 | 104526 | int dc = mpeg4_decode_dc(h, i, &dc_pred_dir); | |
| 1080 | 104526 | if (dc < 0) { | |
| 1081 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1082 | "DC corrupted at %d %d\n", h->c.mb_x, h->c.mb_y); | ||
| 1083 | ✗ | return dc; | |
| 1084 | } | ||
| 1085 | 104526 | dir <<= 1; | |
| 1086 | 104526 | if (dc_pred_dir) | |
| 1087 | 30362 | dir |= 1; | |
| 1088 | } | ||
| 1089 | 17421 | h->c.pred_dir_table[xy] = dir; | |
| 1090 | } else { /* P/S_TYPE */ | ||
| 1091 | int mx, my, pred_x, pred_y, bits; | ||
| 1092 | 122169 | int16_t *const mot_val = h->c.cur_pic.motion_val[0][h->c.block_index[0]]; | |
| 1093 | 122169 | const int stride = h->c.b8_stride * 2; | |
| 1094 | |||
| 1095 | 122169 | try_again: | |
| 1096 | 122169 | bits = show_bits(&h->gb, 17); | |
| 1097 | 122169 | if (bits == MOTION_MARKER) | |
| 1098 | 818 | return mb_num - 1; | |
| 1099 | |||
| 1100 | 121351 | skip_bits1(&h->gb); | |
| 1101 | 121351 | if (bits & 0x10000) { | |
| 1102 | /* skip mb */ | ||
| 1103 | 1001 | if (h->c.pict_type == AV_PICTURE_TYPE_S && | |
| 1104 | ✗ | ctx->vol_sprite_usage == GMC_SPRITE) { | |
| 1105 | ✗ | h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | | |
| 1106 | MB_TYPE_16x16 | | ||
| 1107 | MB_TYPE_GMC | | ||
| 1108 | MB_TYPE_FORWARD_MV; | ||
| 1109 | ✗ | mx = get_amv(ctx, 0); | |
| 1110 | ✗ | my = get_amv(ctx, 1); | |
| 1111 | } else { | ||
| 1112 | 1001 | h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | | |
| 1113 | MB_TYPE_16x16 | | ||
| 1114 | MB_TYPE_FORWARD_MV; | ||
| 1115 | 1001 | mx = my = 0; | |
| 1116 | } | ||
| 1117 | 1001 | mot_val[0] = | |
| 1118 | 1001 | mot_val[2] = | |
| 1119 | 1001 | mot_val[0 + stride] = | |
| 1120 | 1001 | mot_val[2 + stride] = mx; | |
| 1121 | 1001 | mot_val[1] = | |
| 1122 | 1001 | mot_val[3] = | |
| 1123 | 1001 | mot_val[1 + stride] = | |
| 1124 | 1001 | mot_val[3 + stride] = my; | |
| 1125 | |||
| 1126 | 1001 | ff_h263_clean_intra_table_entries(&h->c, xy); | |
| 1127 | 1001 | continue; | |
| 1128 | } | ||
| 1129 | |||
| 1130 | 120350 | cbpc = get_vlc2(&h->gb, ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 2); | |
| 1131 | 120350 | if (cbpc < 0) { | |
| 1132 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1133 | "mcbpc corrupted at %d %d\n", h->c.mb_x, h->c.mb_y); | ||
| 1134 | ✗ | return AVERROR_INVALIDDATA; | |
| 1135 | } | ||
| 1136 | 120350 | if (cbpc == 20) | |
| 1137 | ✗ | goto try_again; | |
| 1138 | |||
| 1139 | 120350 | h->c.cbp_table[xy] = cbpc & (8 + 3); // 8 is dquant | |
| 1140 | |||
| 1141 | 120350 | h->c.mb_intra = ((cbpc & 4) != 0); | |
| 1142 | |||
| 1143 | 120350 | if (h->c.mb_intra) { | |
| 1144 | 6371 | h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA; | |
| 1145 | 6371 | h->c.mbintra_table[xy] = 1; | |
| 1146 | 6371 | mot_val[0] = | |
| 1147 | 6371 | mot_val[2] = | |
| 1148 | 6371 | mot_val[0 + stride] = | |
| 1149 | 6371 | mot_val[2 + stride] = 0; | |
| 1150 | 6371 | mot_val[1] = | |
| 1151 | 6371 | mot_val[3] = | |
| 1152 | 6371 | mot_val[1 + stride] = | |
| 1153 | 6371 | mot_val[3 + stride] = 0; | |
| 1154 | } else { | ||
| 1155 | 113979 | ff_h263_clean_intra_table_entries(&h->c, xy); | |
| 1156 | |||
| 1157 | 113979 | if (h->c.pict_type == AV_PICTURE_TYPE_S && | |
| 1158 | ✗ | ctx->vol_sprite_usage == GMC_SPRITE && | |
| 1159 | ✗ | (cbpc & 16) == 0) | |
| 1160 | ✗ | h->c.mcsel = get_bits1(&h->gb); | |
| 1161 | else | ||
| 1162 | 113979 | h->c.mcsel = 0; | |
| 1163 | |||
| 1164 | 113979 | if ((cbpc & 16) == 0) { | |
| 1165 | /* 16x16 motion prediction */ | ||
| 1166 | |||
| 1167 | 81941 | ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y); | |
| 1168 | 81941 | if (!h->c.mcsel) { | |
| 1169 | 81941 | mx = ff_h263_decode_motion(h, pred_x, ctx->f_code); | |
| 1170 | 81941 | if (mx >= 0xffff) | |
| 1171 | ✗ | return AVERROR_INVALIDDATA; | |
| 1172 | |||
| 1173 | 81941 | my = ff_h263_decode_motion(h, pred_y, ctx->f_code); | |
| 1174 | 81941 | if (my >= 0xffff) | |
| 1175 | ✗ | return AVERROR_INVALIDDATA; | |
| 1176 | 81941 | h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 | | |
| 1177 | MB_TYPE_FORWARD_MV; | ||
| 1178 | } else { | ||
| 1179 | ✗ | mx = get_amv(ctx, 0); | |
| 1180 | ✗ | my = get_amv(ctx, 1); | |
| 1181 | ✗ | h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 | | |
| 1182 | MB_TYPE_GMC | | ||
| 1183 | MB_TYPE_FORWARD_MV; | ||
| 1184 | } | ||
| 1185 | |||
| 1186 | 81941 | mot_val[0] = | |
| 1187 | 81941 | mot_val[2] = | |
| 1188 | 81941 | mot_val[0 + stride] = | |
| 1189 | 81941 | mot_val[2 + stride] = mx; | |
| 1190 | 81941 | mot_val[1] = | |
| 1191 | 81941 | mot_val[3] = | |
| 1192 | 81941 | mot_val[1 + stride] = | |
| 1193 | 81941 | mot_val[3 + stride] = my; | |
| 1194 | } else { | ||
| 1195 | int i; | ||
| 1196 | 32038 | h->c.cur_pic.mb_type[xy] = MB_TYPE_8x8 | | |
| 1197 | MB_TYPE_FORWARD_MV; | ||
| 1198 | 160190 | for (i = 0; i < 4; i++) { | |
| 1199 | 128152 | int16_t *mot_val = ff_h263_pred_motion(&h->c, i, 0, &pred_x, &pred_y); | |
| 1200 | 128152 | mx = ff_h263_decode_motion(h, pred_x, ctx->f_code); | |
| 1201 | 128152 | if (mx >= 0xffff) | |
| 1202 | ✗ | return AVERROR_INVALIDDATA; | |
| 1203 | |||
| 1204 | 128152 | my = ff_h263_decode_motion(h, pred_y, ctx->f_code); | |
| 1205 | 128152 | if (my >= 0xffff) | |
| 1206 | ✗ | return AVERROR_INVALIDDATA; | |
| 1207 | 128152 | mot_val[0] = mx; | |
| 1208 | 128152 | mot_val[1] = my; | |
| 1209 | } | ||
| 1210 | } | ||
| 1211 | } | ||
| 1212 | } | ||
| 1213 | } | ||
| 1214 | 6616 | h->c.mb_x = 0; | |
| 1215 | } | ||
| 1216 | |||
| 1217 | 472 | return mb_num; | |
| 1218 | } | ||
| 1219 | |||
| 1220 | /** | ||
| 1221 | * decode second partition. | ||
| 1222 | * @return <0 if an error occurred | ||
| 1223 | */ | ||
| 1224 | 1729 | static int mpeg4_decode_partition_b(H263DecContext *const h, int mb_count) | |
| 1225 | { | ||
| 1226 | 1729 | int mb_num = 0; | |
| 1227 | static const int8_t quant_tab[4] = { -1, -2, 1, 2 }; | ||
| 1228 | |||
| 1229 | 1729 | h->c.mb_x = h->c.resync_mb_x; | |
| 1230 | 1729 | h->c.first_slice_line = 1; | |
| 1231 | 7754 | for (h->c.mb_y = h->c.resync_mb_y; mb_num < mb_count; h->c.mb_y++) { | |
| 1232 | 7754 | ff_init_block_index(&h->c); | |
| 1233 | 146526 | for (; mb_num < mb_count && h->c.mb_x < h->c.mb_width; h->c.mb_x++) { | |
| 1234 | 138772 | const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride; | |
| 1235 | |||
| 1236 | 138772 | mb_num++; | |
| 1237 | 138772 | ff_update_block_index(&h->c, 8, h->c.avctx->lowres, 1); | |
| 1238 | 138772 | if (h->c.mb_x == h->c.resync_mb_x && h->c.mb_y == h->c.resync_mb_y + 1) | |
| 1239 | 1617 | h->c.first_slice_line = 0; | |
| 1240 | |||
| 1241 | 138772 | if (h->c.pict_type == AV_PICTURE_TYPE_I) { | |
| 1242 | 17421 | int ac_pred = get_bits1(&h->gb); | |
| 1243 | 17421 | int cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); | |
| 1244 | 17421 | if (cbpy < 0) { | |
| 1245 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1246 | "cbpy corrupted at %d %d\n", h->c.mb_x, h->c.mb_y); | ||
| 1247 | ✗ | return AVERROR_INVALIDDATA; | |
| 1248 | } | ||
| 1249 | |||
| 1250 | 17421 | h->c.cbp_table[xy] |= cbpy << 2; | |
| 1251 | 17421 | h->c.cur_pic.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED; | |
| 1252 | } else { /* P || S_TYPE */ | ||
| 1253 | 121351 | if (IS_INTRA(h->c.cur_pic.mb_type[xy])) { | |
| 1254 | int i; | ||
| 1255 | 6371 | int dir = 0; | |
| 1256 | 6371 | int ac_pred = get_bits1(&h->gb); | |
| 1257 | 6371 | int cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); | |
| 1258 | |||
| 1259 | 6371 | if (cbpy < 0) { | |
| 1260 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1261 | "I cbpy corrupted at %d %d\n", h->c.mb_x, h->c.mb_y); | ||
| 1262 | ✗ | return AVERROR_INVALIDDATA; | |
| 1263 | } | ||
| 1264 | |||
| 1265 | 6371 | if (h->c.cbp_table[xy] & 8) | |
| 1266 | ✗ | ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]); | |
| 1267 | 6371 | h->c.cur_pic.qscale_table[xy] = h->c.qscale; | |
| 1268 | |||
| 1269 | 44597 | for (i = 0; i < 6; i++) { | |
| 1270 | int dc_pred_dir; | ||
| 1271 | 38226 | int dc = mpeg4_decode_dc(h, i, &dc_pred_dir); | |
| 1272 | 38226 | if (dc < 0) { | |
| 1273 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1274 | "DC corrupted at %d %d\n", h->c.mb_x, h->c.mb_y); | ||
| 1275 | ✗ | return dc; | |
| 1276 | } | ||
| 1277 | 38226 | dir <<= 1; | |
| 1278 | 38226 | if (dc_pred_dir) | |
| 1279 | 12086 | dir |= 1; | |
| 1280 | } | ||
| 1281 | 6371 | h->c.cbp_table[xy] &= 3; // remove dquant | |
| 1282 | 6371 | h->c.cbp_table[xy] |= cbpy << 2; | |
| 1283 | 6371 | h->c.cur_pic.mb_type[xy] |= ac_pred * MB_TYPE_ACPRED; | |
| 1284 | 6371 | h->c.pred_dir_table[xy] = dir; | |
| 1285 | 114980 | } else if (IS_SKIP(h->c.cur_pic.mb_type[xy])) { | |
| 1286 | 1001 | h->c.cur_pic.qscale_table[xy] = h->c.qscale; | |
| 1287 | 1001 | h->c.cbp_table[xy] = 0; | |
| 1288 | } else { | ||
| 1289 | 113979 | int cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); | |
| 1290 | |||
| 1291 | 113979 | if (cbpy < 0) { | |
| 1292 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1293 | "P cbpy corrupted at %d %d\n", h->c.mb_x, h->c.mb_y); | ||
| 1294 | ✗ | return AVERROR_INVALIDDATA; | |
| 1295 | } | ||
| 1296 | |||
| 1297 | 113979 | if (h->c.cbp_table[xy] & 8) | |
| 1298 | ✗ | ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]); | |
| 1299 | 113979 | h->c.cur_pic.qscale_table[xy] = h->c.qscale; | |
| 1300 | |||
| 1301 | 113979 | h->c.cbp_table[xy] &= 3; // remove dquant | |
| 1302 | 113979 | h->c.cbp_table[xy] |= (cbpy ^ 0xf) << 2; | |
| 1303 | } | ||
| 1304 | } | ||
| 1305 | } | ||
| 1306 | 7754 | if (mb_num >= mb_count) | |
| 1307 | 1729 | return 0; | |
| 1308 | 6025 | h->c.mb_x = 0; | |
| 1309 | } | ||
| 1310 | ✗ | return 0; | |
| 1311 | } | ||
| 1312 | |||
| 1313 | /** | ||
| 1314 | * Decode the first and second partition. | ||
| 1315 | * @return <0 if error (and sets error type in the error_status_table) | ||
| 1316 | */ | ||
| 1317 | 1729 | int ff_mpeg4_decode_partitions(H263DecContext *const h) | |
| 1318 | { | ||
| 1319 | 1729 | Mpeg4DecContext *const ctx = h263_to_mpeg4(h); | |
| 1320 | int mb_num; | ||
| 1321 | int ret; | ||
| 1322 | 1729 | const int part_a_error = h->c.pict_type == AV_PICTURE_TYPE_I ? (ER_DC_ERROR | ER_MV_ERROR) : ER_MV_ERROR; | |
| 1323 | 1729 | const int part_a_end = h->c.pict_type == AV_PICTURE_TYPE_I ? (ER_DC_END | ER_MV_END) : ER_MV_END; | |
| 1324 | |||
| 1325 | 1729 | mb_num = mpeg4_decode_partition_a(ctx); | |
| 1326 | 1729 | if (mb_num <= 0) { | |
| 1327 | ✗ | ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y, | |
| 1328 | h->c.mb_x, h->c.mb_y, part_a_error); | ||
| 1329 | ✗ | return mb_num ? mb_num : AVERROR_INVALIDDATA; | |
| 1330 | } | ||
| 1331 | |||
| 1332 | 1729 | if (h->c.resync_mb_x + h->c.resync_mb_y * h->c.mb_width + mb_num > h->c.mb_num) { | |
| 1333 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "slice below monitor ...\n"); | |
| 1334 | ✗ | ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y, | |
| 1335 | h->c.mb_x, h->c.mb_y, part_a_error); | ||
| 1336 | ✗ | return AVERROR_INVALIDDATA; | |
| 1337 | } | ||
| 1338 | |||
| 1339 | 1729 | h->mb_num_left = mb_num; | |
| 1340 | |||
| 1341 | 1729 | if (h->c.pict_type == AV_PICTURE_TYPE_I) { | |
| 1342 | 499 | while (show_bits(&h->gb, 9) == 1) | |
| 1343 | ✗ | skip_bits(&h->gb, 9); | |
| 1344 | 499 | if (get_bits(&h->gb, 19) != DC_MARKER) { | |
| 1345 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1346 | "marker missing after first I partition at %d %d\n", | ||
| 1347 | h->c.mb_x, h->c.mb_y); | ||
| 1348 | ✗ | return AVERROR_INVALIDDATA; | |
| 1349 | } | ||
| 1350 | } else { | ||
| 1351 | 1230 | while (show_bits(&h->gb, 10) == 1) | |
| 1352 | ✗ | skip_bits(&h->gb, 10); | |
| 1353 | 1230 | if (get_bits(&h->gb, 17) != MOTION_MARKER) { | |
| 1354 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1355 | "marker missing after first P partition at %d %d\n", | ||
| 1356 | h->c.mb_x, h->c.mb_y); | ||
| 1357 | ✗ | return AVERROR_INVALIDDATA; | |
| 1358 | } | ||
| 1359 | } | ||
| 1360 | 1729 | ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y, | |
| 1361 | 1729 | h->c.mb_x - 1, h->c.mb_y, part_a_end); | |
| 1362 | |||
| 1363 | 1729 | ret = mpeg4_decode_partition_b(h, mb_num); | |
| 1364 | 1729 | if (ret < 0) { | |
| 1365 | ✗ | if (h->c.pict_type == AV_PICTURE_TYPE_P) | |
| 1366 | ✗ | ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y, | |
| 1367 | h->c.mb_x, h->c.mb_y, ER_DC_ERROR); | ||
| 1368 | ✗ | return ret; | |
| 1369 | } else { | ||
| 1370 | 1729 | if (h->c.pict_type == AV_PICTURE_TYPE_P) | |
| 1371 | 1230 | ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y, | |
| 1372 | 1230 | h->c.mb_x - 1, h->c.mb_y, ER_DC_END); | |
| 1373 | } | ||
| 1374 | |||
| 1375 | 1729 | return 0; | |
| 1376 | } | ||
| 1377 | |||
| 1378 | /** | ||
| 1379 | * Decode a block. | ||
| 1380 | * @return <0 if an error occurred | ||
| 1381 | */ | ||
| 1382 | 8400924 | static inline int mpeg4_decode_block(Mpeg4DecContext *ctx, int16_t *block, | |
| 1383 | int n, int coded, int intra, | ||
| 1384 | int use_intra_dc_vlc, int rvlc) | ||
| 1385 | { | ||
| 1386 | 8400924 | H263DecContext *const h = &ctx->h; | |
| 1387 | int level, i, last, run, qmul, qadd, pred; | ||
| 1388 | 8400924 | int av_uninit(dc_pred_dir); | |
| 1389 | const RLTable *rl; | ||
| 1390 | const RL_VLC_ELEM *rl_vlc; | ||
| 1391 | const uint8_t *scan_table; | ||
| 1392 | |||
| 1393 | // Note intra & rvlc should be optimized away if this is inlined | ||
| 1394 | |||
| 1395 | 8400924 | if (intra) { | |
| 1396 | // FIXME add short header support | ||
| 1397 | 1162200 | if (use_intra_dc_vlc) { | |
| 1398 | /* DC coef */ | ||
| 1399 | 1162200 | if (h->partitioned_frame) { | |
| 1400 | 142752 | level = h->c.dc_val[h->c.block_index[n]]; | |
| 1401 | 142752 | if (n < 4) | |
| 1402 | 95168 | level = FASTDIV((level + (h->c.y_dc_scale >> 1)), h->c.y_dc_scale); | |
| 1403 | else | ||
| 1404 | 47584 | level = FASTDIV((level + (h->c.c_dc_scale >> 1)), h->c.c_dc_scale); | |
| 1405 | 142752 | dc_pred_dir = (h->c.pred_dir_table[h->c.mb_x + h->c.mb_y * h->c.mb_stride] << n) & 32; | |
| 1406 | } else { | ||
| 1407 | 1019448 | level = mpeg4_decode_dc(h, n, &dc_pred_dir); | |
| 1408 | 1019448 | if (level < 0) | |
| 1409 | ✗ | return level; | |
| 1410 | } | ||
| 1411 | 1162200 | block[0] = level; | |
| 1412 | 1162200 | i = 0; | |
| 1413 | } else { | ||
| 1414 | ✗ | i = -1; | |
| 1415 | ✗ | pred = mpeg4_pred_dc(&h->c, n, &dc_pred_dir); | |
| 1416 | } | ||
| 1417 | 1162200 | if (!coded) | |
| 1418 | 258786 | goto not_coded; | |
| 1419 | |||
| 1420 | 903414 | if (rvlc) { | |
| 1421 | ✗ | rl = &ff_rvlc_rl_intra; | |
| 1422 | ✗ | rl_vlc = ff_rvlc_rl_intra.rl_vlc[0]; | |
| 1423 | } else { | ||
| 1424 | 903414 | rl = &ff_mpeg4_rl_intra; | |
| 1425 | 903414 | rl_vlc = ff_mpeg4_rl_intra.rl_vlc[0]; | |
| 1426 | } | ||
| 1427 | 903414 | if (h->c.ac_pred) { | |
| 1428 | 22006 | if (dc_pred_dir == 0) | |
| 1429 | 15508 | scan_table = h->permutated_intra_v_scantable; /* left */ | |
| 1430 | else | ||
| 1431 | 6498 | scan_table = h->permutated_intra_h_scantable; /* top */ | |
| 1432 | } else { | ||
| 1433 | 881408 | scan_table = h->c.intra_scantable.permutated; | |
| 1434 | } | ||
| 1435 | 903414 | qmul = 1; | |
| 1436 | 903414 | qadd = 0; | |
| 1437 | } else { | ||
| 1438 | 7238724 | i = -1; | |
| 1439 | 7238724 | if (!coded) { | |
| 1440 | 4847616 | h->c.block_last_index[n] = i; | |
| 1441 | 4847616 | return 0; | |
| 1442 | } | ||
| 1443 | 2391108 | if (rvlc) | |
| 1444 | ✗ | rl = &ff_rvlc_rl_inter; | |
| 1445 | else | ||
| 1446 | 2391108 | rl = &ff_h263_rl_inter; | |
| 1447 | |||
| 1448 | 2391108 | scan_table = h->c.intra_scantable.permutated; | |
| 1449 | |||
| 1450 | 2391108 | if (ctx->mpeg_quant) { | |
| 1451 | 113069 | qmul = 1; | |
| 1452 | 113069 | qadd = 0; | |
| 1453 | 113069 | if (rvlc) | |
| 1454 | ✗ | rl_vlc = ff_rvlc_rl_inter.rl_vlc[0]; | |
| 1455 | else | ||
| 1456 | 113069 | rl_vlc = ff_h263_rl_inter.rl_vlc[0]; | |
| 1457 | } else { | ||
| 1458 | 2278039 | qmul = h->c.qscale << 1; | |
| 1459 | 2278039 | qadd = (h->c.qscale - 1) | 1; | |
| 1460 | 2278039 | if (rvlc) | |
| 1461 | ✗ | rl_vlc = ff_rvlc_rl_inter.rl_vlc[h->c.qscale]; | |
| 1462 | else | ||
| 1463 | 2278039 | rl_vlc = ff_h263_rl_inter.rl_vlc[h->c.qscale]; | |
| 1464 | } | ||
| 1465 | } | ||
| 1466 | { | ||
| 1467 | 3294522 | OPEN_READER(re, &h->gb); | |
| 1468 | for (;;) { | ||
| 1469 | 28930476 | UPDATE_CACHE(re, &h->gb); | |
| 1470 | 28930476 | GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 0); | |
| 1471 | 28930476 | if (level == 0) { | |
| 1472 | /* escape */ | ||
| 1473 | 801569 | if (rvlc) { | |
| 1474 | ✗ | if (SHOW_UBITS(re, &h->gb, 1) == 0) { | |
| 1475 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1476 | "1. marker bit missing in rvlc esc\n"); | ||
| 1477 | ✗ | return AVERROR_INVALIDDATA; | |
| 1478 | } | ||
| 1479 | ✗ | SKIP_CACHE(re, &h->gb, 1); | |
| 1480 | |||
| 1481 | ✗ | last = SHOW_UBITS(re, &h->gb, 1); | |
| 1482 | ✗ | SKIP_CACHE(re, &h->gb, 1); | |
| 1483 | ✗ | run = SHOW_UBITS(re, &h->gb, 6); | |
| 1484 | ✗ | SKIP_COUNTER(re, &h->gb, 1 + 1 + 6); | |
| 1485 | ✗ | UPDATE_CACHE(re, &h->gb); | |
| 1486 | |||
| 1487 | ✗ | if (SHOW_UBITS(re, &h->gb, 1) == 0) { | |
| 1488 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1489 | "2. marker bit missing in rvlc esc\n"); | ||
| 1490 | ✗ | return AVERROR_INVALIDDATA; | |
| 1491 | } | ||
| 1492 | ✗ | SKIP_CACHE(re, &h->gb, 1); | |
| 1493 | |||
| 1494 | ✗ | level = SHOW_UBITS(re, &h->gb, 11); | |
| 1495 | ✗ | SKIP_CACHE(re, &h->gb, 11); | |
| 1496 | |||
| 1497 | ✗ | if (SHOW_UBITS(re, &h->gb, 5) != 0x10) { | |
| 1498 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "reverse esc missing\n"); | |
| 1499 | ✗ | return AVERROR_INVALIDDATA; | |
| 1500 | } | ||
| 1501 | ✗ | SKIP_CACHE(re, &h->gb, 5); | |
| 1502 | |||
| 1503 | ✗ | level = level * qmul + qadd; | |
| 1504 | ✗ | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); | |
| 1505 | ✗ | SKIP_COUNTER(re, &h->gb, 1 + 11 + 5 + 1); | |
| 1506 | |||
| 1507 | ✗ | i += run + 1; | |
| 1508 | ✗ | if (last) | |
| 1509 | ✗ | i += 192; | |
| 1510 | } else { | ||
| 1511 | int cache; | ||
| 1512 | 801569 | cache = GET_CACHE(re, &h->gb); | |
| 1513 | |||
| 1514 | if (IS_3IV1) | ||
| 1515 | cache ^= 0xC0000000; | ||
| 1516 | |||
| 1517 | 801569 | if (cache & 0x80000000) { | |
| 1518 | 359306 | if (cache & 0x40000000) { | |
| 1519 | /* third escape */ | ||
| 1520 | 141165 | SKIP_CACHE(re, &h->gb, 2); | |
| 1521 | 141165 | last = SHOW_UBITS(re, &h->gb, 1); | |
| 1522 | 141165 | SKIP_CACHE(re, &h->gb, 1); | |
| 1523 | 141165 | run = SHOW_UBITS(re, &h->gb, 6); | |
| 1524 | 141165 | SKIP_COUNTER(re, &h->gb, 2 + 1 + 6); | |
| 1525 | 141165 | UPDATE_CACHE(re, &h->gb); | |
| 1526 | |||
| 1527 | if (IS_3IV1) { | ||
| 1528 | level = SHOW_SBITS(re, &h->gb, 12); | ||
| 1529 | LAST_SKIP_BITS(re, &h->gb, 12); | ||
| 1530 | } else { | ||
| 1531 | 141165 | if (SHOW_UBITS(re, &h->gb, 1) == 0) { | |
| 1532 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1533 | "1. marker bit missing in 3. esc\n"); | ||
| 1534 | ✗ | if (!(h->c.avctx->err_recognition & AV_EF_IGNORE_ERR) || get_bits_left(&h->gb) <= 0) | |
| 1535 | ✗ | return AVERROR_INVALIDDATA; | |
| 1536 | } | ||
| 1537 | 141165 | SKIP_CACHE(re, &h->gb, 1); | |
| 1538 | |||
| 1539 | 141165 | level = SHOW_SBITS(re, &h->gb, 12); | |
| 1540 | 141165 | SKIP_CACHE(re, &h->gb, 12); | |
| 1541 | |||
| 1542 | 141165 | if (SHOW_UBITS(re, &h->gb, 1) == 0) { | |
| 1543 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1544 | "2. marker bit missing in 3. esc\n"); | ||
| 1545 | ✗ | if (!(h->c.avctx->err_recognition & AV_EF_IGNORE_ERR) || get_bits_left(&h->gb) <= 0) | |
| 1546 | ✗ | return AVERROR_INVALIDDATA; | |
| 1547 | } | ||
| 1548 | |||
| 1549 | 141165 | SKIP_COUNTER(re, &h->gb, 1 + 12 + 1); | |
| 1550 | } | ||
| 1551 | |||
| 1552 | #if 0 | ||
| 1553 | if (h->c.error_recognition >= FF_ER_COMPLIANT) { | ||
| 1554 | const int abs_level= FFABS(level); | ||
| 1555 | if (abs_level<=MAX_LEVEL && run<=MAX_RUN) { | ||
| 1556 | const int run1= run - rl->max_run[last][abs_level] - 1; | ||
| 1557 | if (abs_level <= rl->max_level[last][run]) { | ||
| 1558 | av_log(h->c.avctx, AV_LOG_ERROR, "illegal 3. esc, vlc encoding possible\n"); | ||
| 1559 | return AVERROR_INVALIDDATA; | ||
| 1560 | } | ||
| 1561 | if (h->c.error_recognition > FF_ER_COMPLIANT) { | ||
| 1562 | if (abs_level <= rl->max_level[last][run]*2) { | ||
| 1563 | av_log(h->c.avctx, AV_LOG_ERROR, "illegal 3. esc, esc 1 encoding possible\n"); | ||
| 1564 | return AVERROR_INVALIDDATA; | ||
| 1565 | } | ||
| 1566 | if (run1 >= 0 && abs_level <= rl->max_level[last][run1]) { | ||
| 1567 | av_log(h->c.avctx, AV_LOG_ERROR, "illegal 3. esc, esc 2 encoding possible\n"); | ||
| 1568 | return AVERROR_INVALIDDATA; | ||
| 1569 | } | ||
| 1570 | } | ||
| 1571 | } | ||
| 1572 | } | ||
| 1573 | #endif | ||
| 1574 | 141165 | if (level > 0) | |
| 1575 | 71207 | level = level * qmul + qadd; | |
| 1576 | else | ||
| 1577 | 69958 | level = level * qmul - qadd; | |
| 1578 | |||
| 1579 | 141165 | if ((unsigned)(level + 2048) > 4095) { | |
| 1580 | ✗ | if (h->c.avctx->err_recognition & (AV_EF_BITSTREAM|AV_EF_AGGRESSIVE)) { | |
| 1581 | ✗ | if (level > 2560 || level < -2560) { | |
| 1582 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1583 | "|level| overflow in 3. esc, qp=%d\n", | ||
| 1584 | h->c.qscale); | ||
| 1585 | ✗ | return AVERROR_INVALIDDATA; | |
| 1586 | } | ||
| 1587 | } | ||
| 1588 | ✗ | level = level < 0 ? -2048 : 2047; | |
| 1589 | } | ||
| 1590 | |||
| 1591 | 141165 | i += run + 1; | |
| 1592 | 141165 | if (last) | |
| 1593 | 45255 | i += 192; | |
| 1594 | } else { | ||
| 1595 | /* second escape */ | ||
| 1596 | 218141 | SKIP_BITS(re, &h->gb, 2); | |
| 1597 | 218141 | GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1); | |
| 1598 | 218141 | i += run + rl->max_run[run >> 7][level / qmul] + 1; // FIXME opt indexing | |
| 1599 | 218141 | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); | |
| 1600 | 218141 | LAST_SKIP_BITS(re, &h->gb, 1); | |
| 1601 | } | ||
| 1602 | } else { | ||
| 1603 | /* first escape */ | ||
| 1604 | 442263 | SKIP_BITS(re, &h->gb, 1); | |
| 1605 | 442263 | GET_RL_VLC(level, run, re, &h->gb, rl_vlc, TEX_VLC_BITS, 2, 1); | |
| 1606 | 442263 | i += run; | |
| 1607 | 442263 | level = level + rl->max_level[run >> 7][(run - 1) & 63] * qmul; // FIXME opt indexing | |
| 1608 | 442263 | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); | |
| 1609 | 442263 | LAST_SKIP_BITS(re, &h->gb, 1); | |
| 1610 | } | ||
| 1611 | } | ||
| 1612 | } else { | ||
| 1613 | 28128907 | i += run; | |
| 1614 | 28128907 | level = (level ^ SHOW_SBITS(re, &h->gb, 1)) - SHOW_SBITS(re, &h->gb, 1); | |
| 1615 | 28128907 | LAST_SKIP_BITS(re, &h->gb, 1); | |
| 1616 | } | ||
| 1617 | ff_tlog(h->c.avctx, "dct[%d][%d] = %- 4d end?:%d\n", scan_table[i&63]&7, scan_table[i&63] >> 3, level, i>62); | ||
| 1618 | 28930476 | if (i > 62) { | |
| 1619 | 3294522 | i -= 192; | |
| 1620 | 3294522 | if (i & (~63)) { | |
| 1621 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1622 | "ac-tex damaged at %d %d\n", h->c.mb_x, h->c.mb_y); | ||
| 1623 | ✗ | return AVERROR_INVALIDDATA; | |
| 1624 | } | ||
| 1625 | |||
| 1626 | 3294522 | block[scan_table[i]] = level; | |
| 1627 | 3294522 | break; | |
| 1628 | } | ||
| 1629 | |||
| 1630 | 25635954 | block[scan_table[i]] = level; | |
| 1631 | } | ||
| 1632 | 3294522 | CLOSE_READER(re, &h->gb); | |
| 1633 | } | ||
| 1634 | |||
| 1635 | 3553308 | not_coded: | |
| 1636 | 3553308 | if (intra) { | |
| 1637 | 1162200 | if (!use_intra_dc_vlc) { | |
| 1638 | ✗ | block[0] = mpeg4_get_level_dc(&h->c, n, pred, block[0]); | |
| 1639 | |||
| 1640 | ✗ | i -= i >> 31; // if (i == -1) i = 0; | |
| 1641 | } | ||
| 1642 | |||
| 1643 | 1162200 | ff_mpeg4_pred_ac(h, block, n, dc_pred_dir); | |
| 1644 | 1162200 | if (h->c.ac_pred) | |
| 1645 | 25938 | i = 63; // FIXME not optimal | |
| 1646 | } | ||
| 1647 | 3553308 | h->c.block_last_index[n] = i; | |
| 1648 | 3553308 | return 0; | |
| 1649 | } | ||
| 1650 | |||
| 1651 | /** | ||
| 1652 | * decode partition C of one MB. | ||
| 1653 | * @return <0 if an error occurred | ||
| 1654 | */ | ||
| 1655 | 138772 | static int mpeg4_decode_partitioned_mb(H263DecContext *const h) | |
| 1656 | { | ||
| 1657 | 138772 | Mpeg4DecContext *const ctx = h263_to_mpeg4(h); | |
| 1658 | 138772 | const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride; | |
| 1659 | |||
| 1660 | 138772 | const int mb_type = h->c.cur_pic.mb_type[xy]; | |
| 1661 | 138772 | int cbp = h->c.cbp_table[xy]; | |
| 1662 | |||
| 1663 | 138772 | const int use_intra_dc_vlc = h->c.qscale < ctx->intra_dc_threshold; | |
| 1664 | |||
| 1665 | 138772 | if (h->c.cur_pic.qscale_table[xy] != h->c.qscale) | |
| 1666 | ✗ | ff_set_qscale(&h->c, h->c.cur_pic.qscale_table[xy]); | |
| 1667 | |||
| 1668 | 138772 | if (h->c.pict_type == AV_PICTURE_TYPE_P || | |
| 1669 | 138772 | h->c.pict_type == AV_PICTURE_TYPE_S) { | |
| 1670 | int i; | ||
| 1671 | 606755 | for (i = 0; i < 4; i++) { | |
| 1672 | 485404 | h->c.mv[0][i][0] = h->c.cur_pic.motion_val[0][h->c.block_index[i]][0]; | |
| 1673 | 485404 | h->c.mv[0][i][1] = h->c.cur_pic.motion_val[0][h->c.block_index[i]][1]; | |
| 1674 | } | ||
| 1675 | 121351 | h->c.mb_intra = IS_INTRA(mb_type); | |
| 1676 | |||
| 1677 | 121351 | if (IS_SKIP(mb_type)) { | |
| 1678 | /* skip mb */ | ||
| 1679 | 7007 | for (i = 0; i < 6; i++) | |
| 1680 | 6006 | h->c.block_last_index[i] = -1; | |
| 1681 | 1001 | h->c.mv_dir = MV_DIR_FORWARD; | |
| 1682 | 1001 | h->c.mv_type = MV_TYPE_16X16; | |
| 1683 | 1001 | if (h->c.pict_type == AV_PICTURE_TYPE_S | |
| 1684 | ✗ | && ctx->vol_sprite_usage == GMC_SPRITE) { | |
| 1685 | ✗ | h->c.mcsel = 1; | |
| 1686 | ✗ | h->c.mb_skipped = 0; | |
| 1687 | ✗ | h->c.cur_pic.mbskip_table[xy] = 0; | |
| 1688 | } else { | ||
| 1689 | 1001 | h->c.mcsel = 0; | |
| 1690 | 1001 | h->c.mb_skipped = 1; | |
| 1691 | 1001 | h->c.cur_pic.mbskip_table[xy] = 1; | |
| 1692 | } | ||
| 1693 | 120350 | } else if (h->c.mb_intra) { | |
| 1694 | 6371 | h->c.ac_pred = IS_ACPRED(h->c.cur_pic.mb_type[xy]); | |
| 1695 | 113979 | } else if (!h->c.mb_intra) { | |
| 1696 | // h->c.mcsel = 0; // FIXME do we need to init that? | ||
| 1697 | |||
| 1698 | 113979 | h->c.mv_dir = MV_DIR_FORWARD; | |
| 1699 | 113979 | if (IS_8X8(mb_type)) { | |
| 1700 | 32038 | h->c.mv_type = MV_TYPE_8X8; | |
| 1701 | } else { | ||
| 1702 | 81941 | h->c.mv_type = MV_TYPE_16X16; | |
| 1703 | } | ||
| 1704 | } | ||
| 1705 | } else { /* I-Frame */ | ||
| 1706 | 17421 | h->c.mb_intra = 1; | |
| 1707 | 17421 | h->c.ac_pred = IS_ACPRED(h->c.cur_pic.mb_type[xy]); | |
| 1708 | } | ||
| 1709 | |||
| 1710 | 138772 | if (!IS_SKIP(mb_type)) { | |
| 1711 | int i; | ||
| 1712 | 137771 | h->c.bdsp.clear_blocks(h->block[0]); | |
| 1713 | /* decode each block */ | ||
| 1714 | 964397 | for (i = 0; i < 6; i++) { | |
| 1715 | 826626 | if (mpeg4_decode_block(ctx, h->block[i], i, cbp & 32, h->c.mb_intra, | |
| 1716 | use_intra_dc_vlc, ctx->rvlc) < 0) { | ||
| 1717 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1718 | "texture corrupted at %d %d %d\n", | ||
| 1719 | h->c.mb_x, h->c.mb_y, h->c.mb_intra); | ||
| 1720 | ✗ | return AVERROR_INVALIDDATA; | |
| 1721 | } | ||
| 1722 | 826626 | cbp += cbp; | |
| 1723 | } | ||
| 1724 | } | ||
| 1725 | |||
| 1726 | /* per-MB end of slice check */ | ||
| 1727 | 138772 | if (--h->mb_num_left <= 0) { | |
| 1728 | 1729 | if (mpeg4_is_resync(ctx)) | |
| 1729 | 1729 | return SLICE_END; | |
| 1730 | else | ||
| 1731 | ✗ | return SLICE_NOEND; | |
| 1732 | } else { | ||
| 1733 | 137043 | if (mpeg4_is_resync(ctx)) { | |
| 1734 | 278 | const int delta = h->c.mb_x + 1 == h->c.mb_width ? 2 : 1; | |
| 1735 | 278 | if (h->c.cbp_table[xy + delta]) | |
| 1736 | ✗ | return SLICE_END; | |
| 1737 | } | ||
| 1738 | 137043 | return SLICE_OK; | |
| 1739 | } | ||
| 1740 | } | ||
| 1741 | |||
| 1742 | 1460344 | static int mpeg4_decode_mb(H263DecContext *const h) | |
| 1743 | { | ||
| 1744 | 1460344 | Mpeg4DecContext *const ctx = h263_to_mpeg4(h); | |
| 1745 | int cbpc, cbpy, i, cbp, pred_x, pred_y, mx, my, dquant; | ||
| 1746 | static const int8_t quant_tab[4] = { -1, -2, 1, 2 }; | ||
| 1747 | 1460344 | const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride; | |
| 1748 | int next; | ||
| 1749 | |||
| 1750 | av_assert2(h->c.h263_pred); | ||
| 1751 | |||
| 1752 | 1460344 | if (h->c.pict_type == AV_PICTURE_TYPE_P || | |
| 1753 | 371582 | h->c.pict_type == AV_PICTURE_TYPE_S) { | |
| 1754 | do { | ||
| 1755 | 1088762 | if (get_bits1(&h->gb)) { | |
| 1756 | /* skip mb */ | ||
| 1757 | 165103 | h->c.mb_intra = 0; | |
| 1758 | 1155721 | for (i = 0; i < 6; i++) | |
| 1759 | 990618 | h->c.block_last_index[i] = -1; | |
| 1760 | 165103 | h->c.mv_dir = MV_DIR_FORWARD; | |
| 1761 | 165103 | h->c.mv_type = MV_TYPE_16X16; | |
| 1762 | 165103 | if (h->c.pict_type == AV_PICTURE_TYPE_S && | |
| 1763 | ✗ | ctx->vol_sprite_usage == GMC_SPRITE) { | |
| 1764 | ✗ | h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | | |
| 1765 | MB_TYPE_GMC | | ||
| 1766 | MB_TYPE_16x16 | | ||
| 1767 | MB_TYPE_FORWARD_MV; | ||
| 1768 | ✗ | h->c.mcsel = 1; | |
| 1769 | ✗ | h->c.mv[0][0][0] = get_amv(ctx, 0); | |
| 1770 | ✗ | h->c.mv[0][0][1] = get_amv(ctx, 1); | |
| 1771 | ✗ | h->c.cur_pic.mbskip_table[xy] = 0; | |
| 1772 | ✗ | h->c.mb_skipped = 0; | |
| 1773 | } else { | ||
| 1774 | 165103 | h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | | |
| 1775 | MB_TYPE_FORWARD_MV; | ||
| 1776 | 165103 | h->c.mcsel = 0; | |
| 1777 | 165103 | h->c.mv[0][0][0] = 0; | |
| 1778 | 165103 | h->c.mv[0][0][1] = 0; | |
| 1779 | 165103 | h->c.cur_pic.mbskip_table[xy] = 1; | |
| 1780 | 165103 | h->c.mb_skipped = 1; | |
| 1781 | } | ||
| 1782 | 165103 | goto end; | |
| 1783 | } | ||
| 1784 | 923659 | cbpc = get_vlc2(&h->gb, ff_h263_inter_MCBPC_vlc, INTER_MCBPC_VLC_BITS, 2); | |
| 1785 | 923659 | if (cbpc < 0) { | |
| 1786 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1787 | "mcbpc damaged at %d %d\n", h->c.mb_x, h->c.mb_y); | ||
| 1788 | ✗ | return AVERROR_INVALIDDATA; | |
| 1789 | } | ||
| 1790 | 923659 | } while (cbpc == 20); | |
| 1791 | |||
| 1792 | 923659 | dquant = cbpc & 8; | |
| 1793 | 923659 | h->c.mb_intra = ((cbpc & 4) != 0); | |
| 1794 | 923659 | if (h->c.mb_intra) | |
| 1795 | 32981 | goto intra; | |
| 1796 | 890678 | h->c.bdsp.clear_blocks(h->block[0]); | |
| 1797 | |||
| 1798 | 890678 | if (h->c.pict_type == AV_PICTURE_TYPE_S && | |
| 1799 | ✗ | ctx->vol_sprite_usage == GMC_SPRITE && (cbpc & 16) == 0) | |
| 1800 | ✗ | h->c.mcsel = get_bits1(&h->gb); | |
| 1801 | else | ||
| 1802 | 890678 | h->c.mcsel = 0; | |
| 1803 | 890678 | cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1) ^ 0x0F; | |
| 1804 | 890678 | if (cbpy < 0) { | |
| 1805 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 1806 | "P cbpy damaged at %d %d\n", h->c.mb_x, h->c.mb_y); | ||
| 1807 | ✗ | return AVERROR_INVALIDDATA; | |
| 1808 | } | ||
| 1809 | |||
| 1810 | 890678 | cbp = (cbpc & 3) | (cbpy << 2); | |
| 1811 | 890678 | if (dquant) | |
| 1812 | 23659 | ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]); | |
| 1813 | 890678 | if ((!h->c.progressive_sequence) && | |
| 1814 | ✗ | (cbp || (h->c.workaround_bugs & FF_BUG_XVID_ILACE))) | |
| 1815 | ✗ | h->c.interlaced_dct = get_bits1(&h->gb); | |
| 1816 | |||
| 1817 | 890678 | h->c.mv_dir = MV_DIR_FORWARD; | |
| 1818 | 890678 | if ((cbpc & 16) == 0) { | |
| 1819 | 792091 | if (h->c.mcsel) { | |
| 1820 | ✗ | h->c.cur_pic.mb_type[xy] = MB_TYPE_GMC | MB_TYPE_16x16 | | |
| 1821 | MB_TYPE_FORWARD_MV; | ||
| 1822 | /* 16x16 global motion prediction */ | ||
| 1823 | ✗ | h->c.mv_type = MV_TYPE_16X16; | |
| 1824 | ✗ | mx = get_amv(ctx, 0); | |
| 1825 | ✗ | my = get_amv(ctx, 1); | |
| 1826 | ✗ | h->c.mv[0][0][0] = mx; | |
| 1827 | ✗ | h->c.mv[0][0][1] = my; | |
| 1828 | 792091 | } else if ((!h->c.progressive_sequence) && get_bits1(&h->gb)) { | |
| 1829 | ✗ | h->c.cur_pic.mb_type[xy] = MB_TYPE_16x8 | MB_TYPE_FORWARD_MV | | |
| 1830 | MB_TYPE_INTERLACED; | ||
| 1831 | /* 16x8 field motion prediction */ | ||
| 1832 | ✗ | h->c.mv_type = MV_TYPE_FIELD; | |
| 1833 | |||
| 1834 | ✗ | h->c.field_select[0][0] = get_bits1(&h->gb); | |
| 1835 | ✗ | h->c.field_select[0][1] = get_bits1(&h->gb); | |
| 1836 | |||
| 1837 | ✗ | ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y); | |
| 1838 | |||
| 1839 | ✗ | for (i = 0; i < 2; i++) { | |
| 1840 | ✗ | mx = ff_h263_decode_motion(h, pred_x, ctx->f_code); | |
| 1841 | ✗ | if (mx >= 0xffff) | |
| 1842 | ✗ | return AVERROR_INVALIDDATA; | |
| 1843 | |||
| 1844 | ✗ | my = ff_h263_decode_motion(h, pred_y / 2, ctx->f_code); | |
| 1845 | ✗ | if (my >= 0xffff) | |
| 1846 | ✗ | return AVERROR_INVALIDDATA; | |
| 1847 | |||
| 1848 | ✗ | h->c.mv[0][i][0] = mx; | |
| 1849 | ✗ | h->c.mv[0][i][1] = my; | |
| 1850 | } | ||
| 1851 | } else { | ||
| 1852 | 792091 | h->c.cur_pic.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_FORWARD_MV; | |
| 1853 | /* 16x16 motion prediction */ | ||
| 1854 | 792091 | h->c.mv_type = MV_TYPE_16X16; | |
| 1855 | 792091 | ff_h263_pred_motion(&h->c, 0, 0, &pred_x, &pred_y); | |
| 1856 | 792091 | mx = ff_h263_decode_motion(h, pred_x, ctx->f_code); | |
| 1857 | |||
| 1858 | 792091 | if (mx >= 0xffff) | |
| 1859 | ✗ | return AVERROR_INVALIDDATA; | |
| 1860 | |||
| 1861 | 792091 | my = ff_h263_decode_motion(h, pred_y, ctx->f_code); | |
| 1862 | |||
| 1863 | 792091 | if (my >= 0xffff) | |
| 1864 | ✗ | return AVERROR_INVALIDDATA; | |
| 1865 | 792091 | h->c.mv[0][0][0] = mx; | |
| 1866 | 792091 | h->c.mv[0][0][1] = my; | |
| 1867 | } | ||
| 1868 | } else { | ||
| 1869 | 98587 | h->c.cur_pic.mb_type[xy] = MB_TYPE_8x8 | MB_TYPE_FORWARD_MV; | |
| 1870 | 98587 | h->c.mv_type = MV_TYPE_8X8; | |
| 1871 | 492935 | for (i = 0; i < 4; i++) { | |
| 1872 | 394348 | int16_t *mot_val = ff_h263_pred_motion(&h->c, i, 0, &pred_x, &pred_y); | |
| 1873 | 394348 | mx = ff_h263_decode_motion(h, pred_x, ctx->f_code); | |
| 1874 | 394348 | if (mx >= 0xffff) | |
| 1875 | ✗ | return AVERROR_INVALIDDATA; | |
| 1876 | |||
| 1877 | 394348 | my = ff_h263_decode_motion(h, pred_y, ctx->f_code); | |
| 1878 | 394348 | if (my >= 0xffff) | |
| 1879 | ✗ | return AVERROR_INVALIDDATA; | |
| 1880 | 394348 | h->c.mv[0][i][0] = mx; | |
| 1881 | 394348 | h->c.mv[0][i][1] = my; | |
| 1882 | 394348 | mot_val[0] = mx; | |
| 1883 | 394348 | mot_val[1] = my; | |
| 1884 | } | ||
| 1885 | } | ||
| 1886 | 371582 | } else if (h->c.pict_type == AV_PICTURE_TYPE_B) { | |
| 1887 | int modb1; // first bit of modb | ||
| 1888 | int modb2; // second bit of modb | ||
| 1889 | int mb_type; | ||
| 1890 | |||
| 1891 | 234655 | h->c.mb_intra = 0; // B-frames never contain intra blocks | |
| 1892 | 234655 | h->c.mcsel = 0; // ... true gmc blocks | |
| 1893 | |||
| 1894 | 234655 | if (h->c.mb_x == 0) { | |
| 1895 | 30117 | for (i = 0; i < 2; i++) { | |
| 1896 | 20078 | h->c.last_mv[i][0][0] = | |
| 1897 | 20078 | h->c.last_mv[i][0][1] = | |
| 1898 | 20078 | h->c.last_mv[i][1][0] = | |
| 1899 | 20078 | h->c.last_mv[i][1][1] = 0; | |
| 1900 | } | ||
| 1901 | |||
| 1902 | 10039 | ff_thread_progress_await(&h->c.next_pic.ptr->progress, h->c.mb_y); | |
| 1903 | } | ||
| 1904 | |||
| 1905 | /* if we skipped it in the future P-frame than skip it now too */ | ||
| 1906 | 234655 | h->c.mb_skipped = h->c.next_pic.mbskip_table[h->c.mb_y * h->c.mb_stride + h->c.mb_x]; // Note, skiptab=0 if last was GMC | |
| 1907 | |||
| 1908 | 234655 | if (h->c.mb_skipped) { | |
| 1909 | /* skip mb */ | ||
| 1910 | 230006 | for (i = 0; i < 6; i++) | |
| 1911 | 197148 | h->c.block_last_index[i] = -1; | |
| 1912 | |||
| 1913 | 32858 | h->c.mv_dir = MV_DIR_FORWARD; | |
| 1914 | 32858 | h->c.mv_type = MV_TYPE_16X16; | |
| 1915 | 32858 | h->c.mv[0][0][0] = | |
| 1916 | 32858 | h->c.mv[0][0][1] = | |
| 1917 | 32858 | h->c.mv[1][0][0] = | |
| 1918 | 32858 | h->c.mv[1][0][1] = 0; | |
| 1919 | 32858 | h->c.cur_pic.mb_type[xy] = MB_TYPE_SKIP | | |
| 1920 | MB_TYPE_16x16 | | ||
| 1921 | MB_TYPE_FORWARD_MV; | ||
| 1922 | 32858 | goto end; | |
| 1923 | } | ||
| 1924 | |||
| 1925 | 201797 | modb1 = get_bits1(&h->gb); | |
| 1926 | 201797 | if (modb1) { | |
| 1927 | // like MB_TYPE_B_DIRECT but no vectors coded | ||
| 1928 | 44514 | mb_type = MB_TYPE_DIRECT2 | MB_TYPE_SKIP | MB_TYPE_BIDIR_MV; | |
| 1929 | 44514 | cbp = 0; | |
| 1930 | } else { | ||
| 1931 | 157283 | modb2 = get_bits1(&h->gb); | |
| 1932 | 157283 | mb_type = get_vlc2(&h->gb, mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 1); | |
| 1933 | 157283 | if (mb_type < 0) { | |
| 1934 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "illegal MB_type\n"); | |
| 1935 | ✗ | return AVERROR_INVALIDDATA; | |
| 1936 | } | ||
| 1937 | 157283 | if (modb2) { | |
| 1938 | 65756 | cbp = 0; | |
| 1939 | } else { | ||
| 1940 | 91527 | h->c.bdsp.clear_blocks(h->block[0]); | |
| 1941 | 91527 | cbp = get_bits(&h->gb, 6); | |
| 1942 | } | ||
| 1943 | |||
| 1944 | 157283 | if ((!IS_DIRECT(mb_type)) && cbp) { | |
| 1945 | 65999 | if (get_bits1(&h->gb)) | |
| 1946 | 7480 | ff_set_qscale(&h->c, h->c.qscale + get_bits1(&h->gb) * 4 - 2); | |
| 1947 | } | ||
| 1948 | |||
| 1949 | 157283 | if (!h->c.progressive_sequence) { | |
| 1950 | ✗ | if (cbp) | |
| 1951 | ✗ | h->c.interlaced_dct = get_bits1(&h->gb); | |
| 1952 | |||
| 1953 | ✗ | if (!IS_DIRECT(mb_type) && get_bits1(&h->gb)) { | |
| 1954 | ✗ | mb_type |= MB_TYPE_16x8 | MB_TYPE_INTERLACED; | |
| 1955 | ✗ | mb_type &= ~MB_TYPE_16x16; | |
| 1956 | |||
| 1957 | ✗ | if (HAS_FORWARD_MV(mb_type)) { | |
| 1958 | ✗ | h->c.field_select[0][0] = get_bits1(&h->gb); | |
| 1959 | ✗ | h->c.field_select[0][1] = get_bits1(&h->gb); | |
| 1960 | } | ||
| 1961 | ✗ | if (HAS_BACKWARD_MV(mb_type)) { | |
| 1962 | ✗ | h->c.field_select[1][0] = get_bits1(&h->gb); | |
| 1963 | ✗ | h->c.field_select[1][1] = get_bits1(&h->gb); | |
| 1964 | } | ||
| 1965 | } | ||
| 1966 | } | ||
| 1967 | |||
| 1968 | 157283 | h->c.mv_dir = 0; | |
| 1969 | 157283 | if ((mb_type & (MB_TYPE_DIRECT2 | MB_TYPE_INTERLACED)) == 0) { | |
| 1970 | 116195 | h->c.mv_type = MV_TYPE_16X16; | |
| 1971 | |||
| 1972 | 116195 | if (HAS_FORWARD_MV(mb_type)) { | |
| 1973 | 81033 | h->c.mv_dir = MV_DIR_FORWARD; | |
| 1974 | |||
| 1975 | 81033 | mx = ff_h263_decode_motion(h, h->c.last_mv[0][0][0], ctx->f_code); | |
| 1976 | 81033 | my = ff_h263_decode_motion(h, h->c.last_mv[0][0][1], ctx->f_code); | |
| 1977 | 81033 | h->c.last_mv[0][1][0] = | |
| 1978 | 81033 | h->c.last_mv[0][0][0] = | |
| 1979 | 81033 | h->c.mv[0][0][0] = mx; | |
| 1980 | 81033 | h->c.last_mv[0][1][1] = | |
| 1981 | 81033 | h->c.last_mv[0][0][1] = | |
| 1982 | 81033 | h->c.mv[0][0][1] = my; | |
| 1983 | } | ||
| 1984 | |||
| 1985 | 116195 | if (HAS_BACKWARD_MV(mb_type)) { | |
| 1986 | 92446 | h->c.mv_dir |= MV_DIR_BACKWARD; | |
| 1987 | |||
| 1988 | 92446 | mx = ff_h263_decode_motion(h, h->c.last_mv[1][0][0], ctx->b_code); | |
| 1989 | 92446 | my = ff_h263_decode_motion(h, h->c.last_mv[1][0][1], ctx->b_code); | |
| 1990 | 92446 | h->c.last_mv[1][1][0] = | |
| 1991 | 92446 | h->c.last_mv[1][0][0] = | |
| 1992 | 92446 | h->c.mv[1][0][0] = mx; | |
| 1993 | 92446 | h->c.last_mv[1][1][1] = | |
| 1994 | 92446 | h->c.last_mv[1][0][1] = | |
| 1995 | 92446 | h->c.mv[1][0][1] = my; | |
| 1996 | } | ||
| 1997 | 41088 | } else if (!IS_DIRECT(mb_type)) { | |
| 1998 | ✗ | h->c.mv_type = MV_TYPE_FIELD; | |
| 1999 | |||
| 2000 | ✗ | if (HAS_FORWARD_MV(mb_type)) { | |
| 2001 | ✗ | h->c.mv_dir = MV_DIR_FORWARD; | |
| 2002 | |||
| 2003 | ✗ | for (i = 0; i < 2; i++) { | |
| 2004 | ✗ | mx = ff_h263_decode_motion(h, h->c.last_mv[0][i][0], ctx->f_code); | |
| 2005 | ✗ | my = ff_h263_decode_motion(h, h->c.last_mv[0][i][1] / 2, ctx->f_code); | |
| 2006 | ✗ | h->c.last_mv[0][i][0] = | |
| 2007 | ✗ | h->c.mv[0][i][0] = mx; | |
| 2008 | ✗ | h->c.last_mv[0][i][1] = (h->c.mv[0][i][1] = my) * 2; | |
| 2009 | } | ||
| 2010 | } | ||
| 2011 | |||
| 2012 | ✗ | if (HAS_BACKWARD_MV(mb_type)) { | |
| 2013 | ✗ | h->c.mv_dir |= MV_DIR_BACKWARD; | |
| 2014 | |||
| 2015 | ✗ | for (i = 0; i < 2; i++) { | |
| 2016 | ✗ | mx = ff_h263_decode_motion(h, h->c.last_mv[1][i][0], ctx->b_code); | |
| 2017 | ✗ | my = ff_h263_decode_motion(h, h->c.last_mv[1][i][1] / 2, ctx->b_code); | |
| 2018 | ✗ | h->c.last_mv[1][i][0] = | |
| 2019 | ✗ | h->c.mv[1][i][0] = mx; | |
| 2020 | ✗ | h->c.last_mv[1][i][1] = (h->c.mv[1][i][1] = my) * 2; | |
| 2021 | } | ||
| 2022 | } | ||
| 2023 | } | ||
| 2024 | } | ||
| 2025 | |||
| 2026 | 201797 | if (IS_DIRECT(mb_type)) { | |
| 2027 | 85602 | if (IS_SKIP(mb_type)) { | |
| 2028 | 44514 | mx = | |
| 2029 | 44514 | my = 0; | |
| 2030 | } else { | ||
| 2031 | 41088 | mx = ff_h263_decode_motion(h, 0, 1); | |
| 2032 | 41088 | my = ff_h263_decode_motion(h, 0, 1); | |
| 2033 | } | ||
| 2034 | |||
| 2035 | 85602 | h->c.mv_dir = MV_DIR_FORWARD | MV_DIR_BACKWARD | MV_DIRECT; | |
| 2036 | 85602 | mb_type |= ff_mpeg4_set_direct_mv(&h->c, mx, my); | |
| 2037 | } | ||
| 2038 | 201797 | h->c.cur_pic.mb_type[xy] = mb_type; | |
| 2039 | } else { /* I-Frame */ | ||
| 2040 | int use_intra_dc_vlc; | ||
| 2041 | |||
| 2042 | do { | ||
| 2043 | 136927 | cbpc = get_vlc2(&h->gb, ff_h263_intra_MCBPC_vlc, INTRA_MCBPC_VLC_BITS, 2); | |
| 2044 | 136927 | if (cbpc < 0) { | |
| 2045 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 2046 | "I cbpc damaged at %d %d\n", h->c.mb_x, h->c.mb_y); | ||
| 2047 | ✗ | return AVERROR_INVALIDDATA; | |
| 2048 | } | ||
| 2049 | 136927 | } while (cbpc == 8); | |
| 2050 | |||
| 2051 | 136927 | dquant = cbpc & 4; | |
| 2052 | 136927 | h->c.mb_intra = 1; | |
| 2053 | |||
| 2054 | 169908 | intra: | |
| 2055 | 169908 | h->c.ac_pred = get_bits1(&h->gb); | |
| 2056 | 169908 | if (h->c.ac_pred) | |
| 2057 | 552 | h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA | MB_TYPE_ACPRED; | |
| 2058 | else | ||
| 2059 | 169356 | h->c.cur_pic.mb_type[xy] = MB_TYPE_INTRA; | |
| 2060 | |||
| 2061 | 169908 | cbpy = get_vlc2(&h->gb, ff_h263_cbpy_vlc, CBPY_VLC_BITS, 1); | |
| 2062 | 169908 | if (cbpy < 0) { | |
| 2063 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 2064 | "I cbpy damaged at %d %d\n", h->c.mb_x, h->c.mb_y); | ||
| 2065 | ✗ | return AVERROR_INVALIDDATA; | |
| 2066 | } | ||
| 2067 | 169908 | cbp = (cbpc & 3) | (cbpy << 2); | |
| 2068 | |||
| 2069 | 169908 | use_intra_dc_vlc = h->c.qscale < ctx->intra_dc_threshold; | |
| 2070 | |||
| 2071 | 169908 | if (dquant) | |
| 2072 | 10733 | ff_set_qscale(&h->c, h->c.qscale + quant_tab[get_bits(&h->gb, 2)]); | |
| 2073 | |||
| 2074 | 169908 | if (!h->c.progressive_sequence) | |
| 2075 | ✗ | h->c.interlaced_dct = get_bits1(&h->gb); | |
| 2076 | |||
| 2077 | 169908 | h->c.bdsp.clear_blocks(h->block[0]); | |
| 2078 | /* decode each block */ | ||
| 2079 | 1189356 | for (i = 0; i < 6; i++) { | |
| 2080 | 1019448 | if (mpeg4_decode_block(ctx, h->block[i], i, cbp & 32, | |
| 2081 | 1, use_intra_dc_vlc, 0) < 0) | ||
| 2082 | ✗ | return AVERROR_INVALIDDATA; | |
| 2083 | 1019448 | cbp += cbp; | |
| 2084 | } | ||
| 2085 | 169908 | goto end; | |
| 2086 | } | ||
| 2087 | |||
| 2088 | /* decode each block */ | ||
| 2089 | 7647325 | for (i = 0; i < 6; i++) { | |
| 2090 | 6554850 | if (mpeg4_decode_block(ctx, h->block[i], i, cbp & 32, 0, 0, 0) < 0) | |
| 2091 | ✗ | return AVERROR_INVALIDDATA; | |
| 2092 | 6554850 | cbp += cbp; | |
| 2093 | } | ||
| 2094 | |||
| 2095 | 1092475 | end: | |
| 2096 | /* per-MB end of slice check */ | ||
| 2097 | 1460344 | next = mpeg4_is_resync(ctx); | |
| 2098 | 1460344 | if (next) { | |
| 2099 | 22594 | if (h->c.mb_x + h->c.mb_y*h->c.mb_width + 1 > next && (h->c.avctx->err_recognition & AV_EF_AGGRESSIVE)) { | |
| 2100 | ✗ | return AVERROR_INVALIDDATA; | |
| 2101 | 22594 | } else if (h->c.mb_x + h->c.mb_y*h->c.mb_width + 1 >= next) | |
| 2102 | 14650 | return SLICE_END; | |
| 2103 | |||
| 2104 | 7944 | if (h->c.pict_type == AV_PICTURE_TYPE_B) { | |
| 2105 | 7944 | const int delta = h->c.mb_x + 1 == h->c.mb_width ? 2 : 1; | |
| 2106 | 7944 | ff_thread_progress_await(&h->c.next_pic.ptr->progress, | |
| 2107 | 7944 | (h->c.mb_x + delta >= h->c.mb_width) | |
| 2108 | 168 | ? FFMIN(h->c.mb_y + 1, h->c.mb_height - 1) | |
| 2109 | : h->c.mb_y); | ||
| 2110 | 7944 | if (h->c.next_pic.mbskip_table[xy + delta]) | |
| 2111 | 7944 | return SLICE_OK; | |
| 2112 | } | ||
| 2113 | |||
| 2114 | ✗ | return SLICE_END; | |
| 2115 | } | ||
| 2116 | |||
| 2117 | 1437750 | return SLICE_OK; | |
| 2118 | } | ||
| 2119 | |||
| 2120 | /* As per spec, studio start code search isn't the same as the old type of start code */ | ||
| 2121 | 34 | static void next_start_code_studio(GetBitContext *gb) | |
| 2122 | { | ||
| 2123 | 34 | align_get_bits(gb); | |
| 2124 | |||
| 2125 | 34 | while (get_bits_left(gb) >= 24 && show_bits(gb, 24) != 0x1) { | |
| 2126 | ✗ | get_bits(gb, 8); | |
| 2127 | } | ||
| 2128 | 34 | } | |
| 2129 | |||
| 2130 | /* additional_code, vlc index */ | ||
| 2131 | static const uint8_t ac_state_tab[22][2] = | ||
| 2132 | { | ||
| 2133 | {0, 0}, | ||
| 2134 | {0, 1}, | ||
| 2135 | {1, 1}, | ||
| 2136 | {2, 1}, | ||
| 2137 | {3, 1}, | ||
| 2138 | {4, 1}, | ||
| 2139 | {5, 1}, | ||
| 2140 | {1, 2}, | ||
| 2141 | {2, 2}, | ||
| 2142 | {3, 2}, | ||
| 2143 | {4, 2}, | ||
| 2144 | {5, 2}, | ||
| 2145 | {6, 2}, | ||
| 2146 | {1, 3}, | ||
| 2147 | {2, 4}, | ||
| 2148 | {3, 5}, | ||
| 2149 | {4, 6}, | ||
| 2150 | {5, 7}, | ||
| 2151 | {6, 8}, | ||
| 2152 | {7, 9}, | ||
| 2153 | {8, 10}, | ||
| 2154 | {0, 11} | ||
| 2155 | }; | ||
| 2156 | |||
| 2157 | 5336 | static int mpeg4_decode_studio_block(Mpeg4DecContext *const ctx, int32_t block[64], int n) | |
| 2158 | { | ||
| 2159 | 5336 | H263DecContext *const h = &ctx->h; | |
| 2160 | |||
| 2161 | 5336 | int cc, dct_dc_size, dct_diff, code, j, idx = 1, group = 0, run = 0, | |
| 2162 | additional_code_len, sign, mismatch; | ||
| 2163 | 5336 | const VLCElem *cur_vlc = studio_intra_tab[0]; | |
| 2164 | 5336 | const uint8_t *const scantable = h->c.intra_scantable.permutated; | |
| 2165 | const uint16_t *quant_matrix; | ||
| 2166 | uint32_t flc; | ||
| 2167 | 5336 | const int min = -1 * (1 << (h->c.avctx->bits_per_raw_sample + 6)); | |
| 2168 | 5336 | const int max = ((1 << (h->c.avctx->bits_per_raw_sample + 6)) - 1); | |
| 2169 | 5336 | int shift = 3 - ctx->dct_precision; | |
| 2170 | |||
| 2171 | 5336 | mismatch = 1; | |
| 2172 | |||
| 2173 | 5336 | memset(block, 0, 64 * sizeof(int32_t)); | |
| 2174 | |||
| 2175 | 5336 | if (n < 4) { | |
| 2176 | 2668 | cc = 0; | |
| 2177 | 2668 | dct_dc_size = get_vlc2(&h->gb, studio_luma_dc, STUDIO_INTRA_BITS, 2); | |
| 2178 | 2668 | quant_matrix = h->c.intra_matrix; | |
| 2179 | } else { | ||
| 2180 | 2668 | cc = (n & 1) + 1; | |
| 2181 | 2668 | if (ctx->rgb) | |
| 2182 | ✗ | dct_dc_size = get_vlc2(&h->gb, studio_luma_dc, STUDIO_INTRA_BITS, 2); | |
| 2183 | else | ||
| 2184 | 2668 | dct_dc_size = get_vlc2(&h->gb, studio_chroma_dc, STUDIO_INTRA_BITS, 2); | |
| 2185 | 2668 | quant_matrix = h->c.chroma_intra_matrix; | |
| 2186 | } | ||
| 2187 | |||
| 2188 | 5336 | if (dct_dc_size == 0) { | |
| 2189 | 242 | dct_diff = 0; | |
| 2190 | } else { | ||
| 2191 | 5094 | dct_diff = get_xbits(&h->gb, dct_dc_size); | |
| 2192 | |||
| 2193 | 5094 | if (dct_dc_size > 8) { | |
| 2194 | 227 | if(!check_marker(h->c.avctx, &h->gb, "dct_dc_size > 8")) | |
| 2195 | ✗ | return AVERROR_INVALIDDATA; | |
| 2196 | } | ||
| 2197 | |||
| 2198 | } | ||
| 2199 | |||
| 2200 | 5336 | h->last_dc[cc] += dct_diff; | |
| 2201 | |||
| 2202 | 5336 | if (ctx->mpeg_quant) | |
| 2203 | ✗ | block[0] = h->last_dc[cc] * (8 >> h->c.intra_dc_precision); | |
| 2204 | else | ||
| 2205 | 5336 | block[0] = h->last_dc[cc] * (8 >> h->c.intra_dc_precision) * (8 >> ctx->dct_precision); | |
| 2206 | /* TODO: support mpeg_quant for AC coefficients */ | ||
| 2207 | |||
| 2208 | 5336 | block[0] = av_clip(block[0], min, max); | |
| 2209 | 5336 | mismatch ^= block[0]; | |
| 2210 | |||
| 2211 | /* AC Coefficients */ | ||
| 2212 | while (1) { | ||
| 2213 | 33424 | group = get_vlc2(&h->gb, cur_vlc, STUDIO_INTRA_BITS, 2); | |
| 2214 | |||
| 2215 | 33424 | if (group < 0) { | |
| 2216 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "illegal ac coefficient group vlc\n"); | |
| 2217 | ✗ | return AVERROR_INVALIDDATA; | |
| 2218 | } | ||
| 2219 | |||
| 2220 | 33424 | additional_code_len = ac_state_tab[group][0]; | |
| 2221 | 33424 | cur_vlc = studio_intra_tab[ac_state_tab[group][1]]; | |
| 2222 | |||
| 2223 | 33424 | if (group == 0) { | |
| 2224 | /* End of Block */ | ||
| 2225 | 5336 | break; | |
| 2226 | 28088 | } else if (group >= 1 && group <= 6) { | |
| 2227 | /* Zero run length (Table B.47) */ | ||
| 2228 | 2347 | run = 1 << additional_code_len; | |
| 2229 | 2347 | if (additional_code_len) | |
| 2230 | 927 | run += get_bits(&h->gb, additional_code_len); | |
| 2231 | 2347 | idx += run; | |
| 2232 | 2347 | continue; | |
| 2233 | 25741 | } else if (group >= 7 && group <= 12) { | |
| 2234 | /* Zero run length and +/-1 level (Table B.48) */ | ||
| 2235 | 6045 | code = get_bits(&h->gb, additional_code_len); | |
| 2236 | 6045 | sign = code & 1; | |
| 2237 | 6045 | code >>= 1; | |
| 2238 | 6045 | run = (1 << (additional_code_len - 1)) + code; | |
| 2239 | 6045 | idx += run; | |
| 2240 | 6045 | if (idx > 63) | |
| 2241 | ✗ | return AVERROR_INVALIDDATA; | |
| 2242 | 6045 | j = scantable[idx++]; | |
| 2243 | 6045 | block[j] = sign ? 1 : -1; | |
| 2244 | 19696 | } else if (group >= 13 && group <= 20) { | |
| 2245 | /* Level value (Table B.49) */ | ||
| 2246 | 19696 | if (idx > 63) | |
| 2247 | ✗ | return AVERROR_INVALIDDATA; | |
| 2248 | 19696 | j = scantable[idx++]; | |
| 2249 | 19696 | block[j] = get_xbits(&h->gb, additional_code_len); | |
| 2250 | ✗ | } else if (group == 21) { | |
| 2251 | /* Escape */ | ||
| 2252 | ✗ | if (idx > 63) | |
| 2253 | ✗ | return AVERROR_INVALIDDATA; | |
| 2254 | ✗ | j = scantable[idx++]; | |
| 2255 | ✗ | additional_code_len = h->c.avctx->bits_per_raw_sample + ctx->dct_precision + 4; | |
| 2256 | ✗ | flc = get_bits(&h->gb, additional_code_len); | |
| 2257 | ✗ | if (flc >> (additional_code_len-1)) | |
| 2258 | ✗ | block[j] = -1 * (( flc ^ ((1 << additional_code_len) -1)) + 1); | |
| 2259 | else | ||
| 2260 | ✗ | block[j] = flc; | |
| 2261 | } | ||
| 2262 | 25741 | block[j] = ((block[j] * quant_matrix[j] * h->c.qscale) * (1 << shift)) / 16; | |
| 2263 | 25741 | block[j] = av_clip(block[j], min, max); | |
| 2264 | 25741 | mismatch ^= block[j]; | |
| 2265 | } | ||
| 2266 | |||
| 2267 | 5336 | block[63] ^= mismatch & 1; | |
| 2268 | |||
| 2269 | 5336 | return 0; | |
| 2270 | } | ||
| 2271 | |||
| 2272 | 2049 | static int mpeg4_decode_dpcm_macroblock(Mpeg4DecContext *const ctx, | |
| 2273 | int16_t macroblock[256], int n) | ||
| 2274 | { | ||
| 2275 | 2049 | H263DecContext *const h = &ctx->h; | |
| 2276 | 2049 | int j, w, height, idx = 0; | |
| 2277 | int block_mean, rice_parameter, rice_prefix_code, rice_suffix_code, | ||
| 2278 | dpcm_residual, left, top, topleft, min_left_top, max_left_top, p, p2, output; | ||
| 2279 | 2049 | height = 16 >> (n ? h->c.chroma_y_shift : 0); | |
| 2280 | 2049 | w = 16 >> (n ? h->c.chroma_x_shift : 0); | |
| 2281 | |||
| 2282 | 2049 | block_mean = get_bits(&h->gb, h->c.avctx->bits_per_raw_sample); | |
| 2283 | 2049 | if (block_mean == 0){ | |
| 2284 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden block_mean\n"); | |
| 2285 | ✗ | return AVERROR_INVALIDDATA; | |
| 2286 | } | ||
| 2287 | 2049 | h->last_dc[n] = block_mean * (1 << (ctx->dct_precision + h->c.intra_dc_precision)); | |
| 2288 | |||
| 2289 | 2049 | rice_parameter = get_bits(&h->gb, 4); | |
| 2290 | 2049 | if (rice_parameter == 0) { | |
| 2291 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n"); | |
| 2292 | ✗ | return AVERROR_INVALIDDATA; | |
| 2293 | } | ||
| 2294 | |||
| 2295 | 2049 | if (rice_parameter == 15) | |
| 2296 | 329 | rice_parameter = 0; | |
| 2297 | |||
| 2298 | 2049 | if (rice_parameter > 11) { | |
| 2299 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden rice_parameter\n"); | |
| 2300 | ✗ | return AVERROR_INVALIDDATA; | |
| 2301 | } | ||
| 2302 | |||
| 2303 | 34833 | for (int i = 0; i < height; i++) { | |
| 2304 | 32784 | output = 1 << (h->c.avctx->bits_per_raw_sample - 1); | |
| 2305 | 32784 | top = 1 << (h->c.avctx->bits_per_raw_sample - 1); | |
| 2306 | |||
| 2307 | 382480 | for (j = 0; j < w; j++) { | |
| 2308 | 349696 | left = output; | |
| 2309 | 349696 | topleft = top; | |
| 2310 | |||
| 2311 | 349696 | rice_prefix_code = get_unary(&h->gb, 1, 12); | |
| 2312 | |||
| 2313 | /* Escape */ | ||
| 2314 | 349696 | if (rice_prefix_code == 11) | |
| 2315 | 3815 | dpcm_residual = get_bits(&h->gb, h->c.avctx->bits_per_raw_sample); | |
| 2316 | else { | ||
| 2317 | 345881 | if (rice_prefix_code == 12) { | |
| 2318 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "Forbidden rice_prefix_code\n"); | |
| 2319 | ✗ | return AVERROR_INVALIDDATA; | |
| 2320 | } | ||
| 2321 | 345881 | rice_suffix_code = get_bitsz(&h->gb, rice_parameter); | |
| 2322 | 345881 | dpcm_residual = (rice_prefix_code << rice_parameter) + rice_suffix_code; | |
| 2323 | } | ||
| 2324 | |||
| 2325 | /* Map to a signed residual */ | ||
| 2326 | 349696 | if (dpcm_residual & 1) | |
| 2327 | 94792 | dpcm_residual = (-1 * dpcm_residual) >> 1; | |
| 2328 | else | ||
| 2329 | 254904 | dpcm_residual = (dpcm_residual >> 1); | |
| 2330 | |||
| 2331 | 349696 | if (i != 0) | |
| 2332 | 327840 | top = macroblock[idx-w]; | |
| 2333 | |||
| 2334 | 349696 | p = left + top - topleft; | |
| 2335 | 349696 | min_left_top = FFMIN(left, top); | |
| 2336 | 349696 | if (p < min_left_top) | |
| 2337 | 44503 | p = min_left_top; | |
| 2338 | |||
| 2339 | 349696 | max_left_top = FFMAX(left, top); | |
| 2340 | 349696 | if (p > max_left_top) | |
| 2341 | 42683 | p = max_left_top; | |
| 2342 | |||
| 2343 | 349696 | p2 = (FFMIN(min_left_top, topleft) + FFMAX(max_left_top, topleft)) >> 1; | |
| 2344 | 349696 | if (p2 == p) | |
| 2345 | 67194 | p2 = block_mean; | |
| 2346 | |||
| 2347 | 349696 | if (p2 > p) | |
| 2348 | 174738 | dpcm_residual *= -1; | |
| 2349 | |||
| 2350 | 349696 | macroblock[idx++] = output = (dpcm_residual + p) & ((1 << h->c.avctx->bits_per_raw_sample) - 1); | |
| 2351 | } | ||
| 2352 | } | ||
| 2353 | |||
| 2354 | 2049 | return 0; | |
| 2355 | } | ||
| 2356 | |||
| 2357 | 1350 | static int mpeg4_decode_studio_mb(H263DecContext *const h) | |
| 2358 | { | ||
| 2359 | 1350 | Mpeg4DecContext *const ctx = h263_to_mpeg4(h); | |
| 2360 | int i; | ||
| 2361 | |||
| 2362 | 1350 | ctx->dpcm_direction = 0; | |
| 2363 | |||
| 2364 | /* StudioMacroblock */ | ||
| 2365 | /* Assumes I-VOP */ | ||
| 2366 | 1350 | h->c.mb_intra = 1; | |
| 2367 | 1350 | if (get_bits1(&h->gb)) { /* compression_mode */ | |
| 2368 | /* DCT */ | ||
| 2369 | /* macroblock_type, 1 or 2-bit VLC */ | ||
| 2370 | 667 | if (!get_bits1(&h->gb)) { | |
| 2371 | ✗ | skip_bits1(&h->gb); | |
| 2372 | ✗ | h->c.qscale = mpeg_get_qscale(&h->gb, h->c.q_scale_type); | |
| 2373 | } | ||
| 2374 | |||
| 2375 | 6003 | for (i = 0; i < mpeg4_block_count[h->c.chroma_format]; i++) { | |
| 2376 | 5336 | if (mpeg4_decode_studio_block(ctx, ctx->block32[i], i) < 0) | |
| 2377 | ✗ | return AVERROR_INVALIDDATA; | |
| 2378 | } | ||
| 2379 | } else { | ||
| 2380 | /* DPCM */ | ||
| 2381 | 683 | check_marker(h->c.avctx, &h->gb, "DPCM block start"); | |
| 2382 | 683 | ctx->dpcm_direction = get_bits1(&h->gb) ? -1 : 1; | |
| 2383 | 2732 | for (i = 0; i < 3; i++) { | |
| 2384 | 2049 | if (mpeg4_decode_dpcm_macroblock(ctx, ctx->dpcm_macroblock[i], i) < 0) | |
| 2385 | ✗ | return AVERROR_INVALIDDATA; | |
| 2386 | } | ||
| 2387 | } | ||
| 2388 | |||
| 2389 | 1350 | if (get_bits_left(&h->gb) >= 24 && show_bits(&h->gb, 23) == 0) { | |
| 2390 | 29 | next_start_code_studio(&h->gb); | |
| 2391 | 29 | return SLICE_END; | |
| 2392 | } | ||
| 2393 | |||
| 2394 | //vcon-stp9L1.bits (first frame) | ||
| 2395 | 1321 | if (get_bits_left(&h->gb) == 0) | |
| 2396 | ✗ | return SLICE_END; | |
| 2397 | |||
| 2398 | //vcon-stp2L1.bits, vcon-stp3L1.bits, vcon-stp6L1.bits, vcon-stp7L1.bits, vcon-stp8L1.bits, vcon-stp10L1.bits (first frame) | ||
| 2399 | 1321 | if (get_bits_left(&h->gb) < 8U && show_bits(&h->gb, get_bits_left(&h->gb)) == 0) | |
| 2400 | 1 | return SLICE_END; | |
| 2401 | |||
| 2402 | 1320 | return SLICE_OK; | |
| 2403 | } | ||
| 2404 | |||
| 2405 | 1023 | static int mpeg4_decode_gop_header(MpegEncContext *s, GetBitContext *gb) | |
| 2406 | { | ||
| 2407 | int hours, minutes, seconds; | ||
| 2408 | |||
| 2409 | 1023 | if (!show_bits(gb, 23)) { | |
| 2410 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "GOP header invalid\n"); | |
| 2411 | ✗ | return AVERROR_INVALIDDATA; | |
| 2412 | } | ||
| 2413 | |||
| 2414 | 1023 | hours = get_bits(gb, 5); | |
| 2415 | 1023 | minutes = get_bits(gb, 6); | |
| 2416 | 1023 | check_marker(s->avctx, gb, "in gop_header"); | |
| 2417 | 1023 | seconds = get_bits(gb, 6); | |
| 2418 | |||
| 2419 | 1023 | s->time_base = seconds + 60*(minutes + 60*hours); | |
| 2420 | |||
| 2421 | 1023 | skip_bits1(gb); | |
| 2422 | 1023 | skip_bits1(gb); | |
| 2423 | |||
| 2424 | 1023 | return 0; | |
| 2425 | } | ||
| 2426 | |||
| 2427 | 1224 | static int mpeg4_decode_profile_level(MpegEncContext *s, GetBitContext *gb, int *profile, int *level) | |
| 2428 | { | ||
| 2429 | |||
| 2430 | 1224 | *profile = get_bits(gb, 4); | |
| 2431 | 1224 | *level = get_bits(gb, 4); | |
| 2432 | |||
| 2433 | // for Simple profile, level 0 | ||
| 2434 | 1224 | if (*profile == 0 && *level == 8) { | |
| 2435 | ✗ | *level = 0; | |
| 2436 | } | ||
| 2437 | |||
| 2438 | 1224 | return 0; | |
| 2439 | } | ||
| 2440 | |||
| 2441 | 1224 | static int mpeg4_decode_visual_object(MpegEncContext *s, GetBitContext *gb) | |
| 2442 | { | ||
| 2443 | int visual_object_type; | ||
| 2444 | 1224 | int is_visual_object_identifier = get_bits1(gb); | |
| 2445 | |||
| 2446 | 1224 | if (is_visual_object_identifier) { | |
| 2447 | 1198 | skip_bits(gb, 4+3); | |
| 2448 | } | ||
| 2449 | 1224 | visual_object_type = get_bits(gb, 4); | |
| 2450 | |||
| 2451 | 1224 | if (visual_object_type == VOT_VIDEO_ID || | |
| 2452 | visual_object_type == VOT_STILL_TEXTURE_ID) { | ||
| 2453 | 1224 | int video_signal_type = get_bits1(gb); | |
| 2454 | 1224 | if (video_signal_type) { | |
| 2455 | int video_range, color_description; | ||
| 2456 | 4 | skip_bits(gb, 3); // video_format | |
| 2457 | 4 | video_range = get_bits1(gb); | |
| 2458 | 4 | color_description = get_bits1(gb); | |
| 2459 | |||
| 2460 | 4 | s->avctx->color_range = video_range ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG; | |
| 2461 | |||
| 2462 | 4 | if (color_description) { | |
| 2463 | 4 | s->avctx->color_primaries = get_bits(gb, 8); | |
| 2464 | 4 | s->avctx->color_trc = get_bits(gb, 8); | |
| 2465 | 4 | s->avctx->colorspace = get_bits(gb, 8); | |
| 2466 | |||
| 2467 | // Reserved and unassigned values carry no colour information | ||
| 2468 | 4 | if (s->avctx->color_primaries == AVCOL_PRI_RESERVED0 || | |
| 2469 | ✗ | s->avctx->color_primaries == AVCOL_PRI_RESERVED || | |
| 2470 | ✗ | !av_color_primaries_name(s->avctx->color_primaries)) | |
| 2471 | 4 | s->avctx->color_primaries = AVCOL_PRI_UNSPECIFIED; | |
| 2472 | 4 | if (s->avctx->color_trc == AVCOL_TRC_RESERVED0 || | |
| 2473 | 4 | s->avctx->color_trc == AVCOL_TRC_RESERVED || | |
| 2474 | ✗ | !av_color_transfer_name(s->avctx->color_trc)) | |
| 2475 | 4 | s->avctx->color_trc = AVCOL_TRC_UNSPECIFIED; | |
| 2476 | 4 | if (s->avctx->colorspace == AVCOL_SPC_RESERVED || | |
| 2477 | ✗ | !av_color_space_name(s->avctx->colorspace)) | |
| 2478 | 4 | s->avctx->colorspace = AVCOL_SPC_UNSPECIFIED; | |
| 2479 | } | ||
| 2480 | } | ||
| 2481 | } | ||
| 2482 | |||
| 2483 | 1224 | return 0; | |
| 2484 | } | ||
| 2485 | |||
| 2486 | 110 | static void mpeg4_load_default_matrices(MpegEncContext *s) | |
| 2487 | { | ||
| 2488 | int i, v; | ||
| 2489 | |||
| 2490 | /* load default matrices */ | ||
| 2491 | 7150 | for (i = 0; i < 64; i++) { | |
| 2492 | 7040 | int j = s->idsp.idct_permutation[i]; | |
| 2493 | 7040 | v = ff_mpeg4_default_intra_matrix[i]; | |
| 2494 | 7040 | s->intra_matrix[j] = v; | |
| 2495 | 7040 | s->chroma_intra_matrix[j] = v; | |
| 2496 | |||
| 2497 | 7040 | v = ff_mpeg4_default_non_intra_matrix[i]; | |
| 2498 | 7040 | s->inter_matrix[j] = v; | |
| 2499 | 7040 | s->chroma_inter_matrix[j] = v; | |
| 2500 | } | ||
| 2501 | 110 | } | |
| 2502 | |||
| 2503 | ✗ | static int read_quant_matrix_ext(MpegEncContext *s, GetBitContext *gb) | |
| 2504 | { | ||
| 2505 | int i, j, v; | ||
| 2506 | |||
| 2507 | ✗ | if (get_bits1(gb)) { | |
| 2508 | ✗ | if (get_bits_left(gb) < 64*8) | |
| 2509 | ✗ | return AVERROR_INVALIDDATA; | |
| 2510 | /* intra_quantiser_matrix */ | ||
| 2511 | ✗ | for (i = 0; i < 64; i++) { | |
| 2512 | ✗ | v = get_bits(gb, 8); | |
| 2513 | ✗ | j = s->idsp.idct_permutation[ff_zigzag_direct[i]]; | |
| 2514 | ✗ | s->intra_matrix[j] = v; | |
| 2515 | ✗ | s->chroma_intra_matrix[j] = v; | |
| 2516 | } | ||
| 2517 | } | ||
| 2518 | |||
| 2519 | ✗ | if (get_bits1(gb)) { | |
| 2520 | ✗ | if (get_bits_left(gb) < 64*8) | |
| 2521 | ✗ | return AVERROR_INVALIDDATA; | |
| 2522 | /* non_intra_quantiser_matrix */ | ||
| 2523 | ✗ | for (i = 0; i < 64; i++) { | |
| 2524 | ✗ | get_bits(gb, 8); | |
| 2525 | } | ||
| 2526 | } | ||
| 2527 | |||
| 2528 | ✗ | if (get_bits1(gb)) { | |
| 2529 | ✗ | if (get_bits_left(gb) < 64*8) | |
| 2530 | ✗ | return AVERROR_INVALIDDATA; | |
| 2531 | /* chroma_intra_quantiser_matrix */ | ||
| 2532 | ✗ | for (i = 0; i < 64; i++) { | |
| 2533 | ✗ | v = get_bits(gb, 8); | |
| 2534 | ✗ | j = s->idsp.idct_permutation[ff_zigzag_direct[i]]; | |
| 2535 | ✗ | s->chroma_intra_matrix[j] = v; | |
| 2536 | } | ||
| 2537 | } | ||
| 2538 | |||
| 2539 | ✗ | if (get_bits1(gb)) { | |
| 2540 | ✗ | if (get_bits_left(gb) < 64*8) | |
| 2541 | ✗ | return AVERROR_INVALIDDATA; | |
| 2542 | /* chroma_non_intra_quantiser_matrix */ | ||
| 2543 | ✗ | for (i = 0; i < 64; i++) { | |
| 2544 | ✗ | get_bits(gb, 8); | |
| 2545 | } | ||
| 2546 | } | ||
| 2547 | |||
| 2548 | ✗ | next_start_code_studio(gb); | |
| 2549 | ✗ | return 0; | |
| 2550 | } | ||
| 2551 | |||
| 2552 | 5 | static void extension_and_user_data(MpegEncContext *s, GetBitContext *gb, int id) | |
| 2553 | { | ||
| 2554 | uint32_t startcode; | ||
| 2555 | uint8_t extension_type; | ||
| 2556 | |||
| 2557 | 5 | startcode = show_bits_long(gb, 32); | |
| 2558 | 5 | if (startcode == USER_DATA_STARTCODE || startcode == EXT_STARTCODE) { | |
| 2559 | |||
| 2560 | ✗ | if ((id == 2 || id == 4) && startcode == EXT_STARTCODE) { | |
| 2561 | ✗ | skip_bits_long(gb, 32); | |
| 2562 | ✗ | extension_type = get_bits(gb, 4); | |
| 2563 | ✗ | if (extension_type == QUANT_MATRIX_EXT_ID) | |
| 2564 | ✗ | read_quant_matrix_ext(s, gb); | |
| 2565 | } | ||
| 2566 | } | ||
| 2567 | 5 | } | |
| 2568 | |||
| 2569 | 3 | static int decode_studio_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb) | |
| 2570 | { | ||
| 2571 | 3 | MPVContext *const s = &ctx->h.c; | |
| 2572 | int width, height, aspect_ratio_info; | ||
| 2573 | int bits_per_raw_sample; | ||
| 2574 | int rgb, chroma_format; | ||
| 2575 | |||
| 2576 | // random_accessible_vol and video_object_type_indication have already | ||
| 2577 | // been read by the caller decode_vol_header() | ||
| 2578 | 3 | skip_bits(gb, 4); /* video_object_layer_verid */ | |
| 2579 | 3 | ctx->shape = get_bits(gb, 2); /* video_object_layer_shape */ | |
| 2580 | 3 | skip_bits(gb, 4); /* video_object_layer_shape_extension */ | |
| 2581 | 3 | skip_bits1(gb); /* progressive_sequence */ | |
| 2582 | 3 | if (ctx->shape != RECT_SHAPE) { | |
| 2583 | ✗ | avpriv_request_sample(s->avctx, "MPEG-4 Studio profile non rectangular shape"); | |
| 2584 | ✗ | return AVERROR_PATCHWELCOME; | |
| 2585 | } | ||
| 2586 | 3 | if (ctx->shape != BIN_ONLY_SHAPE) { | |
| 2587 | 3 | rgb = get_bits1(gb); /* rgb_components */ | |
| 2588 | 3 | chroma_format = get_bits(gb, 2); /* chroma_format */ | |
| 2589 | 3 | if (!chroma_format || chroma_format == CHROMA_420 || (rgb && chroma_format == CHROMA_422)) { | |
| 2590 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "illegal chroma format\n"); | |
| 2591 | ✗ | return AVERROR_INVALIDDATA; | |
| 2592 | } | ||
| 2593 | |||
| 2594 | 3 | bits_per_raw_sample = get_bits(gb, 4); /* bit_depth */ | |
| 2595 | 3 | if (bits_per_raw_sample == 10) { | |
| 2596 | 3 | if (rgb) { | |
| 2597 | ✗ | s->avctx->pix_fmt = AV_PIX_FMT_GBRP10; | |
| 2598 | } else { | ||
| 2599 | 3 | s->avctx->pix_fmt = chroma_format == CHROMA_422 ? AV_PIX_FMT_YUV422P10 : AV_PIX_FMT_YUV444P10; | |
| 2600 | } | ||
| 2601 | } else { | ||
| 2602 | ✗ | avpriv_request_sample(s->avctx, "MPEG-4 Studio profile bit-depth %u", bits_per_raw_sample); | |
| 2603 | ✗ | return AVERROR_PATCHWELCOME; | |
| 2604 | } | ||
| 2605 | 3 | if (rgb != ctx->rgb || s->chroma_format != chroma_format) | |
| 2606 | 2 | s->context_reinit = 1; | |
| 2607 | 3 | s->avctx->bits_per_raw_sample = bits_per_raw_sample; | |
| 2608 | 3 | ctx->rgb = rgb; | |
| 2609 | 3 | s->chroma_format = chroma_format; | |
| 2610 | } | ||
| 2611 | 3 | if (ctx->shape == RECT_SHAPE) { | |
| 2612 | 3 | check_marker(s->avctx, gb, "before video_object_layer_width"); | |
| 2613 | 3 | width = get_bits(gb, 14); /* video_object_layer_width */ | |
| 2614 | 3 | check_marker(s->avctx, gb, "before video_object_layer_height"); | |
| 2615 | 3 | height = get_bits(gb, 14); /* video_object_layer_height */ | |
| 2616 | 3 | check_marker(s->avctx, gb, "after video_object_layer_height"); | |
| 2617 | |||
| 2618 | /* Do the same check as non-studio profile */ | ||
| 2619 | 3 | if (width && height) { | |
| 2620 | 3 | if (s->width && s->height && | |
| 2621 | 2 | (s->width != width || s->height != height)) | |
| 2622 | ✗ | s->context_reinit = 1; | |
| 2623 | 3 | s->width = width; | |
| 2624 | 3 | s->height = height; | |
| 2625 | } | ||
| 2626 | } | ||
| 2627 | 3 | aspect_ratio_info = get_bits(gb, 4); | |
| 2628 | 3 | if (aspect_ratio_info == FF_ASPECT_EXTENDED) { | |
| 2629 | ✗ | s->avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width | |
| 2630 | ✗ | s->avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height | |
| 2631 | } else { | ||
| 2632 | 3 | s->avctx->sample_aspect_ratio = ff_h263_pixel_aspect[aspect_ratio_info]; | |
| 2633 | } | ||
| 2634 | 3 | skip_bits(gb, 4); /* frame_rate_code */ | |
| 2635 | 3 | skip_bits(gb, 15); /* first_half_bit_rate */ | |
| 2636 | 3 | check_marker(s->avctx, gb, "after first_half_bit_rate"); | |
| 2637 | 3 | skip_bits(gb, 15); /* latter_half_bit_rate */ | |
| 2638 | 3 | check_marker(s->avctx, gb, "after latter_half_bit_rate"); | |
| 2639 | 3 | skip_bits(gb, 15); /* first_half_vbv_buffer_size */ | |
| 2640 | 3 | check_marker(s->avctx, gb, "after first_half_vbv_buffer_size"); | |
| 2641 | 3 | skip_bits(gb, 3); /* latter_half_vbv_buffer_size */ | |
| 2642 | 3 | skip_bits(gb, 11); /* first_half_vbv_buffer_size */ | |
| 2643 | 3 | check_marker(s->avctx, gb, "after first_half_vbv_buffer_size"); | |
| 2644 | 3 | skip_bits(gb, 15); /* latter_half_vbv_occupancy */ | |
| 2645 | 3 | check_marker(s->avctx, gb, "after latter_half_vbv_occupancy"); | |
| 2646 | 3 | s->low_delay = get_bits1(gb); | |
| 2647 | 3 | ctx->mpeg_quant = get_bits1(gb); /* mpeg2_stream */ | |
| 2648 | |||
| 2649 | 3 | next_start_code_studio(gb); | |
| 2650 | 3 | extension_and_user_data(s, gb, 2); | |
| 2651 | |||
| 2652 | 3 | return 0; | |
| 2653 | } | ||
| 2654 | |||
| 2655 | 1257 | static int decode_vol_header(Mpeg4DecContext *ctx, GetBitContext *gb) | |
| 2656 | { | ||
| 2657 | 1257 | H263DecContext *const h = &ctx->h; | |
| 2658 | int width, height, vo_ver_id, aspect_ratio_info; | ||
| 2659 | |||
| 2660 | /* vol header */ | ||
| 2661 | 1257 | skip_bits(gb, 1); /* random access */ | |
| 2662 | 1257 | ctx->vo_type = get_bits(gb, 8); | |
| 2663 | |||
| 2664 | /* If we are in studio profile (per vo_type), check if its all consistent | ||
| 2665 | * and if so continue pass control to decode_studio_vol_header(). | ||
| 2666 | * elIf something is inconsistent, error out | ||
| 2667 | * else continue with (non studio) vol header decpoding. | ||
| 2668 | */ | ||
| 2669 | 1257 | if (ctx->vo_type == CORE_STUDIO_VO_TYPE || | |
| 2670 | 1254 | ctx->vo_type == SIMPLE_STUDIO_VO_TYPE) { | |
| 2671 | 3 | if (h->c.avctx->profile != AV_PROFILE_UNKNOWN && h->c.avctx->profile != AV_PROFILE_MPEG4_SIMPLE_STUDIO) | |
| 2672 | ✗ | return AVERROR_INVALIDDATA; | |
| 2673 | 3 | h->c.studio_profile = 1; | |
| 2674 | 3 | h->c.avctx->profile = AV_PROFILE_MPEG4_SIMPLE_STUDIO; | |
| 2675 | 3 | return decode_studio_vol_header(ctx, gb); | |
| 2676 | 1254 | } else if (h->c.studio_profile) { | |
| 2677 | ✗ | return AVERROR_PATCHWELCOME; | |
| 2678 | } | ||
| 2679 | |||
| 2680 | 1254 | if (get_bits1(gb) != 0) { /* is_ol_id */ | |
| 2681 | 1226 | vo_ver_id = get_bits(gb, 4); /* vo_ver_id */ | |
| 2682 | 1226 | skip_bits(gb, 3); /* vo_priority */ | |
| 2683 | } else { | ||
| 2684 | 28 | vo_ver_id = 1; | |
| 2685 | } | ||
| 2686 | 1254 | aspect_ratio_info = get_bits(gb, 4); | |
| 2687 | 1254 | if (aspect_ratio_info == FF_ASPECT_EXTENDED) { | |
| 2688 | 2 | h->c.avctx->sample_aspect_ratio.num = get_bits(gb, 8); // par_width | |
| 2689 | 2 | h->c.avctx->sample_aspect_ratio.den = get_bits(gb, 8); // par_height | |
| 2690 | } else { | ||
| 2691 | 1252 | h->c.avctx->sample_aspect_ratio = ff_h263_pixel_aspect[aspect_ratio_info]; | |
| 2692 | } | ||
| 2693 | |||
| 2694 | 1254 | if ((ctx->vol_control_parameters = get_bits1(gb))) { /* vol control parameter */ | |
| 2695 | 1217 | int chroma_format = get_bits(gb, 2); | |
| 2696 | 1217 | if (chroma_format != CHROMA_420) | |
| 2697 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "illegal chroma format\n"); | |
| 2698 | |||
| 2699 | 1217 | h->c.low_delay = get_bits1(gb); | |
| 2700 | 1217 | if (get_bits1(gb)) { /* vbv parameters */ | |
| 2701 | ✗ | get_bits(gb, 15); /* first_half_bitrate */ | |
| 2702 | ✗ | check_marker(h->c.avctx, gb, "after first_half_bitrate"); | |
| 2703 | ✗ | get_bits(gb, 15); /* latter_half_bitrate */ | |
| 2704 | ✗ | check_marker(h->c.avctx, gb, "after latter_half_bitrate"); | |
| 2705 | ✗ | get_bits(gb, 15); /* first_half_vbv_buffer_size */ | |
| 2706 | ✗ | check_marker(h->c.avctx, gb, "after first_half_vbv_buffer_size"); | |
| 2707 | ✗ | get_bits(gb, 3); /* latter_half_vbv_buffer_size */ | |
| 2708 | ✗ | get_bits(gb, 11); /* first_half_vbv_occupancy */ | |
| 2709 | ✗ | check_marker(h->c.avctx, gb, "after first_half_vbv_occupancy"); | |
| 2710 | ✗ | get_bits(gb, 15); /* latter_half_vbv_occupancy */ | |
| 2711 | ✗ | check_marker(h->c.avctx, gb, "after latter_half_vbv_occupancy"); | |
| 2712 | } | ||
| 2713 | } else { | ||
| 2714 | /* is setting low delay flag only once the smartest thing to do? | ||
| 2715 | * low delay detection will not be overridden. */ | ||
| 2716 | 37 | if (h->picture_number == 0) { | |
| 2717 | 8 | switch (ctx->vo_type) { | |
| 2718 | 4 | case SIMPLE_VO_TYPE: | |
| 2719 | case ADV_SIMPLE_VO_TYPE: | ||
| 2720 | 4 | h->c.low_delay = 1; | |
| 2721 | 4 | break; | |
| 2722 | 4 | default: | |
| 2723 | 4 | h->c.low_delay = 0; | |
| 2724 | } | ||
| 2725 | } | ||
| 2726 | } | ||
| 2727 | |||
| 2728 | 1254 | ctx->shape = get_bits(gb, 2); /* vol shape */ | |
| 2729 | 1254 | if (ctx->shape != RECT_SHAPE) | |
| 2730 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "only rectangular vol supported\n"); | |
| 2731 | 1254 | if (ctx->shape == GRAY_SHAPE && vo_ver_id != 1) { | |
| 2732 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "Gray shape not supported\n"); | |
| 2733 | ✗ | skip_bits(gb, 4); /* video_object_layer_shape_extension */ | |
| 2734 | } | ||
| 2735 | |||
| 2736 | 1254 | check_marker(h->c.avctx, gb, "before time_increment_resolution"); | |
| 2737 | |||
| 2738 | 1254 | h->c.avctx->framerate.num = get_bits(gb, 16); | |
| 2739 | 1254 | if (!h->c.avctx->framerate.num) { | |
| 2740 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "framerate==0\n"); | |
| 2741 | ✗ | return AVERROR_INVALIDDATA; | |
| 2742 | } | ||
| 2743 | |||
| 2744 | 1254 | ctx->time_increment_bits = av_log2(h->c.avctx->framerate.num - 1) + 1; | |
| 2745 | 1254 | if (ctx->time_increment_bits < 1) | |
| 2746 | ✗ | ctx->time_increment_bits = 1; | |
| 2747 | |||
| 2748 | 1254 | check_marker(h->c.avctx, gb, "before fixed_vop_rate"); | |
| 2749 | |||
| 2750 | 1254 | if (get_bits1(gb) != 0) /* fixed_vop_rate */ | |
| 2751 | 19 | h->c.avctx->framerate.den = get_bits(gb, ctx->time_increment_bits); | |
| 2752 | else | ||
| 2753 | 1235 | h->c.avctx->framerate.den = 1; | |
| 2754 | |||
| 2755 | 1254 | ctx->t_frame = 0; | |
| 2756 | |||
| 2757 | 1254 | if (ctx->shape != BIN_ONLY_SHAPE) { | |
| 2758 | 1254 | if (ctx->shape == RECT_SHAPE) { | |
| 2759 | 1254 | check_marker(h->c.avctx, gb, "before width"); | |
| 2760 | 1254 | width = get_bits(gb, 13); | |
| 2761 | 1254 | check_marker(h->c.avctx, gb, "before height"); | |
| 2762 | 1254 | height = get_bits(gb, 13); | |
| 2763 | 1254 | check_marker(h->c.avctx, gb, "after height"); | |
| 2764 | 1254 | if (width && height && /* they should be non zero but who knows */ | |
| 2765 | 1254 | !(h->c.width && h->c.codec_tag == AV_RL32("MP4S"))) { | |
| 2766 | 1254 | if (h->c.width && h->c.height && | |
| 2767 | 888 | (h->c.width != width || h->c.height != height)) | |
| 2768 | 20 | h->c.context_reinit = 1; | |
| 2769 | 1254 | h->c.width = width; | |
| 2770 | 1254 | h->c.height = height; | |
| 2771 | } | ||
| 2772 | } | ||
| 2773 | |||
| 2774 | 1254 | h->c.progressive_sequence = | |
| 2775 | 1254 | h->c.progressive_frame = get_bits1(gb) ^ 1; | |
| 2776 | 1254 | h->c.interlaced_dct = 0; | |
| 2777 | 1254 | if (!get_bits1(gb) && (h->c.avctx->debug & FF_DEBUG_PICT_INFO)) | |
| 2778 | ✗ | av_log(h->c.avctx, AV_LOG_INFO, /* OBMC Disable */ | |
| 2779 | "MPEG-4 OBMC not supported (very likely buggy encoder)\n"); | ||
| 2780 | 1254 | if (vo_ver_id == 1) | |
| 2781 | 763 | ctx->vol_sprite_usage = get_bits1(gb); /* vol_sprite_usage */ | |
| 2782 | else | ||
| 2783 | 491 | ctx->vol_sprite_usage = get_bits(gb, 2); /* vol_sprite_usage */ | |
| 2784 | |||
| 2785 | 1254 | if (ctx->vol_sprite_usage == STATIC_SPRITE) | |
| 2786 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "Static Sprites not supported\n"); | |
| 2787 | 1254 | if (ctx->vol_sprite_usage == STATIC_SPRITE || | |
| 2788 | 1254 | ctx->vol_sprite_usage == GMC_SPRITE) { | |
| 2789 | ✗ | if (ctx->vol_sprite_usage == STATIC_SPRITE) { | |
| 2790 | ✗ | skip_bits(gb, 13); // sprite_width | |
| 2791 | ✗ | check_marker(h->c.avctx, gb, "after sprite_width"); | |
| 2792 | ✗ | skip_bits(gb, 13); // sprite_height | |
| 2793 | ✗ | check_marker(h->c.avctx, gb, "after sprite_height"); | |
| 2794 | ✗ | skip_bits(gb, 13); // sprite_left | |
| 2795 | ✗ | check_marker(h->c.avctx, gb, "after sprite_left"); | |
| 2796 | ✗ | skip_bits(gb, 13); // sprite_top | |
| 2797 | ✗ | check_marker(h->c.avctx, gb, "after sprite_top"); | |
| 2798 | } | ||
| 2799 | ✗ | ctx->num_sprite_warping_points = get_bits(gb, 6); | |
| 2800 | ✗ | if (ctx->num_sprite_warping_points > 3) { | |
| 2801 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 2802 | "%d sprite_warping_points\n", | ||
| 2803 | ctx->num_sprite_warping_points); | ||
| 2804 | ✗ | ctx->num_sprite_warping_points = 0; | |
| 2805 | ✗ | return AVERROR_INVALIDDATA; | |
| 2806 | } | ||
| 2807 | ✗ | ctx->sprite_warping_accuracy = get_bits(gb, 2); | |
| 2808 | ✗ | ctx->sprite_brightness_change = get_bits1(gb); | |
| 2809 | ✗ | if (ctx->vol_sprite_usage == STATIC_SPRITE) | |
| 2810 | ✗ | skip_bits1(gb); // low_latency_sprite | |
| 2811 | } | ||
| 2812 | // FIXME sadct disable bit if verid!=1 && shape not rect | ||
| 2813 | |||
| 2814 | 1254 | if (get_bits1(gb) == 1) { /* not_8_bit */ | |
| 2815 | ✗ | ctx->quant_precision = get_bits(gb, 4); /* quant_precision */ | |
| 2816 | ✗ | if (get_bits(gb, 4) != 8) /* bits_per_pixel */ | |
| 2817 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "N-bit not supported\n"); | |
| 2818 | ✗ | if (ctx->quant_precision != 5) | |
| 2819 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 2820 | "quant precision %d\n", ctx->quant_precision); | ||
| 2821 | ✗ | if (ctx->quant_precision < 3 || ctx->quant_precision > 9) | |
| 2822 | ✗ | ctx->quant_precision = 5; | |
| 2823 | } else { | ||
| 2824 | 1254 | ctx->quant_precision = 5; | |
| 2825 | } | ||
| 2826 | |||
| 2827 | // FIXME a bunch of grayscale shape things | ||
| 2828 | |||
| 2829 | 1254 | if ((ctx->mpeg_quant = get_bits1(gb))) { /* vol_quant_type */ | |
| 2830 | int i, v; | ||
| 2831 | |||
| 2832 | 108 | mpeg4_load_default_matrices(&h->c); | |
| 2833 | |||
| 2834 | /* load custom intra matrix */ | ||
| 2835 | 108 | if (get_bits1(gb)) { | |
| 2836 | 8 | int last = 0; | |
| 2837 | 240 | for (i = 0; i < 64; i++) { | |
| 2838 | int j; | ||
| 2839 | 240 | if (get_bits_left(gb) < 8) { | |
| 2840 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n"); | |
| 2841 | ✗ | return AVERROR_INVALIDDATA; | |
| 2842 | } | ||
| 2843 | 240 | v = get_bits(gb, 8); | |
| 2844 | 240 | if (v == 0) | |
| 2845 | 8 | break; | |
| 2846 | |||
| 2847 | 232 | last = v; | |
| 2848 | 232 | j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]]; | |
| 2849 | 232 | h->c.intra_matrix[j] = last; | |
| 2850 | } | ||
| 2851 | |||
| 2852 | /* replicate last value */ | ||
| 2853 | 288 | for (; i < 64; i++) { | |
| 2854 | 280 | int j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]]; | |
| 2855 | 280 | h->c.intra_matrix[j] = last; | |
| 2856 | } | ||
| 2857 | } | ||
| 2858 | |||
| 2859 | /* load custom non intra matrix */ | ||
| 2860 | 108 | if (get_bits1(gb)) { | |
| 2861 | 8 | int last = 0; | |
| 2862 | 240 | for (i = 0; i < 64; i++) { | |
| 2863 | int j; | ||
| 2864 | 240 | if (get_bits_left(gb) < 8) { | |
| 2865 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "insufficient data for custom matrix\n"); | |
| 2866 | ✗ | return AVERROR_INVALIDDATA; | |
| 2867 | } | ||
| 2868 | 240 | v = get_bits(gb, 8); | |
| 2869 | 240 | if (v == 0) | |
| 2870 | 8 | break; | |
| 2871 | |||
| 2872 | 232 | last = v; | |
| 2873 | 232 | j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]]; | |
| 2874 | 232 | h->c.inter_matrix[j] = v; | |
| 2875 | } | ||
| 2876 | |||
| 2877 | /* replicate last value */ | ||
| 2878 | 288 | for (; i < 64; i++) { | |
| 2879 | 280 | int j = h->c.idsp.idct_permutation[ff_zigzag_direct[i]]; | |
| 2880 | 280 | h->c.inter_matrix[j] = last; | |
| 2881 | } | ||
| 2882 | } | ||
| 2883 | |||
| 2884 | // FIXME a bunch of grayscale shape things | ||
| 2885 | } | ||
| 2886 | |||
| 2887 | 1254 | if (vo_ver_id != 1) | |
| 2888 | 491 | h->c.quarter_sample = get_bits1(gb); | |
| 2889 | else | ||
| 2890 | 763 | h->c.quarter_sample = 0; | |
| 2891 | |||
| 2892 | 1254 | if (get_bits_left(gb) < 4) { | |
| 2893 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "VOL Header truncated\n"); | |
| 2894 | ✗ | return AVERROR_INVALIDDATA; | |
| 2895 | } | ||
| 2896 | |||
| 2897 | 1254 | if (!get_bits1(gb)) { | |
| 2898 | ✗ | int pos = get_bits_count(gb); | |
| 2899 | ✗ | int estimation_method = get_bits(gb, 2); | |
| 2900 | ✗ | if (estimation_method < 2) { | |
| 2901 | ✗ | if (!get_bits1(gb)) { | |
| 2902 | ✗ | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* opaque */ | |
| 2903 | ✗ | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* transparent */ | |
| 2904 | ✗ | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_cae */ | |
| 2905 | ✗ | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* inter_cae */ | |
| 2906 | ✗ | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* no_update */ | |
| 2907 | ✗ | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* upsampling */ | |
| 2908 | } | ||
| 2909 | ✗ | if (!get_bits1(gb)) { | |
| 2910 | ✗ | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* intra_blocks */ | |
| 2911 | ✗ | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter_blocks */ | |
| 2912 | ✗ | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* inter4v_blocks */ | |
| 2913 | ✗ | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* not coded blocks */ | |
| 2914 | } | ||
| 2915 | ✗ | if (!check_marker(h->c.avctx, gb, "in complexity estimation part 1")) { | |
| 2916 | ✗ | skip_bits_long(gb, pos - get_bits_count(gb)); | |
| 2917 | ✗ | goto no_cplx_est; | |
| 2918 | } | ||
| 2919 | ✗ | if (!get_bits1(gb)) { | |
| 2920 | ✗ | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_coeffs */ | |
| 2921 | ✗ | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* dct_lines */ | |
| 2922 | ✗ | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* vlc_syms */ | |
| 2923 | ✗ | ctx->cplx_estimation_trash_i += 4 * get_bits1(gb); /* vlc_bits */ | |
| 2924 | } | ||
| 2925 | ✗ | if (!get_bits1(gb)) { | |
| 2926 | ✗ | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* apm */ | |
| 2927 | ✗ | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* npm */ | |
| 2928 | ✗ | ctx->cplx_estimation_trash_b += 8 * get_bits1(gb); /* interpolate_mc_q */ | |
| 2929 | ✗ | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* forwback_mc_q */ | |
| 2930 | ✗ | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel2 */ | |
| 2931 | ✗ | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* halfpel4 */ | |
| 2932 | } | ||
| 2933 | ✗ | if (!check_marker(h->c.avctx, gb, "in complexity estimation part 2")) { | |
| 2934 | ✗ | skip_bits_long(gb, pos - get_bits_count(gb)); | |
| 2935 | ✗ | goto no_cplx_est; | |
| 2936 | } | ||
| 2937 | ✗ | if (estimation_method == 1) { | |
| 2938 | ✗ | ctx->cplx_estimation_trash_i += 8 * get_bits1(gb); /* sadct */ | |
| 2939 | ✗ | ctx->cplx_estimation_trash_p += 8 * get_bits1(gb); /* qpel */ | |
| 2940 | } | ||
| 2941 | } else | ||
| 2942 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 2943 | "Invalid Complexity estimation method %d\n", | ||
| 2944 | estimation_method); | ||
| 2945 | } else { | ||
| 2946 | |||
| 2947 | 1254 | no_cplx_est: | |
| 2948 | 1254 | ctx->cplx_estimation_trash_i = | |
| 2949 | 1254 | ctx->cplx_estimation_trash_p = | |
| 2950 | 1254 | ctx->cplx_estimation_trash_b = 0; | |
| 2951 | } | ||
| 2952 | |||
| 2953 | 1254 | ctx->resync_marker = !get_bits1(gb); /* resync_marker_disabled */ | |
| 2954 | |||
| 2955 | 1254 | h->data_partitioning = get_bits1(gb); | |
| 2956 | 1254 | if (h->data_partitioning) | |
| 2957 | 273 | ctx->rvlc = get_bits1(gb); | |
| 2958 | |||
| 2959 | 1254 | if (vo_ver_id != 1) { | |
| 2960 | 491 | ctx->new_pred = get_bits1(gb); | |
| 2961 | 491 | if (ctx->new_pred) { | |
| 2962 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "new pred not supported\n"); | |
| 2963 | ✗ | skip_bits(gb, 2); /* requested upstream message type */ | |
| 2964 | ✗ | skip_bits1(gb); /* newpred segment type */ | |
| 2965 | } | ||
| 2966 | 491 | if (get_bits1(gb)) // reduced_res_vop | |
| 2967 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 2968 | "reduced resolution VOP not supported\n"); | ||
| 2969 | } else { | ||
| 2970 | 763 | ctx->new_pred = 0; | |
| 2971 | } | ||
| 2972 | |||
| 2973 | 1254 | ctx->scalability = get_bits1(gb); | |
| 2974 | |||
| 2975 | 1254 | if (ctx->scalability) { | |
| 2976 | ✗ | GetBitContext bak = *gb; | |
| 2977 | int h_sampling_factor_n; | ||
| 2978 | int h_sampling_factor_m; | ||
| 2979 | int v_sampling_factor_n; | ||
| 2980 | int v_sampling_factor_m; | ||
| 2981 | |||
| 2982 | ✗ | skip_bits1(gb); // hierarchy_type | |
| 2983 | ✗ | skip_bits(gb, 4); /* ref_layer_id */ | |
| 2984 | ✗ | skip_bits1(gb); /* ref_layer_sampling_dir */ | |
| 2985 | ✗ | h_sampling_factor_n = get_bits(gb, 5); | |
| 2986 | ✗ | h_sampling_factor_m = get_bits(gb, 5); | |
| 2987 | ✗ | v_sampling_factor_n = get_bits(gb, 5); | |
| 2988 | ✗ | v_sampling_factor_m = get_bits(gb, 5); | |
| 2989 | ✗ | ctx->enhancement_type = get_bits1(gb); | |
| 2990 | |||
| 2991 | ✗ | if (h_sampling_factor_n == 0 || h_sampling_factor_m == 0 || | |
| 2992 | ✗ | v_sampling_factor_n == 0 || v_sampling_factor_m == 0) { | |
| 2993 | /* illegal scalability header (VERY broken encoder), | ||
| 2994 | * trying to workaround */ | ||
| 2995 | ✗ | ctx->scalability = 0; | |
| 2996 | ✗ | *gb = bak; | |
| 2997 | } else | ||
| 2998 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "scalability not supported\n"); | |
| 2999 | |||
| 3000 | // bin shape stuff FIXME | ||
| 3001 | } | ||
| 3002 | } | ||
| 3003 | |||
| 3004 | 1254 | if (h->c.avctx->debug&FF_DEBUG_PICT_INFO) { | |
| 3005 | ✗ | av_log(h->c.avctx, AV_LOG_DEBUG, "tb %d/%d, tincrbits:%d, qp_prec:%d, ps:%d, low_delay:%d %s%s%s%s\n", | |
| 3006 | ✗ | h->c.avctx->framerate.den, h->c.avctx->framerate.num, | |
| 3007 | ctx->time_increment_bits, | ||
| 3008 | ctx->quant_precision, | ||
| 3009 | h->c.progressive_sequence, | ||
| 3010 | h->c.low_delay, | ||
| 3011 | ✗ | ctx->scalability ? "scalability " :"" , | |
| 3012 | ✗ | h->c.quarter_sample ? "qpel " : "", | |
| 3013 | ✗ | h->data_partitioning ? "partition " : "", | |
| 3014 | ✗ | ctx->rvlc ? "rvlc " : "" | |
| 3015 | ); | ||
| 3016 | } | ||
| 3017 | |||
| 3018 | 1254 | return 0; | |
| 3019 | } | ||
| 3020 | |||
| 3021 | /** | ||
| 3022 | * Decode the user data stuff in the header. | ||
| 3023 | * Also initializes divx/xvid/lavc_version/build. | ||
| 3024 | */ | ||
| 3025 | 90 | static int decode_user_data(Mpeg4DecContext *ctx, GetBitContext *gb) | |
| 3026 | { | ||
| 3027 | 90 | H263DecContext *const h = &ctx->h; | |
| 3028 | char buf[256]; | ||
| 3029 | int i; | ||
| 3030 | int e; | ||
| 3031 | 90 | int ver = 0, build = 0, ver2 = 0, ver3 = 0; | |
| 3032 | char last; | ||
| 3033 | |||
| 3034 | 980 | for (i = 0; i < 255 && get_bits_count(gb) < gb->size_in_bits; i++) { | |
| 3035 | 944 | if (show_bits(gb, 23) == 0) | |
| 3036 | 54 | break; | |
| 3037 | 890 | buf[i] = get_bits(gb, 8); | |
| 3038 | } | ||
| 3039 | 90 | buf[i] = 0; | |
| 3040 | |||
| 3041 | /* divx detection */ | ||
| 3042 | 90 | e = sscanf(buf, "DivX%dBuild%d%c", &ver, &build, &last); | |
| 3043 | 90 | if (e < 2) | |
| 3044 | 90 | e = sscanf(buf, "DivX%db%d%c", &ver, &build, &last); | |
| 3045 | 90 | if (e >= 2) { | |
| 3046 | 10 | ctx->divx_version = ver; | |
| 3047 | 10 | ctx->divx_build = build; | |
| 3048 | 10 | h->divx_packed = e == 3 && last == 'p'; | |
| 3049 | } | ||
| 3050 | |||
| 3051 | /* libavcodec detection */ | ||
| 3052 | 90 | e = sscanf(buf, "FFmpe%*[^b]b%d", &build) + 3; | |
| 3053 | 90 | if (e != 4) | |
| 3054 | 90 | e = sscanf(buf, "FFmpeg v%d.%d.%d / libavcodec build: %d", &ver, &ver2, &ver3, &build); | |
| 3055 | 90 | if (e != 4) { | |
| 3056 | 90 | e = sscanf(buf, "Lavc%d.%d.%d", &ver, &ver2, &ver3) + 1; | |
| 3057 | 90 | if (e > 1) { | |
| 3058 | 33 | if (ver > 0xFFU || ver2 > 0xFFU || ver3 > 0xFFU) { | |
| 3059 | ✗ | av_log(h->c.avctx, AV_LOG_WARNING, | |
| 3060 | "Unknown Lavc version string encountered, %d.%d.%d; " | ||
| 3061 | "clamping sub-version values to 8-bits.\n", | ||
| 3062 | ver, ver2, ver3); | ||
| 3063 | } | ||
| 3064 | 33 | build = ((ver & 0xFF) << 16) + ((ver2 & 0xFF) << 8) + (ver3 & 0xFF); | |
| 3065 | } | ||
| 3066 | } | ||
| 3067 | 90 | if (e != 4) { | |
| 3068 | 57 | if (strcmp(buf, "ffmpeg") == 0) | |
| 3069 | ✗ | ctx->lavc_build = 4600; | |
| 3070 | } | ||
| 3071 | 90 | if (e == 4) | |
| 3072 | 33 | ctx->lavc_build = build; | |
| 3073 | |||
| 3074 | /* Xvid detection */ | ||
| 3075 | 90 | e = sscanf(buf, "XviD%d", &build); | |
| 3076 | 90 | if (e == 1) | |
| 3077 | 19 | ctx->xvid_build = build; | |
| 3078 | |||
| 3079 | 90 | return 0; | |
| 3080 | } | ||
| 3081 | |||
| 3082 | 10791 | static int decode_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb, | |
| 3083 | int parse_only) | ||
| 3084 | { | ||
| 3085 | 10791 | H263DecContext *const h = &ctx->h; | |
| 3086 | int time_incr, time_increment; | ||
| 3087 | int64_t pts; | ||
| 3088 | |||
| 3089 | 10791 | h->c.mcsel = 0; | |
| 3090 | 10791 | h->c.pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* pict type: I = 0 , P = 1 */ | |
| 3091 | 10791 | if (h->c.pict_type == AV_PICTURE_TYPE_B && h->c.low_delay && | |
| 3092 | ✗ | ctx->vol_control_parameters == 0 && !(h->c.avctx->flags & AV_CODEC_FLAG_LOW_DELAY)) { | |
| 3093 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "low_delay flag set incorrectly, clearing it\n"); | |
| 3094 | ✗ | h->c.low_delay = 0; | |
| 3095 | } | ||
| 3096 | |||
| 3097 | 10791 | h->partitioned_frame = h->data_partitioning && h->c.pict_type != AV_PICTURE_TYPE_B; | |
| 3098 | 10791 | if (h->partitioned_frame) | |
| 3099 | 1028 | h->decode_mb = mpeg4_decode_partitioned_mb; | |
| 3100 | else | ||
| 3101 | 9763 | h->decode_mb = mpeg4_decode_mb; | |
| 3102 | |||
| 3103 | 10791 | time_incr = 0; | |
| 3104 | 11184 | while (get_bits1(gb) != 0) | |
| 3105 | 393 | time_incr++; | |
| 3106 | |||
| 3107 | 10791 | check_marker(h->c.avctx, gb, "before time_increment"); | |
| 3108 | |||
| 3109 | 10791 | if (ctx->time_increment_bits == 0 || | |
| 3110 | 10789 | !(show_bits(gb, ctx->time_increment_bits + 1) & 1)) { | |
| 3111 | 10 | av_log(h->c.avctx, AV_LOG_WARNING, | |
| 3112 | "time_increment_bits %d is invalid in relation to the current bitstream, this is likely caused by a missing VOL header\n", ctx->time_increment_bits); | ||
| 3113 | |||
| 3114 | 10 | for (ctx->time_increment_bits = 1; | |
| 3115 | 110 | ctx->time_increment_bits < 16; | |
| 3116 | 100 | ctx->time_increment_bits++) { | |
| 3117 | 110 | if (h->c.pict_type == AV_PICTURE_TYPE_P || | |
| 3118 | 110 | (h->c.pict_type == AV_PICTURE_TYPE_S && | |
| 3119 | ✗ | ctx->vol_sprite_usage == GMC_SPRITE)) { | |
| 3120 | ✗ | if ((show_bits(gb, ctx->time_increment_bits + 6) & 0x37) == 0x30) | |
| 3121 | ✗ | break; | |
| 3122 | 110 | } else if ((show_bits(gb, ctx->time_increment_bits + 5) & 0x1F) == 0x18) | |
| 3123 | 10 | break; | |
| 3124 | } | ||
| 3125 | |||
| 3126 | 10 | av_log(h->c.avctx, AV_LOG_WARNING, | |
| 3127 | "time_increment_bits set to %d bits, based on bitstream analysis\n", ctx->time_increment_bits); | ||
| 3128 | } | ||
| 3129 | |||
| 3130 | if (IS_3IV1) | ||
| 3131 | time_increment = get_bits1(gb); // FIXME investigate further | ||
| 3132 | else | ||
| 3133 | 10791 | time_increment = get_bits(gb, ctx->time_increment_bits); | |
| 3134 | |||
| 3135 | 10791 | if (h->c.pict_type != AV_PICTURE_TYPE_B) { | |
| 3136 | 9490 | h->c.last_time_base = h->c.time_base; | |
| 3137 | 9490 | h->c.time_base += time_incr; | |
| 3138 | 9490 | h->c.time = h->c.time_base * (int64_t)h->c.avctx->framerate.num + time_increment; | |
| 3139 | 9490 | if (h->c.workaround_bugs & FF_BUG_UMP4) { | |
| 3140 | ✗ | if (h->c.time < h->c.last_non_b_time) { | |
| 3141 | /* header is not mpeg-4-compatible, broken encoder, | ||
| 3142 | * trying to workaround */ | ||
| 3143 | ✗ | h->c.time_base++; | |
| 3144 | ✗ | h->c.time += h->c.avctx->framerate.num; | |
| 3145 | } | ||
| 3146 | } | ||
| 3147 | 9490 | h->c.pp_time = h->c.time - h->c.last_non_b_time; | |
| 3148 | 9490 | h->c.last_non_b_time = h->c.time; | |
| 3149 | } else { | ||
| 3150 | 1301 | h->c.time = (h->c.last_time_base + time_incr) * (int64_t)h->c.avctx->framerate.num + time_increment; | |
| 3151 | 1301 | h->c.pb_time = h->c.pp_time - (h->c.last_non_b_time - h->c.time); | |
| 3152 | 1301 | if (h->c.pp_time <= h->c.pb_time || | |
| 3153 | 1301 | h->c.pp_time <= h->c.pp_time - h->c.pb_time || | |
| 3154 | 1301 | h->c.pp_time <= 0) { | |
| 3155 | /* messed up order, maybe after seeking? skipping current B-frame */ | ||
| 3156 | ✗ | return FRAME_SKIPPED; | |
| 3157 | } | ||
| 3158 | 1301 | ff_mpeg4_init_direct_mv(&h->c); | |
| 3159 | |||
| 3160 | 1301 | if (ctx->t_frame == 0) | |
| 3161 | 215 | ctx->t_frame = h->c.pb_time; | |
| 3162 | 1301 | if (ctx->t_frame == 0) | |
| 3163 | ✗ | ctx->t_frame = 1; // 1/0 protection | |
| 3164 | 1301 | h->c.pp_field_time = (ROUNDED_DIV(h->c.last_non_b_time, ctx->t_frame) - | |
| 3165 | 1301 | ROUNDED_DIV(h->c.last_non_b_time - h->c.pp_time, ctx->t_frame)) * 2; | |
| 3166 | 1301 | h->c.pb_field_time = (ROUNDED_DIV(h->c.time, ctx->t_frame) - | |
| 3167 | 1301 | ROUNDED_DIV(h->c.last_non_b_time - h->c.pp_time, ctx->t_frame)) * 2; | |
| 3168 | 1301 | if (h->c.pp_field_time <= h->c.pb_field_time || h->c.pb_field_time <= 1) { | |
| 3169 | 2 | h->c.pb_field_time = 2; | |
| 3170 | 2 | h->c.pp_field_time = 4; | |
| 3171 | 2 | if (!h->c.progressive_sequence) | |
| 3172 | ✗ | return FRAME_SKIPPED; | |
| 3173 | } | ||
| 3174 | } | ||
| 3175 | |||
| 3176 | 10791 | if (h->c.avctx->framerate.den) | |
| 3177 | 10791 | pts = ROUNDED_DIV(h->c.time, h->c.avctx->framerate.den); | |
| 3178 | else | ||
| 3179 | ✗ | pts = AV_NOPTS_VALUE; | |
| 3180 | ff_dlog(h->c.avctx, "MPEG4 PTS: %"PRId64"\n", pts); | ||
| 3181 | |||
| 3182 | 10791 | check_marker(h->c.avctx, gb, "before vop_coded"); | |
| 3183 | |||
| 3184 | /* vop coded */ | ||
| 3185 | 10791 | if (get_bits1(gb) != 1) { | |
| 3186 | 8 | if (h->c.avctx->debug & FF_DEBUG_PICT_INFO) | |
| 3187 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "vop not coded\n"); | |
| 3188 | 8 | h->skipped_last_frame = 1; | |
| 3189 | 8 | return FRAME_SKIPPED; | |
| 3190 | } | ||
| 3191 | 10783 | if (ctx->new_pred) | |
| 3192 | ✗ | decode_new_pred(ctx, gb); | |
| 3193 | |||
| 3194 | 10783 | if (ctx->shape != BIN_ONLY_SHAPE && | |
| 3195 | 10783 | (h->c.pict_type == AV_PICTURE_TYPE_P || | |
| 3196 | 2377 | (h->c.pict_type == AV_PICTURE_TYPE_S && | |
| 3197 | ✗ | ctx->vol_sprite_usage == GMC_SPRITE))) { | |
| 3198 | /* rounding type for motion estimation */ | ||
| 3199 | 8406 | h->c.no_rounding = get_bits1(gb); | |
| 3200 | } else { | ||
| 3201 | 2377 | h->c.no_rounding = 0; | |
| 3202 | } | ||
| 3203 | // FIXME reduced res stuff | ||
| 3204 | |||
| 3205 | 10783 | if (ctx->shape != RECT_SHAPE) { | |
| 3206 | ✗ | if (ctx->vol_sprite_usage != 1 || h->c.pict_type != AV_PICTURE_TYPE_I) { | |
| 3207 | ✗ | skip_bits(gb, 13); /* width */ | |
| 3208 | ✗ | check_marker(h->c.avctx, gb, "after width"); | |
| 3209 | ✗ | skip_bits(gb, 13); /* height */ | |
| 3210 | ✗ | check_marker(h->c.avctx, gb, "after height"); | |
| 3211 | ✗ | skip_bits(gb, 13); /* hor_spat_ref */ | |
| 3212 | ✗ | check_marker(h->c.avctx, gb, "after hor_spat_ref"); | |
| 3213 | ✗ | skip_bits(gb, 13); /* ver_spat_ref */ | |
| 3214 | } | ||
| 3215 | ✗ | skip_bits1(gb); /* change_CR_disable */ | |
| 3216 | |||
| 3217 | ✗ | if (get_bits1(gb) != 0) | |
| 3218 | ✗ | skip_bits(gb, 8); /* constant_alpha_value */ | |
| 3219 | } | ||
| 3220 | |||
| 3221 | // FIXME complexity estimation stuff | ||
| 3222 | |||
| 3223 | 10783 | if (ctx->shape != BIN_ONLY_SHAPE) { | |
| 3224 | 10783 | skip_bits_long(gb, ctx->cplx_estimation_trash_i); | |
| 3225 | 10783 | if (h->c.pict_type != AV_PICTURE_TYPE_I) | |
| 3226 | 9707 | skip_bits_long(gb, ctx->cplx_estimation_trash_p); | |
| 3227 | 10783 | if (h->c.pict_type == AV_PICTURE_TYPE_B) | |
| 3228 | 1301 | skip_bits_long(gb, ctx->cplx_estimation_trash_b); | |
| 3229 | |||
| 3230 | 10783 | if (get_bits_left(gb) < 3) { | |
| 3231 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "Header truncated\n"); | |
| 3232 | ✗ | return AVERROR_INVALIDDATA; | |
| 3233 | } | ||
| 3234 | 10783 | ctx->intra_dc_threshold = ff_mpeg4_dc_threshold[get_bits(gb, 3)]; | |
| 3235 | 10783 | if (!h->c.progressive_sequence) { | |
| 3236 | 50 | h->c.top_field_first = get_bits1(gb); | |
| 3237 | 50 | h->c.alternate_scan = get_bits1(gb); | |
| 3238 | } else | ||
| 3239 | 10733 | h->c.alternate_scan = 0; | |
| 3240 | } | ||
| 3241 | /* Skip at this point when only parsing since the remaining | ||
| 3242 | * data is not useful for a parser and requires the | ||
| 3243 | * sprite_trajectory VLC to be initialized. */ | ||
| 3244 | 10783 | if (parse_only) | |
| 3245 | 5373 | goto end; | |
| 3246 | |||
| 3247 | 5410 | if (h->c.alternate_scan) { | |
| 3248 | ✗ | ff_init_scantable(h->c.idsp.idct_permutation, &h->c.intra_scantable, ff_alternate_vertical_scan); | |
| 3249 | ✗ | ff_permute_scantable(h->permutated_intra_h_scantable, ff_alternate_vertical_scan, | |
| 3250 | ✗ | h->c.idsp.idct_permutation); | |
| 3251 | } else { | ||
| 3252 | 5410 | ff_init_scantable(h->c.idsp.idct_permutation, &h->c.intra_scantable, ff_zigzag_direct); | |
| 3253 | 5410 | ff_permute_scantable(h->permutated_intra_h_scantable, ff_alternate_horizontal_scan, | |
| 3254 | 5410 | h->c.idsp.idct_permutation); | |
| 3255 | } | ||
| 3256 | 5410 | ff_permute_scantable(h->permutated_intra_v_scantable, ff_alternate_vertical_scan, | |
| 3257 | 5410 | h->c.idsp.idct_permutation); | |
| 3258 | |||
| 3259 | 5410 | if (h->c.pict_type == AV_PICTURE_TYPE_S) { | |
| 3260 | ✗ | if((ctx->vol_sprite_usage == STATIC_SPRITE || | |
| 3261 | ✗ | ctx->vol_sprite_usage == GMC_SPRITE)) { | |
| 3262 | ✗ | if (mpeg4_decode_sprite_trajectory(ctx, gb) < 0) | |
| 3263 | ✗ | return AVERROR_INVALIDDATA; | |
| 3264 | ✗ | if (ctx->sprite_brightness_change) | |
| 3265 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 3266 | "sprite_brightness_change not supported\n"); | ||
| 3267 | ✗ | if (ctx->vol_sprite_usage == STATIC_SPRITE) | |
| 3268 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, "static sprite not supported\n"); | |
| 3269 | } else { | ||
| 3270 | ✗ | memset(ctx->sprite_offset, 0, sizeof(ctx->sprite_offset)); | |
| 3271 | ✗ | memset(ctx->sprite_delta, 0, sizeof(ctx->sprite_delta)); | |
| 3272 | } | ||
| 3273 | } | ||
| 3274 | |||
| 3275 | 5410 | ctx->f_code = 1; | |
| 3276 | 5410 | ctx->b_code = 1; | |
| 3277 | 5410 | if (ctx->shape != BIN_ONLY_SHAPE) { | |
| 3278 | 5410 | h->c.chroma_qscale = h->c.qscale = get_bits(gb, ctx->quant_precision); | |
| 3279 | 5410 | if (h->c.qscale == 0) { | |
| 3280 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 3281 | "Error, header damaged or not MPEG-4 header (qscale=0)\n"); | ||
| 3282 | ✗ | return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then | |
| 3283 | } | ||
| 3284 | |||
| 3285 | 5410 | if (h->c.pict_type != AV_PICTURE_TYPE_I) { | |
| 3286 | 4923 | ctx->f_code = get_bits(gb, 3); /* fcode_for */ | |
| 3287 | 4923 | if (ctx->f_code == 0) { | |
| 3288 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 3289 | "Error, header damaged or not MPEG-4 header (f_code=0)\n"); | ||
| 3290 | ✗ | ctx->f_code = 1; | |
| 3291 | ✗ | return AVERROR_INVALIDDATA; // makes no sense to continue, as there is nothing left from the image then | |
| 3292 | } | ||
| 3293 | } | ||
| 3294 | |||
| 3295 | 5410 | if (h->c.pict_type == AV_PICTURE_TYPE_B) { | |
| 3296 | 648 | ctx->b_code = get_bits(gb, 3); | |
| 3297 | 648 | if (ctx->b_code == 0) { | |
| 3298 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 3299 | "Error, header damaged or not MPEG4 header (b_code=0)\n"); | ||
| 3300 | ✗ | ctx->b_code=1; | |
| 3301 | ✗ | return AVERROR_INVALIDDATA; // makes no sense to continue, as the MV decoding will break very quickly | |
| 3302 | } | ||
| 3303 | } | ||
| 3304 | |||
| 3305 | 5410 | if (h->c.avctx->debug & FF_DEBUG_PICT_INFO) { | |
| 3306 | ✗ | av_log(h->c.avctx, AV_LOG_DEBUG, | |
| 3307 | "qp:%d fc:%d,%d %c size:%d pro:%d alt:%d top:%d %cpel part:%d resync:%d w:%d a:%d rnd:%d vot:%d%s dc:%d ce:%d/%d/%d time:%"PRId64" tincr:%d\n", | ||
| 3308 | h->c.qscale, ctx->f_code, ctx->b_code, | ||
| 3309 | ✗ | h->c.pict_type == AV_PICTURE_TYPE_I ? 'I' : (h->c.pict_type == AV_PICTURE_TYPE_P ? 'P' : (h->c.pict_type == AV_PICTURE_TYPE_B ? 'B' : 'S')), | |
| 3310 | gb->size_in_bits,h->c.progressive_sequence, h->c.alternate_scan, | ||
| 3311 | ✗ | h->c.top_field_first, h->c.quarter_sample ? 'q' : 'h', | |
| 3312 | h->data_partitioning, ctx->resync_marker, | ||
| 3313 | ctx->num_sprite_warping_points, ctx->sprite_warping_accuracy, | ||
| 3314 | ✗ | 1 - h->c.no_rounding, ctx->vo_type, | |
| 3315 | ✗ | ctx->vol_control_parameters ? " VOLC" : " ", ctx->intra_dc_threshold, | |
| 3316 | ctx->cplx_estimation_trash_i, ctx->cplx_estimation_trash_p, | ||
| 3317 | ctx->cplx_estimation_trash_b, | ||
| 3318 | h->c.time, | ||
| 3319 | time_increment | ||
| 3320 | ); | ||
| 3321 | } | ||
| 3322 | |||
| 3323 | 5410 | if (!ctx->scalability) { | |
| 3324 | 5410 | if (ctx->shape != RECT_SHAPE && h->c.pict_type != AV_PICTURE_TYPE_I) | |
| 3325 | ✗ | skip_bits1(gb); // vop shape coding type | |
| 3326 | } else { | ||
| 3327 | ✗ | if (ctx->enhancement_type) { | |
| 3328 | ✗ | int load_backward_shape = get_bits1(gb); | |
| 3329 | ✗ | if (load_backward_shape) | |
| 3330 | ✗ | av_log(h->c.avctx, AV_LOG_ERROR, | |
| 3331 | "load backward shape isn't supported\n"); | ||
| 3332 | } | ||
| 3333 | ✗ | skip_bits(gb, 2); // ref_select_code | |
| 3334 | } | ||
| 3335 | } | ||
| 3336 | |||
| 3337 | 10820 | h->c.dct_unquantize_intra = ctx->mpeg_quant ? ctx->dct_unquantize_mpeg2_intra | |
| 3338 | 5410 | : ctx->dct_unquantize_h263_intra; | |
| 3339 | // The following tells ff_mpv_reconstruct_mb() to unquantize iff mpeg_quant | ||
| 3340 | 5410 | h->c.dct_unquantize_inter = ctx->mpeg_quant ? ctx->dct_unquantize_mpeg2_inter : NULL; | |
| 3341 | |||
| 3342 | 10783 | end: | |
| 3343 | /* detect buggy encoders which don't set the low_delay flag | ||
| 3344 | * (divx4/xvid/opendivx). Note we cannot detect divx5 without B-frames | ||
| 3345 | * easily (although it's buggy too) */ | ||
| 3346 | 10783 | if (ctx->vo_type == 0 && ctx->vol_control_parameters == 0 && | |
| 3347 | 52 | ctx->divx_version == -1 && h->picture_number == 0) { | |
| 3348 | 2 | av_log(h->c.avctx, AV_LOG_WARNING, | |
| 3349 | "looks like this file was encoded with (divx4/(old)xvid/opendivx) -> forcing low_delay flag\n"); | ||
| 3350 | 2 | h->c.low_delay = 1; | |
| 3351 | } | ||
| 3352 | |||
| 3353 | 10783 | h->picture_number++; // better than pic number==0 always ;) | |
| 3354 | |||
| 3355 | 10783 | if (h->c.workaround_bugs & FF_BUG_EDGE) { | |
| 3356 | ✗ | h->c.h_edge_pos = h->c.width; | |
| 3357 | ✗ | h->c.v_edge_pos = h->c.height; | |
| 3358 | } | ||
| 3359 | 10783 | return 0; | |
| 3360 | } | ||
| 3361 | |||
| 3362 | 2 | static void decode_smpte_tc(Mpeg4DecContext *ctx, GetBitContext *gb) | |
| 3363 | { | ||
| 3364 | 2 | AVCodecContext *const avctx = ctx->h.c.avctx; | |
| 3365 | |||
| 3366 | 2 | skip_bits(gb, 16); /* Time_code[63..48] */ | |
| 3367 | 2 | check_marker(avctx, gb, "after Time_code[63..48]"); | |
| 3368 | 2 | skip_bits(gb, 16); /* Time_code[47..32] */ | |
| 3369 | 2 | check_marker(avctx, gb, "after Time_code[47..32]"); | |
| 3370 | 2 | skip_bits(gb, 16); /* Time_code[31..16] */ | |
| 3371 | 2 | check_marker(avctx, gb, "after Time_code[31..16]"); | |
| 3372 | 2 | skip_bits(gb, 16); /* Time_code[15..0] */ | |
| 3373 | 2 | check_marker(avctx, gb, "after Time_code[15..0]"); | |
| 3374 | 2 | skip_bits(gb, 4); /* reserved_bits */ | |
| 3375 | 2 | } | |
| 3376 | |||
| 3377 | /** | ||
| 3378 | * Decode the next studio vop header. | ||
| 3379 | * @return <0 if something went wrong | ||
| 3380 | */ | ||
| 3381 | 2 | static int decode_studio_vop_header(Mpeg4DecContext *ctx, GetBitContext *gb) | |
| 3382 | { | ||
| 3383 | 2 | H263DecContext *const h = &ctx->h; | |
| 3384 | |||
| 3385 | 2 | if (get_bits_left(gb) <= 32) | |
| 3386 | ✗ | return 0; | |
| 3387 | |||
| 3388 | 2 | h->partitioned_frame = 0; | |
| 3389 | 2 | h->c.interlaced_dct = 0; | |
| 3390 | 2 | h->decode_mb = mpeg4_decode_studio_mb; | |
| 3391 | |||
| 3392 | 2 | decode_smpte_tc(ctx, gb); | |
| 3393 | |||
| 3394 | 2 | skip_bits(gb, 10); /* temporal_reference */ | |
| 3395 | 2 | skip_bits(gb, 2); /* vop_structure */ | |
| 3396 | 2 | h->c.pict_type = get_bits(gb, 2) + AV_PICTURE_TYPE_I; /* vop_coding_type */ | |
| 3397 | 2 | if (get_bits1(gb)) { /* vop_coded */ | |
| 3398 | 2 | skip_bits1(gb); /* top_field_first */ | |
| 3399 | 2 | skip_bits1(gb); /* repeat_first_field */ | |
| 3400 | 2 | h->c.progressive_frame = get_bits1(gb) ^ 1; /* progressive_frame */ | |
| 3401 | } | ||
| 3402 | |||
| 3403 | 2 | if (h->c.pict_type == AV_PICTURE_TYPE_I) { | |
| 3404 | 2 | if (get_bits1(gb)) | |
| 3405 | ✗ | reset_studio_dc_predictors(ctx); | |
| 3406 | } | ||
| 3407 | |||
| 3408 | 2 | if (ctx->shape != BIN_ONLY_SHAPE) { | |
| 3409 | 2 | h->c.alternate_scan = get_bits1(gb); | |
| 3410 | 2 | h->c.frame_pred_frame_dct = get_bits1(gb); | |
| 3411 | 2 | ctx->dct_precision = get_bits(gb, 2); | |
| 3412 | 2 | h->c.intra_dc_precision = get_bits(gb, 2); | |
| 3413 | 2 | h->c.q_scale_type = get_bits1(gb); | |
| 3414 | } | ||
| 3415 | |||
| 3416 | 2 | ff_init_scantable(h->c.idsp.idct_permutation, &h->c.intra_scantable, | |
| 3417 | 2 | h->c.alternate_scan ? ff_alternate_vertical_scan : ff_zigzag_direct); | |
| 3418 | |||
| 3419 | 2 | mpeg4_load_default_matrices(&h->c); | |
| 3420 | |||
| 3421 | 2 | next_start_code_studio(gb); | |
| 3422 | 2 | extension_and_user_data(&h->c, gb, 4); | |
| 3423 | |||
| 3424 | 2 | return 0; | |
| 3425 | } | ||
| 3426 | |||
| 3427 | ✗ | static int decode_studiovisualobject(Mpeg4DecContext *ctx, GetBitContext *gb) | |
| 3428 | { | ||
| 3429 | int visual_object_type; | ||
| 3430 | |||
| 3431 | ✗ | skip_bits(gb, 4); /* visual_object_verid */ | |
| 3432 | ✗ | visual_object_type = get_bits(gb, 4); | |
| 3433 | ✗ | if (visual_object_type != VOT_VIDEO_ID) { | |
| 3434 | ✗ | avpriv_request_sample(ctx->h.c.avctx, "VO type %u", visual_object_type); | |
| 3435 | ✗ | return AVERROR_PATCHWELCOME; | |
| 3436 | } | ||
| 3437 | |||
| 3438 | ✗ | next_start_code_studio(gb); | |
| 3439 | ✗ | extension_and_user_data(&ctx->h.c, gb, 1); | |
| 3440 | |||
| 3441 | ✗ | return 0; | |
| 3442 | } | ||
| 3443 | |||
| 3444 | /** | ||
| 3445 | * Decode MPEG-4 headers. | ||
| 3446 | * | ||
| 3447 | * @param header If set the absence of a VOP is not treated as error; otherwise, it is treated as such. | ||
| 3448 | * @param parse_only If set, things only relevant to a decoder may be skipped; | ||
| 3449 | * furthermore, the VLC tables may be uninitialized. | ||
| 3450 | * @return <0 if an error occurred | ||
| 3451 | * FRAME_SKIPPED if a not coded VOP is found | ||
| 3452 | * 0 else | ||
| 3453 | */ | ||
| 3454 | 11295 | int ff_mpeg4_parse_picture_header(Mpeg4DecContext *ctx, GetBitContext *gb, | |
| 3455 | int header, int parse_only) | ||
| 3456 | { | ||
| 3457 | 11295 | MPVContext *const s = &ctx->h.c; | |
| 3458 | unsigned startcode, v; | ||
| 3459 | int ret; | ||
| 3460 | 11295 | int vol = 0; | |
| 3461 | |||
| 3462 | /* search next start code */ | ||
| 3463 | 11295 | align_get_bits(gb); | |
| 3464 | |||
| 3465 | // If we have not switched to studio profile than we also did not switch bps | ||
| 3466 | // that means something else (like a previous instance) outside set bps which | ||
| 3467 | // would be inconsistent with the correct state, thus reset it | ||
| 3468 | 11295 | if (!s->studio_profile && s->avctx->bits_per_raw_sample != 8) | |
| 3469 | 11293 | s->avctx->bits_per_raw_sample = 0; | |
| 3470 | |||
| 3471 | 11295 | if (s->codec_tag == AV_RL32("WV1F") && show_bits(gb, 24) == 0x575630) { | |
| 3472 | ✗ | skip_bits(gb, 24); | |
| 3473 | ✗ | if (get_bits(gb, 8) == 0xF0) | |
| 3474 | ✗ | goto end; | |
| 3475 | } | ||
| 3476 | |||
| 3477 | 11295 | startcode = 0xff; | |
| 3478 | for (;;) { | ||
| 3479 | 67992 | if (get_bits_count(gb) >= gb->size_in_bits) { | |
| 3480 | 502 | if (gb->size_in_bits == 8 && | |
| 3481 | 502 | (ctx->divx_version >= 0 || ctx->xvid_build >= 0) || s->codec_tag == AV_RL32("QMP4")) { | |
| 3482 | ✗ | av_log(s->avctx, AV_LOG_VERBOSE, "frame skip %d\n", gb->size_in_bits); | |
| 3483 | ✗ | return FRAME_SKIPPED; // divx bug | |
| 3484 | 502 | } else if (header && get_bits_count(gb) == gb->size_in_bits) { | |
| 3485 | 398 | return 0; // ordinary return value for parsing of extradata | |
| 3486 | } else | ||
| 3487 | 104 | return AVERROR_INVALIDDATA; // end of stream | |
| 3488 | } | ||
| 3489 | |||
| 3490 | /* use the bits after the test */ | ||
| 3491 | 67490 | v = get_bits(gb, 8); | |
| 3492 | 67490 | startcode = ((startcode << 8) | v) & 0xffffffff; | |
| 3493 | |||
| 3494 | 67490 | if ((startcode & 0xFFFFFF00) != 0x100) | |
| 3495 | 50622 | continue; // no startcode | |
| 3496 | |||
| 3497 | 16868 | if (s->avctx->debug & FF_DEBUG_STARTCODE) { | |
| 3498 | const char *name; | ||
| 3499 | ✗ | if (startcode <= 0x11F) | |
| 3500 | ✗ | name = "Video Object Start"; | |
| 3501 | ✗ | else if (startcode <= 0x12F) | |
| 3502 | ✗ | name = "Video Object Layer Start"; | |
| 3503 | ✗ | else if (startcode <= 0x13F) | |
| 3504 | ✗ | name = "Reserved"; | |
| 3505 | ✗ | else if (startcode <= 0x15F) | |
| 3506 | ✗ | name = "FGS bp start"; | |
| 3507 | ✗ | else if (startcode <= 0x1AF) | |
| 3508 | ✗ | name = "Reserved"; | |
| 3509 | ✗ | else if (startcode == 0x1B0) | |
| 3510 | ✗ | name = "Visual Object Seq Start"; | |
| 3511 | ✗ | else if (startcode == 0x1B1) | |
| 3512 | ✗ | name = "Visual Object Seq End"; | |
| 3513 | ✗ | else if (startcode == 0x1B2) | |
| 3514 | ✗ | name = "User Data"; | |
| 3515 | ✗ | else if (startcode == 0x1B3) | |
| 3516 | ✗ | name = "Group of VOP start"; | |
| 3517 | ✗ | else if (startcode == 0x1B4) | |
| 3518 | ✗ | name = "Video Session Error"; | |
| 3519 | ✗ | else if (startcode == 0x1B5) | |
| 3520 | ✗ | name = "Visual Object Start"; | |
| 3521 | ✗ | else if (startcode == 0x1B6) | |
| 3522 | ✗ | name = "Video Object Plane start"; | |
| 3523 | ✗ | else if (startcode == 0x1B7) | |
| 3524 | ✗ | name = "slice start"; | |
| 3525 | ✗ | else if (startcode == 0x1B8) | |
| 3526 | ✗ | name = "extension start"; | |
| 3527 | ✗ | else if (startcode == 0x1B9) | |
| 3528 | ✗ | name = "fgs start"; | |
| 3529 | ✗ | else if (startcode == 0x1BA) | |
| 3530 | ✗ | name = "FBA Object start"; | |
| 3531 | ✗ | else if (startcode == 0x1BB) | |
| 3532 | ✗ | name = "FBA Object Plane start"; | |
| 3533 | ✗ | else if (startcode == 0x1BC) | |
| 3534 | ✗ | name = "Mesh Object start"; | |
| 3535 | ✗ | else if (startcode == 0x1BD) | |
| 3536 | ✗ | name = "Mesh Object Plane start"; | |
| 3537 | ✗ | else if (startcode == 0x1BE) | |
| 3538 | ✗ | name = "Still Texture Object start"; | |
| 3539 | ✗ | else if (startcode == 0x1BF) | |
| 3540 | ✗ | name = "Texture Spatial Layer start"; | |
| 3541 | ✗ | else if (startcode == 0x1C0) | |
| 3542 | ✗ | name = "Texture SNR Layer start"; | |
| 3543 | ✗ | else if (startcode == 0x1C1) | |
| 3544 | ✗ | name = "Texture Tile start"; | |
| 3545 | ✗ | else if (startcode == 0x1C2) | |
| 3546 | ✗ | name = "Texture Shape Layer start"; | |
| 3547 | ✗ | else if (startcode == 0x1C3) | |
| 3548 | ✗ | name = "stuffing start"; | |
| 3549 | ✗ | else if (startcode <= 0x1C5) | |
| 3550 | ✗ | name = "Reserved"; | |
| 3551 | ✗ | else if (startcode <= 0x1FF) | |
| 3552 | ✗ | name = "System start"; | |
| 3553 | else | ||
| 3554 | ✗ | av_unreachable("Unexpected startcode"); | |
| 3555 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "startcode: %3X %s at %d\n", | |
| 3556 | startcode, name, get_bits_count(gb)); | ||
| 3557 | } | ||
| 3558 | |||
| 3559 | 16868 | if (startcode >= 0x120 && startcode <= 0x12F) { | |
| 3560 | 1257 | if (vol) { | |
| 3561 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Ignoring multiple VOL headers\n"); | |
| 3562 | ✗ | continue; | |
| 3563 | } | ||
| 3564 | 1257 | vol++; | |
| 3565 | 1257 | if ((ret = decode_vol_header(ctx, gb)) < 0) | |
| 3566 | ✗ | return ret; | |
| 3567 | 15611 | } else if (startcode == USER_DATA_STARTCODE) { | |
| 3568 | 90 | decode_user_data(ctx, gb); | |
| 3569 | 15521 | } else if (startcode == GOP_STARTCODE) { | |
| 3570 | 1023 | mpeg4_decode_gop_header(s, gb); | |
| 3571 | 14498 | } else if (startcode == VOS_STARTCODE) { | |
| 3572 | int profile, level; | ||
| 3573 | 1224 | mpeg4_decode_profile_level(s, gb, &profile, &level); | |
| 3574 | 1224 | if (profile == AV_PROFILE_MPEG4_SIMPLE_STUDIO && | |
| 3575 | ✗ | (level > 0 && level < 9)) { | |
| 3576 | ✗ | s->studio_profile = 1; | |
| 3577 | ✗ | next_start_code_studio(gb); | |
| 3578 | ✗ | extension_and_user_data(s, gb, 0); | |
| 3579 | 1224 | } else if (s->studio_profile) { | |
| 3580 | ✗ | avpriv_request_sample(s->avctx, "Mix of studio and non studio profile"); | |
| 3581 | ✗ | return AVERROR_PATCHWELCOME; | |
| 3582 | } | ||
| 3583 | 1224 | s->avctx->profile = profile; | |
| 3584 | 1224 | s->avctx->level = level; | |
| 3585 | 13274 | } else if (startcode == VISUAL_OBJ_STARTCODE) { | |
| 3586 | 1224 | if (s->studio_profile) { | |
| 3587 | ✗ | if ((ret = decode_studiovisualobject(ctx, gb)) < 0) | |
| 3588 | ✗ | return ret; | |
| 3589 | } else | ||
| 3590 | 1224 | mpeg4_decode_visual_object(s, gb); | |
| 3591 | 12050 | } else if (startcode == VOP_STARTCODE) { | |
| 3592 | 10793 | break; | |
| 3593 | } | ||
| 3594 | |||
| 3595 | 6075 | align_get_bits(gb); | |
| 3596 | 6075 | startcode = 0xff; | |
| 3597 | } | ||
| 3598 | |||
| 3599 | 10793 | end: | |
| 3600 | 10793 | if (s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY) | |
| 3601 | ✗ | s->low_delay = 1; | |
| 3602 | |||
| 3603 | 10793 | if (s->studio_profile) { | |
| 3604 | 2 | if (!s->avctx->bits_per_raw_sample) { | |
| 3605 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Missing VOL header\n"); | |
| 3606 | ✗ | return AVERROR_INVALIDDATA; | |
| 3607 | } | ||
| 3608 | 2 | return decode_studio_vop_header(ctx, gb); | |
| 3609 | } else | ||
| 3610 | 10791 | return decode_vop_header(ctx, gb, parse_only); | |
| 3611 | } | ||
| 3612 | |||
| 3613 | #if CONFIG_MPEG4_DECODER | ||
| 3614 | 8 | static av_cold void permute_quant_matrix(uint16_t matrix[64], | |
| 3615 | const uint8_t new_perm[64], | ||
| 3616 | const uint8_t old_perm[64]) | ||
| 3617 | { | ||
| 3618 | uint16_t tmp[64]; | ||
| 3619 | |||
| 3620 | 8 | memcpy(tmp, matrix, sizeof(tmp)); | |
| 3621 | 520 | for (int i = 0; i < 64; ++i) | |
| 3622 | 512 | matrix[new_perm[i]] = tmp[old_perm[i]]; | |
| 3623 | 8 | } | |
| 3624 | |||
| 3625 | 4 | static av_cold void switch_to_xvid_idct(AVCodecContext *const avctx, | |
| 3626 | H263DecContext *const h) | ||
| 3627 | { | ||
| 3628 | uint8_t old_permutation[64]; | ||
| 3629 | |||
| 3630 | 4 | memcpy(old_permutation, h->c.idsp.idct_permutation, sizeof(old_permutation)); | |
| 3631 | |||
| 3632 | 4 | avctx->idct_algo = FF_IDCT_XVID; | |
| 3633 | 4 | ff_mpv_idct_init(&h->c); | |
| 3634 | 4 | ff_permute_scantable(h->permutated_intra_h_scantable, | |
| 3635 | 4 | h->c.alternate_scan ? ff_alternate_vertical_scan : ff_alternate_horizontal_scan, | |
| 3636 | 4 | h->c.idsp.idct_permutation); | |
| 3637 | 4 | ff_permute_scantable(h->permutated_intra_v_scantable, ff_alternate_vertical_scan, | |
| 3638 | 4 | h->c.idsp.idct_permutation); | |
| 3639 | |||
| 3640 | // Normal (i.e. non-studio) MPEG-4 does not use the chroma matrices. | ||
| 3641 | 4 | permute_quant_matrix(h->c.inter_matrix, h->c.idsp.idct_permutation, old_permutation); | |
| 3642 | 4 | permute_quant_matrix(h->c.intra_matrix, h->c.idsp.idct_permutation, old_permutation); | |
| 3643 | 4 | } | |
| 3644 | |||
| 3645 | 5411 | void ff_mpeg4_workaround_bugs(AVCodecContext *avctx) | |
| 3646 | { | ||
| 3647 | 5411 | Mpeg4DecContext *ctx = avctx->priv_data; | |
| 3648 | 5411 | H263DecContext *const h = &ctx->h; | |
| 3649 | |||
| 3650 | 5411 | if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) { | |
| 3651 | 5245 | if (h->c.codec_tag == AV_RL32("XVID") || | |
| 3652 | 5245 | h->c.codec_tag == AV_RL32("XVIX") || | |
| 3653 | 5245 | h->c.codec_tag == AV_RL32("RMP4") || | |
| 3654 | 5245 | h->c.codec_tag == AV_RL32("ZMP4") || | |
| 3655 | 5245 | h->c.codec_tag == AV_RL32("SIPP")) | |
| 3656 | ✗ | ctx->xvid_build = 0; | |
| 3657 | } | ||
| 3658 | |||
| 3659 | 5411 | if (ctx->xvid_build == -1 && ctx->divx_version == -1 && ctx->lavc_build == -1) | |
| 3660 | 5245 | if (h->c.codec_tag == AV_RL32("DIVX") && ctx->vo_type == 0 && | |
| 3661 | ✗ | ctx->vol_control_parameters == 0) | |
| 3662 | ✗ | ctx->divx_version = 400; // divx 4 | |
| 3663 | |||
| 3664 | 5411 | if (ctx->xvid_build >= 0 && ctx->divx_version >= 0) { | |
| 3665 | 3 | ctx->divx_version = | |
| 3666 | 3 | ctx->divx_build = -1; | |
| 3667 | } | ||
| 3668 | |||
| 3669 | 5411 | if (h->c.workaround_bugs & FF_BUG_AUTODETECT) { | |
| 3670 | 5411 | if (h->c.codec_tag == AV_RL32("XVIX")) | |
| 3671 | ✗ | h->c.workaround_bugs |= FF_BUG_XVID_ILACE; | |
| 3672 | |||
| 3673 | 5411 | if (h->c.codec_tag == AV_RL32("UMP4")) | |
| 3674 | ✗ | h->c.workaround_bugs |= FF_BUG_UMP4; | |
| 3675 | |||
| 3676 | 5411 | if (ctx->divx_version >= 500 && ctx->divx_build < 1814) | |
| 3677 | ✗ | h->c.workaround_bugs |= FF_BUG_QPEL_CHROMA; | |
| 3678 | |||
| 3679 | 5411 | if (ctx->divx_version > 502 && ctx->divx_build < 1814) | |
| 3680 | ✗ | h->c.workaround_bugs |= FF_BUG_QPEL_CHROMA2; | |
| 3681 | |||
| 3682 | 5411 | if (ctx->xvid_build <= 3U) | |
| 3683 | ✗ | h->padding_bug_score = 256 * 256 * 256 * 64; | |
| 3684 | |||
| 3685 | 5411 | if (ctx->xvid_build <= 1U) | |
| 3686 | ✗ | h->c.workaround_bugs |= FF_BUG_QPEL_CHROMA; | |
| 3687 | |||
| 3688 | 5411 | if (ctx->xvid_build <= 12U) | |
| 3689 | ✗ | h->c.workaround_bugs |= FF_BUG_EDGE; | |
| 3690 | |||
| 3691 | 5411 | if (ctx->xvid_build <= 32U) | |
| 3692 | ✗ | h->c.workaround_bugs |= FF_BUG_DC_CLIP; | |
| 3693 | |||
| 3694 | #define SET_QPEL_FUNC(postfix1, postfix2) \ | ||
| 3695 | h->c.qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \ | ||
| 3696 | h->c.qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \ | ||
| 3697 | h->c.qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2; | ||
| 3698 | |||
| 3699 | 5411 | if (ctx->lavc_build < 4653U) | |
| 3700 | ✗ | h->c.workaround_bugs |= FF_BUG_STD_QPEL; | |
| 3701 | |||
| 3702 | 5411 | if (ctx->lavc_build < 4655U) | |
| 3703 | ✗ | h->c.workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE; | |
| 3704 | |||
| 3705 | 5411 | if (ctx->lavc_build < 4670U) | |
| 3706 | ✗ | h->c.workaround_bugs |= FF_BUG_EDGE; | |
| 3707 | |||
| 3708 | 5411 | if (ctx->lavc_build <= 4712U) | |
| 3709 | ✗ | h->c.workaround_bugs |= FF_BUG_DC_CLIP; | |
| 3710 | |||
| 3711 | 5411 | if ((ctx->lavc_build&0xFF) >= 100) { | |
| 3712 | 5411 | if (ctx->lavc_build > 3621476 && ctx->lavc_build < 3752552 && | |
| 3713 | ✗ | (ctx->lavc_build < 3752037 || ctx->lavc_build > 3752191) // 3.2.1+ | |
| 3714 | ) | ||
| 3715 | ✗ | h->c.workaround_bugs |= FF_BUG_IEDGE; | |
| 3716 | } | ||
| 3717 | |||
| 3718 | 5411 | if (ctx->divx_version >= 0) | |
| 3719 | ✗ | h->c.workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE; | |
| 3720 | 5411 | if (ctx->divx_version == 501 && ctx->divx_build == 20020416) | |
| 3721 | ✗ | h->padding_bug_score = 256 * 256 * 256 * 64; | |
| 3722 | |||
| 3723 | 5411 | if (ctx->divx_version < 500U) | |
| 3724 | ✗ | h->c.workaround_bugs |= FF_BUG_EDGE; | |
| 3725 | |||
| 3726 | 5411 | if (ctx->divx_version >= 0) | |
| 3727 | ✗ | h->c.workaround_bugs |= FF_BUG_HPEL_CHROMA; | |
| 3728 | } | ||
| 3729 | |||
| 3730 | 5411 | if (h->c.workaround_bugs & FF_BUG_STD_QPEL) { | |
| 3731 | ✗ | SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c) | |
| 3732 | ✗ | SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c) | |
| 3733 | ✗ | SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c) | |
| 3734 | ✗ | SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c) | |
| 3735 | ✗ | SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c) | |
| 3736 | ✗ | SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c) | |
| 3737 | |||
| 3738 | ✗ | SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c) | |
| 3739 | ✗ | SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c) | |
| 3740 | ✗ | SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c) | |
| 3741 | ✗ | SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c) | |
| 3742 | ✗ | SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c) | |
| 3743 | ✗ | SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c) | |
| 3744 | } | ||
| 3745 | |||
| 3746 | 5411 | if (avctx->debug & FF_DEBUG_BUGS) | |
| 3747 | ✗ | av_log(h->c.avctx, AV_LOG_DEBUG, | |
| 3748 | "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n", | ||
| 3749 | h->c.workaround_bugs, ctx->lavc_build, ctx->xvid_build, | ||
| 3750 | ✗ | ctx->divx_version, ctx->divx_build, h->divx_packed ? "p" : ""); | |
| 3751 | |||
| 3752 | 5411 | if (ctx->xvid_build >= 0 && | |
| 3753 | 60 | avctx->idct_algo == FF_IDCT_AUTO && !h->c.studio_profile) { | |
| 3754 | 4 | switch_to_xvid_idct(avctx, h); | |
| 3755 | } | ||
| 3756 | 5411 | } | |
| 3757 | |||
| 3758 | 5411 | static int mpeg4_decode_picture_header(H263DecContext *const h) | |
| 3759 | { | ||
| 3760 | 5411 | Mpeg4DecContext *const ctx = h263_to_mpeg4(h); | |
| 3761 | |||
| 3762 | 5411 | h->skipped_last_frame = 0; | |
| 3763 | |||
| 3764 | 5411 | if (ctx->bitstream_buffer) { | |
| 3765 | 11 | int buf_size = get_bits_left(&h->gb) / 8U; | |
| 3766 | 11 | int bitstream_buffer_size = ctx->bitstream_buffer->size; | |
| 3767 | 11 | const uint8_t *buf = h->gb.buffer; | |
| 3768 | |||
| 3769 | 11 | if (h->divx_packed) { | |
| 3770 | 11 | for (int i = 0; i < buf_size - 3; i++) { | |
| 3771 | 11 | if (buf[i] == 0 && buf[i+1] == 0 && buf[i+2] == 1) { | |
| 3772 | 11 | if (buf[i+3] == 0xB0) { | |
| 3773 | ✗ | av_log(h->c.avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n"); | |
| 3774 | ✗ | bitstream_buffer_size = 0; | |
| 3775 | } | ||
| 3776 | 11 | break; | |
| 3777 | } | ||
| 3778 | } | ||
| 3779 | } | ||
| 3780 | 11 | ctx->bitstream_buffer->size = 0; | |
| 3781 | 18 | if (bitstream_buffer_size && (h->divx_packed || buf_size <= MAX_NVOP_SIZE)) {// divx 5.01+/xvid frame reorder | |
| 3782 | 7 | int ret = init_get_bits8(&h->gb, ctx->bitstream_buffer->data, | |
| 3783 | bitstream_buffer_size); | ||
| 3784 | 7 | if (ret < 0) | |
| 3785 | ✗ | return ret; | |
| 3786 | } else | ||
| 3787 | 4 | av_buffer_unref(&ctx->bitstream_buffer); | |
| 3788 | } | ||
| 3789 | |||
| 3790 | 5411 | return ff_mpeg4_parse_picture_header(ctx, &h->gb, 0, 0); | |
| 3791 | } | ||
| 3792 | |||
| 3793 | 5240 | int ff_mpeg4_frame_end(AVCodecContext *avctx, const AVPacket *pkt) | |
| 3794 | { | ||
| 3795 | 5240 | Mpeg4DecContext *ctx = avctx->priv_data; | |
| 3796 | 5240 | H263DecContext *const h = &ctx->h; | |
| 3797 | int ret; | ||
| 3798 | |||
| 3799 | av_assert1(!ctx->bitstream_buffer || !ctx->bitstream_buffer->size); | ||
| 3800 | |||
| 3801 | /* divx 5.01+ bitstream reorder stuff */ | ||
| 3802 | 5240 | if (h->divx_packed) { | |
| 3803 | 15 | int current_pos = ctx->bitstream_buffer && h->gb.buffer == ctx->bitstream_buffer->data ? 0 : (get_bits_count(&h->gb) >> 3); | |
| 3804 | 15 | int startcode_found = 0; | |
| 3805 | 15 | uint8_t *buf = pkt->data; | |
| 3806 | 15 | int buf_size = pkt->size; | |
| 3807 | |||
| 3808 | 15 | if (buf_size - current_pos > 7) { | |
| 3809 | |||
| 3810 | int i; | ||
| 3811 | 13 | for (i = current_pos; i < buf_size - 4; i++) | |
| 3812 | |||
| 3813 | 13 | if (buf[i] == 0 && | |
| 3814 | 8 | buf[i + 1] == 0 && | |
| 3815 | 8 | buf[i + 2] == 1 && | |
| 3816 | 8 | buf[i + 3] == 0xB6) { | |
| 3817 | 8 | startcode_found = !(buf[i + 4] & 0x40); | |
| 3818 | 8 | break; | |
| 3819 | } | ||
| 3820 | } | ||
| 3821 | |||
| 3822 | 15 | if (startcode_found) { | |
| 3823 | 8 | if (!ctx->showed_packed_warning) { | |
| 3824 | 1 | av_log(h->c.avctx, AV_LOG_INFO, "Video uses a non-standard and " | |
| 3825 | "wasteful way to store B-frames ('packed B-frames'). " | ||
| 3826 | "Consider using the mpeg4_unpack_bframes bitstream filter without encoding but stream copy to fix it.\n"); | ||
| 3827 | 1 | ctx->showed_packed_warning = 1; | |
| 3828 | } | ||
| 3829 | 8 | ret = av_buffer_replace(&ctx->bitstream_buffer, pkt->buf); | |
| 3830 | 8 | if (ret < 0) | |
| 3831 | ✗ | return ret; | |
| 3832 | |||
| 3833 | 8 | ctx->bitstream_buffer->data = buf + current_pos; | |
| 3834 | 8 | ctx->bitstream_buffer->size = buf_size - current_pos; | |
| 3835 | } | ||
| 3836 | } | ||
| 3837 | |||
| 3838 | 5240 | return 0; | |
| 3839 | } | ||
| 3840 | |||
| 3841 | #if HAVE_THREADS | ||
| 3842 | 16 | static av_cold void clear_context(MpegEncContext *s) | |
| 3843 | { | ||
| 3844 | 16 | memset(&s->buffer_pools, 0, sizeof(s->buffer_pools)); | |
| 3845 | 16 | memset(&s->next_pic, 0, sizeof(s->next_pic)); | |
| 3846 | 16 | memset(&s->last_pic, 0, sizeof(s->last_pic)); | |
| 3847 | 16 | memset(&s->cur_pic, 0, sizeof(s->cur_pic)); | |
| 3848 | |||
| 3849 | 16 | memset(s->thread_context, 0, sizeof(s->thread_context)); | |
| 3850 | |||
| 3851 | 16 | s->ac_val_base = NULL; | |
| 3852 | 16 | s->ac_val = NULL; | |
| 3853 | 16 | memset(&s->sc, 0, sizeof(s->sc)); | |
| 3854 | |||
| 3855 | 16 | s->p_field_mv_table_base = NULL; | |
| 3856 | 48 | for (int i = 0; i < 2; i++) | |
| 3857 | 96 | for (int j = 0; j < 2; j++) | |
| 3858 | 64 | s->p_field_mv_table[i][j] = NULL; | |
| 3859 | |||
| 3860 | 16 | s->dc_val_base = NULL; | |
| 3861 | 16 | s->coded_block_base = NULL; | |
| 3862 | 16 | s->mbintra_table = NULL; | |
| 3863 | 16 | s->cbp_table = NULL; | |
| 3864 | 16 | s->pred_dir_table = NULL; | |
| 3865 | |||
| 3866 | 16 | s->mbskip_table = NULL; | |
| 3867 | |||
| 3868 | 16 | s->er.error_status_table = NULL; | |
| 3869 | 16 | s->er.er_temp_buffer = NULL; | |
| 3870 | 16 | s->mb_index2xy = NULL; | |
| 3871 | |||
| 3872 | 16 | s->context_initialized = 0; | |
| 3873 | 16 | s->context_reinit = 0; | |
| 3874 | 16 | } | |
| 3875 | |||
| 3876 | 16 | static av_cold int update_mpvctx(MpegEncContext *s, const MpegEncContext *s1) | |
| 3877 | { | ||
| 3878 | 16 | AVCodecContext *avctx = s->avctx; | |
| 3879 | // FIXME the following leads to a data race; instead copy only | ||
| 3880 | // the necessary fields. | ||
| 3881 | 16 | memcpy(s, s1, sizeof(*s)); | |
| 3882 | 16 | clear_context(s); | |
| 3883 | |||
| 3884 | 16 | s->avctx = avctx; | |
| 3885 | |||
| 3886 | 16 | if (s1->context_initialized) { | |
| 3887 | 16 | int err = ff_mpv_common_init(s); | |
| 3888 | 16 | if (err < 0) | |
| 3889 | ✗ | return err; | |
| 3890 | } | ||
| 3891 | 16 | return 0; | |
| 3892 | } | ||
| 3893 | |||
| 3894 | 1846 | static int mpeg4_update_thread_context(AVCodecContext *dst, | |
| 3895 | const AVCodecContext *src) | ||
| 3896 | { | ||
| 3897 | 1846 | Mpeg4DecContext *s = dst->priv_data; | |
| 3898 | 1846 | const Mpeg4DecContext *s1 = src->priv_data; | |
| 3899 | 1846 | int init = s->h.c.context_initialized; | |
| 3900 | int ret; | ||
| 3901 | |||
| 3902 | 1846 | if (!init) { | |
| 3903 | 16 | ret = update_mpvctx(&s->h.c, &s1->h.c); | |
| 3904 | 16 | if (ret < 0) | |
| 3905 | ✗ | return ret; | |
| 3906 | } | ||
| 3907 | |||
| 3908 | 1846 | ret = ff_mpeg_update_thread_context(dst, src); | |
| 3909 | 1846 | if (ret < 0) | |
| 3910 | ✗ | return ret; | |
| 3911 | |||
| 3912 | // copy all the necessary fields explicitly | ||
| 3913 | 1846 | s->time_increment_bits = s1->time_increment_bits; | |
| 3914 | 1846 | s->shape = s1->shape; | |
| 3915 | 1846 | s->vol_sprite_usage = s1->vol_sprite_usage; | |
| 3916 | 1846 | s->sprite_brightness_change = s1->sprite_brightness_change; | |
| 3917 | 1846 | s->sprite_warping_accuracy = s1->sprite_warping_accuracy; | |
| 3918 | 1846 | s->num_sprite_warping_points = s1->num_sprite_warping_points; | |
| 3919 | 1846 | s->h.data_partitioning = s1->h.data_partitioning; | |
| 3920 | 1846 | s->mpeg_quant = s1->mpeg_quant; | |
| 3921 | 1846 | s->rvlc = s1->rvlc; | |
| 3922 | 1846 | s->resync_marker = s1->resync_marker; | |
| 3923 | 1846 | s->t_frame = s1->t_frame; | |
| 3924 | 1846 | s->new_pred = s1->new_pred; | |
| 3925 | 1846 | s->enhancement_type = s1->enhancement_type; | |
| 3926 | 1846 | s->scalability = s1->scalability; | |
| 3927 | 1846 | s->intra_dc_threshold = s1->intra_dc_threshold; | |
| 3928 | 1846 | s->h.divx_packed = s1->h.divx_packed; | |
| 3929 | 1846 | s->divx_version = s1->divx_version; | |
| 3930 | 1846 | s->divx_build = s1->divx_build; | |
| 3931 | 1846 | s->xvid_build = s1->xvid_build; | |
| 3932 | 1846 | s->lavc_build = s1->lavc_build; | |
| 3933 | 1846 | s->vo_type = s1->vo_type; | |
| 3934 | 1846 | s->showed_packed_warning = s1->showed_packed_warning; | |
| 3935 | 1846 | s->vol_control_parameters = s1->vol_control_parameters; | |
| 3936 | 1846 | s->cplx_estimation_trash_i = s1->cplx_estimation_trash_i; | |
| 3937 | 1846 | s->cplx_estimation_trash_p = s1->cplx_estimation_trash_p; | |
| 3938 | 1846 | s->cplx_estimation_trash_b = s1->cplx_estimation_trash_b; | |
| 3939 | 1846 | s->rgb = s1->rgb; | |
| 3940 | 1846 | s->h.c.studio_profile = s1->h.c.studio_profile; | |
| 3941 | |||
| 3942 | 1846 | s->h.skipped_last_frame = s1->h.skipped_last_frame; | |
| 3943 | 1846 | s->h.padding_bug_score = s1->h.padding_bug_score; // FIXME: racy | |
| 3944 | |||
| 3945 | 1846 | s->h.picture_number = s1->h.picture_number; | |
| 3946 | |||
| 3947 | 1846 | memcpy(s->sprite_shift, s1->sprite_shift, sizeof(s1->sprite_shift)); | |
| 3948 | 1846 | memcpy(s->sprite_traj, s1->sprite_traj, sizeof(s1->sprite_traj)); | |
| 3949 | |||
| 3950 | 1846 | return av_buffer_replace(&s->bitstream_buffer, s1->bitstream_buffer); | |
| 3951 | } | ||
| 3952 | |||
| 3953 | 1834 | static int mpeg4_update_thread_context_for_user(AVCodecContext *dst, | |
| 3954 | const AVCodecContext *src) | ||
| 3955 | { | ||
| 3956 | 1834 | H263DecContext *const h = dst->priv_data; | |
| 3957 | 1834 | const H263DecContext *const h1 = src->priv_data; | |
| 3958 | |||
| 3959 | 1834 | h->c.quarter_sample = h1->c.quarter_sample; | |
| 3960 | 1834 | h->divx_packed = h1->divx_packed; | |
| 3961 | |||
| 3962 | 1834 | return 0; | |
| 3963 | } | ||
| 3964 | #endif | ||
| 3965 | |||
| 3966 | 124 | static av_cold void mpeg4_init_static(void) | |
| 3967 | { | ||
| 3968 | static VLCElem vlc_buf[6498]; | ||
| 3969 | 124 | VLCInitState state = VLC_INIT_STATE(vlc_buf); | |
| 3970 | |||
| 3971 | 124 | VLC_INIT_STATIC_TABLE_FROM_LENGTHS(studio_luma_dc, STUDIO_INTRA_BITS, 19, | |
| 3972 | &ff_mpeg4_studio_dc_luma[0][1], 2, | ||
| 3973 | &ff_mpeg4_studio_dc_luma[0][0], 2, 1, | ||
| 3974 | 0, 0); | ||
| 3975 | |||
| 3976 | 124 | VLC_INIT_STATIC_TABLE_FROM_LENGTHS(studio_chroma_dc, STUDIO_INTRA_BITS, 19, | |
| 3977 | &ff_mpeg4_studio_dc_chroma[0][1], 2, | ||
| 3978 | &ff_mpeg4_studio_dc_chroma[0][0], 2, 1, | ||
| 3979 | 0, 0); | ||
| 3980 | |||
| 3981 | 1612 | for (unsigned i = 0; i < 12; i++) { | |
| 3982 | 1488 | studio_intra_tab[i] = | |
| 3983 | 1488 | ff_vlc_init_tables_from_lengths(&state, STUDIO_INTRA_BITS, 24, | |
| 3984 | 1488 | &ff_mpeg4_studio_intra[i][0][1], 2, | |
| 3985 | 1488 | &ff_mpeg4_studio_intra[i][0][0], 2, 1, | |
| 3986 | 0, 0); | ||
| 3987 | } | ||
| 3988 | |||
| 3989 | static uint8_t mpeg4_rl_intra_table[2][2 * MAX_RUN + MAX_LEVEL + 3]; | ||
| 3990 | 124 | ff_rl_init(&ff_mpeg4_rl_intra, mpeg4_rl_intra_table); | |
| 3991 | 124 | ff_h263_init_rl_inter(); | |
| 3992 | |||
| 3993 | 124 | INIT_FIRST_VLC_RL(ff_mpeg4_rl_intra, 554); | |
| 3994 | 4092 | VLC_INIT_RL(ff_rvlc_rl_inter, 1072); | |
| 3995 | 124 | INIT_FIRST_VLC_RL(ff_rvlc_rl_intra, 1072); | |
| 3996 | 124 | VLC_INIT_STATIC_TABLE(dc_lum, DC_VLC_BITS, 10 /* 13 */, | |
| 3997 | &ff_mpeg4_DCtab_lum[0][1], 2, 1, | ||
| 3998 | &ff_mpeg4_DCtab_lum[0][0], 2, 1, 0); | ||
| 3999 | 124 | VLC_INIT_STATIC_TABLE(dc_chrom, DC_VLC_BITS, 10 /* 13 */, | |
| 4000 | &ff_mpeg4_DCtab_chrom[0][1], 2, 1, | ||
| 4001 | &ff_mpeg4_DCtab_chrom[0][0], 2, 1, 0); | ||
| 4002 | 124 | VLC_INIT_STATIC_TABLE_FROM_LENGTHS(sprite_trajectory, SPRITE_TRAJ_VLC_BITS, 15, | |
| 4003 | ff_sprite_trajectory_lens, 1, | ||
| 4004 | NULL, 0, 0, 0, 0); | ||
| 4005 | 124 | VLC_INIT_STATIC_SPARSE_TABLE(mb_type_b_vlc, MB_TYPE_B_VLC_BITS, 4, | |
| 4006 | &ff_mb_type_b_tab[0][1], 2, 1, | ||
| 4007 | &ff_mb_type_b_tab[0][0], 2, 1, | ||
| 4008 | mb_type_b_map, 2, 2, 0); | ||
| 4009 | 124 | } | |
| 4010 | |||
| 4011 | 231 | static av_cold int decode_init(AVCodecContext *avctx) | |
| 4012 | { | ||
| 4013 | static AVOnce init_static_once = AV_ONCE_INIT; | ||
| 4014 | 231 | Mpeg4DecContext *ctx = avctx->priv_data; | |
| 4015 | 231 | H263DecContext *const h = &ctx->h; | |
| 4016 | MPVUnquantDSPContext unquant_dsp_ctx; | ||
| 4017 | int ret; | ||
| 4018 | |||
| 4019 | 231 | ctx->divx_version = | |
| 4020 | 231 | ctx->divx_build = | |
| 4021 | 231 | ctx->xvid_build = | |
| 4022 | 231 | ctx->lavc_build = -1; | |
| 4023 | |||
| 4024 | 231 | if ((ret = ff_h263_decode_init(avctx)) < 0) | |
| 4025 | ✗ | return ret; | |
| 4026 | |||
| 4027 | 231 | ff_mpv_unquantize_init(&unquant_dsp_ctx, | |
| 4028 | avctx->flags & AV_CODEC_FLAG_BITEXACT, 0); | ||
| 4029 | |||
| 4030 | 231 | ctx->dct_unquantize_h263_intra = unquant_dsp_ctx.dct_unquantize_h263_intra; | |
| 4031 | 231 | ctx->dct_unquantize_mpeg2_intra = unquant_dsp_ctx.dct_unquantize_mpeg2_intra; | |
| 4032 | // dct_unquantize_inter is only used with MPEG-2 quantizers, | ||
| 4033 | // so that is all we keep. | ||
| 4034 | 231 | ctx->dct_unquantize_mpeg2_inter = unquant_dsp_ctx.dct_unquantize_mpeg2_inter; | |
| 4035 | |||
| 4036 | 231 | h->c.y_dc_scale_table = ff_mpeg4_y_dc_scale_table; | |
| 4037 | 231 | h->c.c_dc_scale_table = ff_mpeg4_c_dc_scale_table; | |
| 4038 | |||
| 4039 | 231 | h->c.h263_pred = 1; | |
| 4040 | 231 | h->c.low_delay = 0; /* default, might be overridden in the vol header during header parsing */ | |
| 4041 | 231 | h->decode_header = mpeg4_decode_picture_header; | |
| 4042 | 231 | h->decode_mb = mpeg4_decode_mb; | |
| 4043 | 231 | ctx->time_increment_bits = 4; /* default value for broken headers */ | |
| 4044 | 231 | ctx->quant_precision = 5; | |
| 4045 | |||
| 4046 | 231 | avctx->chroma_sample_location = AVCHROMA_LOC_LEFT; | |
| 4047 | |||
| 4048 | 231 | ff_qpeldsp_init(&h->c.qdsp); | |
| 4049 | 231 | ff_mpeg4videodsp_init(&ctx->mdsp); | |
| 4050 | |||
| 4051 | 231 | ff_thread_once(&init_static_once, mpeg4_init_static); | |
| 4052 | |||
| 4053 | /* Must be after initializing the MPEG-4 static tables */ | ||
| 4054 | 231 | if (avctx->extradata_size && !avctx->internal->is_copy) { | |
| 4055 | GetBitContext gb; | ||
| 4056 | |||
| 4057 | 145 | if (init_get_bits8(&gb, avctx->extradata, avctx->extradata_size) >= 0) | |
| 4058 | 145 | ff_mpeg4_parse_picture_header(ctx, &gb, 1, 0); | |
| 4059 | } | ||
| 4060 | |||
| 4061 | 231 | return 0; | |
| 4062 | } | ||
| 4063 | |||
| 4064 | ✗ | static av_cold void mpeg4_flush(AVCodecContext *avctx) | |
| 4065 | { | ||
| 4066 | ✗ | Mpeg4DecContext *const ctx = avctx->priv_data; | |
| 4067 | |||
| 4068 | ✗ | av_buffer_unref(&ctx->bitstream_buffer); | |
| 4069 | ✗ | ff_mpeg_flush(avctx); | |
| 4070 | ✗ | } | |
| 4071 | |||
| 4072 | 231 | static av_cold int mpeg4_close(AVCodecContext *avctx) | |
| 4073 | { | ||
| 4074 | 231 | Mpeg4DecContext *const ctx = avctx->priv_data; | |
| 4075 | |||
| 4076 | 231 | av_buffer_unref(&ctx->bitstream_buffer); | |
| 4077 | |||
| 4078 | 231 | return ff_mpv_decode_close(avctx); | |
| 4079 | } | ||
| 4080 | |||
| 4081 | #define OFFSET(x) offsetof(H263DecContext, x) | ||
| 4082 | #define FLAGS AV_OPT_FLAG_EXPORT | AV_OPT_FLAG_READONLY | ||
| 4083 | static const AVOption mpeg4_options[] = { | ||
| 4084 | {"quarter_sample", "1/4 subpel MC", OFFSET(c.quarter_sample), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS}, | ||
| 4085 | {"divx_packed", "divx style packed b frames", OFFSET(divx_packed), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS}, | ||
| 4086 | {NULL} | ||
| 4087 | }; | ||
| 4088 | |||
| 4089 | static const AVClass mpeg4_class = { | ||
| 4090 | .class_name = "MPEG4 Video Decoder", | ||
| 4091 | .item_name = av_default_item_name, | ||
| 4092 | .option = mpeg4_options, | ||
| 4093 | .version = LIBAVUTIL_VERSION_INT, | ||
| 4094 | }; | ||
| 4095 | |||
| 4096 | const FFCodec ff_mpeg4_decoder = { | ||
| 4097 | .p.name = "mpeg4", | ||
| 4098 | CODEC_LONG_NAME("MPEG-4 part 2"), | ||
| 4099 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 4100 | .p.id = AV_CODEC_ID_MPEG4, | ||
| 4101 | .priv_data_size = sizeof(Mpeg4DecContext), | ||
| 4102 | .init = decode_init, | ||
| 4103 | FF_CODEC_DECODE_CB(ff_h263_decode_frame), | ||
| 4104 | .close = mpeg4_close, | ||
| 4105 | .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 | | ||
| 4106 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_FRAME_THREADS, | ||
| 4107 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | | ||
| 4108 | FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, | ||
| 4109 | .flush = mpeg4_flush, | ||
| 4110 | .p.max_lowres = 3, | ||
| 4111 | .p.profiles = NULL_IF_CONFIG_SMALL(ff_mpeg4_video_profiles), | ||
| 4112 | UPDATE_THREAD_CONTEXT(mpeg4_update_thread_context), | ||
| 4113 | UPDATE_THREAD_CONTEXT_FOR_USER(mpeg4_update_thread_context_for_user), | ||
| 4114 | .p.priv_class = &mpeg4_class, | ||
| 4115 | .hw_configs = (const AVCodecHWConfigInternal *const []) { | ||
| 4116 | #if CONFIG_MPEG4_NVDEC_HWACCEL | ||
| 4117 | HWACCEL_NVDEC(mpeg4), | ||
| 4118 | #endif | ||
| 4119 | #if CONFIG_MPEG4_NVDEC_CUARRAY_HWACCEL | ||
| 4120 | HWACCEL_NVDEC_CUARRAY(mpeg4), | ||
| 4121 | #endif | ||
| 4122 | #if CONFIG_MPEG4_VAAPI_HWACCEL | ||
| 4123 | HWACCEL_VAAPI(mpeg4), | ||
| 4124 | #endif | ||
| 4125 | #if CONFIG_MPEG4_VDPAU_HWACCEL | ||
| 4126 | HWACCEL_VDPAU(mpeg4), | ||
| 4127 | #endif | ||
| 4128 | #if CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL | ||
| 4129 | HWACCEL_VIDEOTOOLBOX(mpeg4), | ||
| 4130 | #endif | ||
| 4131 | NULL | ||
| 4132 | }, | ||
| 4133 | }; | ||
| 4134 | #endif /* CONFIG_MPEG4_DECODER */ | ||
| 4135 |