| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * H.261 decoder | ||
| 3 | * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at> | ||
| 4 | * Copyright (c) 2004 Maarten Daniels | ||
| 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 | /** | ||
| 24 | * @file | ||
| 25 | * H.261 decoder. | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include "libavutil/mem_internal.h" | ||
| 29 | #include "libavutil/thread.h" | ||
| 30 | #include "avcodec.h" | ||
| 31 | #include "codec_internal.h" | ||
| 32 | #include "decode.h" | ||
| 33 | #include "get_bits.h" | ||
| 34 | #include "mpeg_er.h" | ||
| 35 | #include "mpegutils.h" | ||
| 36 | #include "mpegvideo.h" | ||
| 37 | #include "mpegvideodec.h" | ||
| 38 | #include "h261.h" | ||
| 39 | |||
| 40 | #define SLICE_OK 0 | ||
| 41 | #define SLICE_ERROR -1 | ||
| 42 | #define SLICE_END -2 ///<end marker found | ||
| 43 | |||
| 44 | #define H261_MBA_VLC_BITS 8 | ||
| 45 | #define H261_MTYPE_VLC_BITS 6 | ||
| 46 | #define H261_MV_VLC_BITS 7 | ||
| 47 | #define H261_CBP_VLC_BITS 9 | ||
| 48 | #define TCOEFF_VLC_BITS 9 | ||
| 49 | #define MBA_STUFFING 33 | ||
| 50 | #define MBA_STARTCODE 34 | ||
| 51 | |||
| 52 | static VLCElem h261_mba_vlc[540]; | ||
| 53 | static VLCElem h261_mtype_vlc[80]; | ||
| 54 | static VLCElem h261_mv_vlc[144]; | ||
| 55 | static VLCElem h261_cbp_vlc[512]; | ||
| 56 | |||
| 57 | typedef struct H261DecContext { | ||
| 58 | MpegEncContext s; | ||
| 59 | |||
| 60 | GetBitContext gb; | ||
| 61 | |||
| 62 | int current_mba; | ||
| 63 | int mba_diff; | ||
| 64 | int current_mv_x; | ||
| 65 | int current_mv_y; | ||
| 66 | int gob_number; | ||
| 67 | int gob_start_code_skipped; // 1 if gob start code is already read before gob header is read | ||
| 68 | |||
| 69 | DECLARE_ALIGNED_32(int16_t, block)[6][64]; | ||
| 70 | } H261DecContext; | ||
| 71 | |||
| 72 | 7 | static av_cold void h261_decode_init_static(void) | |
| 73 | { | ||
| 74 | 7 | VLC_INIT_STATIC_TABLE(h261_mba_vlc, H261_MBA_VLC_BITS, 35, | |
| 75 | ff_h261_mba_bits, 1, 1, | ||
| 76 | ff_h261_mba_code, 1, 1, 0); | ||
| 77 | 7 | VLC_INIT_STATIC_SPARSE_TABLE(h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10, | |
| 78 | ff_h261_mtype_bits, 1, 1, | ||
| 79 | ff_h261_mtype_code, 1, 1, | ||
| 80 | ff_h261_mtype_map, 2, 2, 0); | ||
| 81 | 7 | VLC_INIT_STATIC_TABLE(h261_mv_vlc, H261_MV_VLC_BITS, 17, | |
| 82 | &ff_h261_mv_tab[0][1], 2, 1, | ||
| 83 | &ff_h261_mv_tab[0][0], 2, 1, 0); | ||
| 84 | 7 | VLC_INIT_STATIC_TABLE(h261_cbp_vlc, H261_CBP_VLC_BITS, 63, | |
| 85 | &ff_h261_cbp_tab[0][1], 2, 1, | ||
| 86 | &ff_h261_cbp_tab[0][0], 2, 1, 0); | ||
| 87 | 7 | INIT_FIRST_VLC_RL(ff_h261_rl_tcoeff, 552); | |
| 88 | 7 | } | |
| 89 | |||
| 90 | 13 | static av_cold int h261_decode_init(AVCodecContext *avctx) | |
| 91 | { | ||
| 92 | static AVOnce init_static_once = AV_ONCE_INIT; | ||
| 93 | 13 | H261DecContext *const h = avctx->priv_data; | |
| 94 | 13 | MpegEncContext *const s = &h->s; | |
| 95 | int ret; | ||
| 96 | |||
| 97 | 13 | avctx->framerate = (AVRational) { 30000, 1001 }; | |
| 98 | |||
| 99 | /* The H.261 analog of intra/key frames is setting the freeze picture release flag, | ||
| 100 | * but this does not guarantee that the frame uses intra-only encoding, | ||
| 101 | * so we still need to allocate dummy frames. So set pict_type to P here | ||
| 102 | * for all frames and override it after having decoded the frame. */ | ||
| 103 | 13 | s->pict_type = AV_PICTURE_TYPE_P; | |
| 104 | |||
| 105 | // set defaults | ||
| 106 | 13 | ret = ff_mpv_decode_init(s, avctx); | |
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (ret < 0) |
| 108 | ✗ | return ret; | |
| 109 | |||
| 110 | 13 | s->out_format = FMT_H261; | |
| 111 | 13 | s->low_delay = 1; | |
| 112 | 13 | avctx->pix_fmt = AV_PIX_FMT_YUV420P; | |
| 113 | |||
| 114 | 13 | ff_thread_once(&init_static_once, h261_decode_init_static); | |
| 115 | |||
| 116 | 13 | return 0; | |
| 117 | } | ||
| 118 | |||
| 119 | 118800 | static inline void h261_init_dest(MpegEncContext *s) | |
| 120 | { | ||
| 121 | 118800 | const unsigned block_size = 8 >> s->avctx->lowres; | |
| 122 | 118800 | ff_init_block_index(s); | |
| 123 | 118800 | s->dest[0] += 2 * block_size; | |
| 124 | 118800 | s->dest[1] += block_size; | |
| 125 | 118800 | s->dest[2] += block_size; | |
| 126 | 118800 | } | |
| 127 | |||
| 128 | /** | ||
| 129 | * Decode the group of blocks header or slice header. | ||
| 130 | * @return <0 if an error occurred | ||
| 131 | */ | ||
| 132 | 3600 | static int h261_decode_gob_header(H261DecContext *h) | |
| 133 | { | ||
| 134 | unsigned int val; | ||
| 135 | 3600 | MpegEncContext *const s = &h->s; | |
| 136 | |||
| 137 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 3300 times.
|
3600 | if (!h->gob_start_code_skipped) { |
| 138 | /* Check for GOB Start Code */ | ||
| 139 | 300 | val = show_bits(&h->gb, 15); | |
| 140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 300 times.
|
300 | if (val) |
| 141 | ✗ | return -1; | |
| 142 | |||
| 143 | /* We have a GBSC */ | ||
| 144 | 300 | skip_bits(&h->gb, 16); | |
| 145 | } | ||
| 146 | |||
| 147 | 3600 | h->gob_start_code_skipped = 0; | |
| 148 | |||
| 149 | 3600 | h->gob_number = get_bits(&h->gb, 4); /* GN */ | |
| 150 | 3600 | s->qscale = get_bits(&h->gb, 5); /* GQUANT */ | |
| 151 | |||
| 152 | /* Check if gob_number is valid */ | ||
| 153 |
1/2✓ Branch 0 taken 3600 times.
✗ Branch 1 not taken.
|
3600 | if (s->mb_height == 18) { // CIF |
| 154 |
2/4✓ Branch 0 taken 3600 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3600 times.
|
3600 | if ((h->gob_number <= 0) || (h->gob_number > 12)) |
| 155 | ✗ | return -1; | |
| 156 | } else { // QCIF | ||
| 157 | ✗ | if ((h->gob_number != 1) && (h->gob_number != 3) && | |
| 158 | ✗ | (h->gob_number != 5)) | |
| 159 | ✗ | return -1; | |
| 160 | } | ||
| 161 | |||
| 162 | /* GEI */ | ||
| 163 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3600 times.
|
3600 | if (skip_1stop_8data_bits(&h->gb) < 0) |
| 164 | ✗ | return AVERROR_INVALIDDATA; | |
| 165 | |||
| 166 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3600 times.
|
3600 | if (s->qscale == 0) { |
| 167 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n"); | |
| 168 | ✗ | if (s->avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT)) | |
| 169 | ✗ | return -1; | |
| 170 | ✗ | s->qscale = 1; | |
| 171 | } | ||
| 172 | |||
| 173 | /* For the first transmitted macroblock in a GOB, MBA is the absolute | ||
| 174 | * address. For subsequent macroblocks, MBA is the difference between | ||
| 175 | * the absolute addresses of the macroblock and the last transmitted | ||
| 176 | * macroblock. */ | ||
| 177 | 3600 | h->current_mba = 0; | |
| 178 | 3600 | h->mba_diff = 0; | |
| 179 | |||
| 180 | 3600 | return 0; | |
| 181 | } | ||
| 182 | |||
| 183 | /** | ||
| 184 | * Decode skipped macroblocks. | ||
| 185 | * @return 0 | ||
| 186 | */ | ||
| 187 | 121541 | static int h261_decode_mb_skipped(H261DecContext *h, int mba1, int mba2) | |
| 188 | { | ||
| 189 | 121541 | MpegEncContext *const s = &h->s; | |
| 190 | int i; | ||
| 191 | |||
| 192 | 121541 | s->mb_intra = 0; | |
| 193 | |||
| 194 |
2/2✓ Branch 0 taken 859 times.
✓ Branch 1 taken 121541 times.
|
122400 | for (i = mba1; i < mba2; i++) { |
| 195 | int j, xy; | ||
| 196 | |||
| 197 | 859 | s->mb_x = ((h->gob_number - 1) % 2) * 11 + i % 11; | |
| 198 | 859 | s->mb_y = ((h->gob_number - 1) / 2) * 3 + i / 11; | |
| 199 | 859 | xy = s->mb_x + s->mb_y * s->mb_stride; | |
| 200 | 859 | h261_init_dest(s); | |
| 201 | |||
| 202 |
2/2✓ Branch 0 taken 5154 times.
✓ Branch 1 taken 859 times.
|
6013 | for (j = 0; j < 6; j++) |
| 203 | 5154 | s->block_last_index[j] = -1; | |
| 204 | |||
| 205 | 859 | s->mv_dir = MV_DIR_FORWARD; | |
| 206 | 859 | s->mv_type = MV_TYPE_16X16; | |
| 207 | 859 | s->cur_pic.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_FORWARD_MV; | |
| 208 | 859 | s->mv[0][0][0] = 0; | |
| 209 | 859 | s->mv[0][0][1] = 0; | |
| 210 | 859 | s->mb_skipped = 1; | |
| 211 | 859 | s->mtype &= ~MB_TYPE_H261_FIL; | |
| 212 | |||
| 213 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 859 times.
|
859 | if (s->cur_pic.motion_val[0]) { |
| 214 | ✗ | int b_stride = 2*s->mb_width + 1; | |
| 215 | ✗ | int b_xy = 2 * s->mb_x + (2 * s->mb_y) * b_stride; | |
| 216 | ✗ | s->cur_pic.motion_val[0][b_xy][0] = s->mv[0][0][0]; | |
| 217 | ✗ | s->cur_pic.motion_val[0][b_xy][1] = s->mv[0][0][1]; | |
| 218 | } | ||
| 219 | |||
| 220 | 859 | ff_mpv_reconstruct_mb(s, h->block); | |
| 221 | } | ||
| 222 | |||
| 223 | 121541 | return 0; | |
| 224 | } | ||
| 225 | |||
| 226 | 198948 | static int decode_mv_component(GetBitContext *gb, int v) | |
| 227 | { | ||
| 228 | 198948 | int mv_diff = get_vlc2(gb, h261_mv_vlc, H261_MV_VLC_BITS, 2); | |
| 229 | |||
| 230 | /* check if mv_diff is valid */ | ||
| 231 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 198948 times.
|
198948 | if (mv_diff < 0) |
| 232 | ✗ | return v; | |
| 233 | |||
| 234 |
4/4✓ Branch 0 taken 83960 times.
✓ Branch 1 taken 114988 times.
✓ Branch 3 taken 45885 times.
✓ Branch 4 taken 38075 times.
|
198948 | if (mv_diff && get_bits1(gb)) |
| 235 | 45885 | mv_diff = -mv_diff; | |
| 236 | |||
| 237 | 198948 | v += mv_diff; | |
| 238 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 198916 times.
|
198948 | if (v <= -16) |
| 239 | 32 | v += 32; | |
| 240 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 198881 times.
|
198916 | else if (v >= 16) |
| 241 | 35 | v -= 32; | |
| 242 | |||
| 243 | 198948 | return v; | |
| 244 | } | ||
| 245 | |||
| 246 | /** | ||
| 247 | * Decode a macroblock. | ||
| 248 | * @return <0 if an error occurred | ||
| 249 | */ | ||
| 250 | 591000 | static int h261_decode_block(H261DecContext *h, int16_t *block, int n, int coded) | |
| 251 | { | ||
| 252 | 591000 | MpegEncContext *const s = &h->s; | |
| 253 | int level, i, j, run; | ||
| 254 | 591000 | const RLTable *rl = &ff_h261_rl_tcoeff; | |
| 255 | const uint8_t *scan_table; | ||
| 256 | 591000 | const int qmul = s->qscale << 1, qadd = (s->qscale - 1) | 1; | |
| 257 | |||
| 258 | /* For the variable length encoding there are two code tables, one being | ||
| 259 | * used for the first transmitted LEVEL in INTER, INTER + MC and | ||
| 260 | * INTER + MC + FIL blocks, the second for all other LEVELs except the | ||
| 261 | * first one in INTRA blocks which is fixed length coded with 8 bits. | ||
| 262 | * NOTE: The two code tables only differ in one VLC so we handle that | ||
| 263 | * manually. */ | ||
| 264 | 591000 | scan_table = s->intra_scantable.permutated; | |
| 265 |
2/2✓ Branch 0 taken 101472 times.
✓ Branch 1 taken 489528 times.
|
591000 | if (s->mb_intra) { |
| 266 | /* DC coef */ | ||
| 267 | 101472 | level = get_bits(&h->gb, 8); | |
| 268 | // 0 (00000000b) and -128 (10000000b) are FORBIDDEN | ||
| 269 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 101472 times.
|
101472 | if ((level & 0x7F) == 0) { |
| 270 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", | |
| 271 | level, s->mb_x, s->mb_y); | ||
| 272 | ✗ | return -1; | |
| 273 | } | ||
| 274 | /* The code 1000 0000 is not used, the reconstruction level of 1024 | ||
| 275 | * being coded as 1111 1111. */ | ||
| 276 |
2/2✓ Branch 0 taken 1145 times.
✓ Branch 1 taken 100327 times.
|
101472 | if (level == 255) |
| 277 | 1145 | level = 128; | |
| 278 | 101472 | block[0] = level * 8; | |
| 279 | 101472 | i = 1; | |
| 280 |
2/2✓ Branch 0 taken 259485 times.
✓ Branch 1 taken 230043 times.
|
489528 | } else if (coded) { |
| 281 | // Run Level Code | ||
| 282 | // EOB Not possible for first level when cbp is available (that's why the table is different) | ||
| 283 | // 0 1 1s | ||
| 284 | // * * 0* | ||
| 285 | 259485 | int check = show_bits(&h->gb, 2); | |
| 286 | 259485 | i = 0; | |
| 287 |
2/2✓ Branch 0 taken 87589 times.
✓ Branch 1 taken 171896 times.
|
259485 | if (check & 0x2) { |
| 288 | 87589 | skip_bits(&h->gb, 2); | |
| 289 | 87589 | block[0] = qmul + qadd; | |
| 290 |
2/2✓ Branch 0 taken 45233 times.
✓ Branch 1 taken 42356 times.
|
87589 | block[0] *= (check & 0x1) ? -1 : 1; |
| 291 | 87589 | i = 1; | |
| 292 | } | ||
| 293 | } else { | ||
| 294 | 230043 | i = 0; | |
| 295 | } | ||
| 296 |
2/2✓ Branch 0 taken 230043 times.
✓ Branch 1 taken 360957 times.
|
591000 | if (!coded) { |
| 297 | 230043 | s->block_last_index[n] = i - 1; | |
| 298 | 230043 | return 0; | |
| 299 | } | ||
| 300 | { | ||
| 301 | 360957 | OPEN_READER(re, &h->gb); | |
| 302 | 360957 | i--; // offset by -1 to allow direct indexing of scan_table | |
| 303 | for (;;) { | ||
| 304 | 5175539 | UPDATE_CACHE(re, &h->gb); | |
| 305 |
2/2✓ Branch 1 taken 178749 times.
✓ Branch 2 taken 2589499 times.
|
2768248 | GET_RL_VLC(level, run, re, &h->gb, rl->rl_vlc[0], TCOEFF_VLC_BITS, 2, 0); |
| 306 |
2/2✓ Branch 0 taken 40537 times.
✓ Branch 1 taken 2727711 times.
|
2768248 | if (run == 66) { |
| 307 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40537 times.
|
40537 | if (level) { |
| 308 | ✗ | CLOSE_READER(re, &h->gb); | |
| 309 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", | |
| 310 | s->mb_x, s->mb_y); | ||
| 311 | ✗ | return -1; | |
| 312 | } | ||
| 313 | /* escape */ | ||
| 314 | /* The remaining combinations of (run, level) are encoded with a | ||
| 315 | * 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits | ||
| 316 | * level. */ | ||
| 317 | 40537 | run = SHOW_UBITS(re, &h->gb, 6) + 1; | |
| 318 | 40537 | SKIP_CACHE(re, &h->gb, 6); | |
| 319 | 40537 | level = SHOW_SBITS(re, &h->gb, 8); | |
| 320 |
2/2✓ Branch 0 taken 21553 times.
✓ Branch 1 taken 18984 times.
|
40537 | if (level > 0) |
| 321 | 21553 | level = level * qmul + qadd; | |
| 322 |
1/2✓ Branch 0 taken 18984 times.
✗ Branch 1 not taken.
|
18984 | else if (level < 0) |
| 323 | 18984 | level = level * qmul - qadd; | |
| 324 | 40537 | SKIP_COUNTER(re, &h->gb, 6 + 8); | |
| 325 |
2/2✓ Branch 0 taken 360957 times.
✓ Branch 1 taken 2366754 times.
|
2727711 | } else if (level == 0) { |
| 326 | 360957 | break; | |
| 327 | } else { | ||
| 328 | 2366754 | level = level * qmul + qadd; | |
| 329 |
2/2✓ Branch 1 taken 1184977 times.
✓ Branch 2 taken 1181777 times.
|
2366754 | if (SHOW_UBITS(re, &h->gb, 1)) |
| 330 | 1184977 | level = -level; | |
| 331 | 2366754 | SKIP_COUNTER(re, &h->gb, 1); | |
| 332 | } | ||
| 333 | 2407291 | i += run; | |
| 334 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2407291 times.
|
2407291 | if (i >= 64) { |
| 335 | ✗ | CLOSE_READER(re, &h->gb); | |
| 336 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", | |
| 337 | s->mb_x, s->mb_y); | ||
| 338 | ✗ | return -1; | |
| 339 | } | ||
| 340 | 2407291 | j = scan_table[i]; | |
| 341 | 2407291 | block[j] = level; | |
| 342 | } | ||
| 343 | 360957 | CLOSE_READER(re, &h->gb); | |
| 344 | } | ||
| 345 | 360957 | s->block_last_index[n] = i; | |
| 346 | 360957 | return 0; | |
| 347 | } | ||
| 348 | |||
| 349 | 121541 | static int h261_decode_mb(H261DecContext *h) | |
| 350 | { | ||
| 351 | 121541 | MpegEncContext *const s = &h->s; | |
| 352 | int i, cbp, xy; | ||
| 353 | |||
| 354 | 121541 | cbp = 63; | |
| 355 | // Read mba | ||
| 356 | do { | ||
| 357 | 121541 | h->mba_diff = get_vlc2(&h->gb, h261_mba_vlc, | |
| 358 | H261_MBA_VLC_BITS, 2); | ||
| 359 | |||
| 360 | /* Check for slice end */ | ||
| 361 | /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */ | ||
| 362 |
2/2✓ Branch 0 taken 3300 times.
✓ Branch 1 taken 118241 times.
|
121541 | if (h->mba_diff == MBA_STARTCODE) { // start code |
| 363 | 3300 | h->gob_start_code_skipped = 1; | |
| 364 | 3300 | return SLICE_END; | |
| 365 | } | ||
| 366 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 118241 times.
|
118241 | } while (h->mba_diff == MBA_STUFFING); // stuffing |
| 367 | |||
| 368 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 117941 times.
|
118241 | if (h->mba_diff < 0) { |
| 369 |
1/2✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
|
300 | if (get_bits_left(&h->gb) <= 7) |
| 370 | 300 | return SLICE_END; | |
| 371 | |||
| 372 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y); | |
| 373 | ✗ | return SLICE_ERROR; | |
| 374 | } | ||
| 375 | |||
| 376 | 117941 | h->mba_diff += 1; | |
| 377 | 117941 | h->current_mba += h->mba_diff; | |
| 378 | |||
| 379 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 117941 times.
|
117941 | if (h->current_mba > MBA_STUFFING) |
| 380 | ✗ | return SLICE_ERROR; | |
| 381 | |||
| 382 | 117941 | s->mb_x = ((h->gob_number - 1) % 2) * 11 + ((h->current_mba - 1) % 11); | |
| 383 | 117941 | s->mb_y = ((h->gob_number - 1) / 2) * 3 + ((h->current_mba - 1) / 11); | |
| 384 | 117941 | xy = s->mb_x + s->mb_y * s->mb_stride; | |
| 385 | 117941 | h261_init_dest(s); | |
| 386 | |||
| 387 | // Read mtype | ||
| 388 | 117941 | s->mtype = get_vlc2(&h->gb, h261_mtype_vlc, H261_MTYPE_VLC_BITS, 2); | |
| 389 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 117941 times.
|
117941 | if (s->mtype < 0) { |
| 390 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid mtype index\n"); | |
| 391 | ✗ | return SLICE_ERROR; | |
| 392 | } | ||
| 393 | |||
| 394 | // Read mquant | ||
| 395 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 117941 times.
|
117941 | if (IS_QUANT(s->mtype)) { |
| 396 | ✗ | s->qscale = get_bits(&h->gb, 5); | |
| 397 | ✗ | if (!s->qscale) | |
| 398 | ✗ | s->qscale = 1; | |
| 399 | } | ||
| 400 | |||
| 401 | 117941 | s->mb_intra = IS_INTRA4x4(s->mtype); | |
| 402 | |||
| 403 | // Read mv | ||
| 404 |
2/2✓ Branch 0 taken 99474 times.
✓ Branch 1 taken 18467 times.
|
117941 | if (IS_16X16(s->mtype)) { |
| 405 | /* Motion vector data is included for all MC macroblocks. MVD is | ||
| 406 | * obtained from the macroblock vector by subtracting the vector | ||
| 407 | * of the preceding macroblock. For this calculation the vector | ||
| 408 | * of the preceding macroblock is regarded as zero in the | ||
| 409 | * following three situations: | ||
| 410 | * 1) evaluating MVD for macroblocks 1, 12 and 23; | ||
| 411 | * 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1; | ||
| 412 | * 3) MTYPE of the previous macroblock was not MC. */ | ||
| 413 |
4/4✓ Branch 0 taken 96664 times.
✓ Branch 1 taken 2810 times.
✓ Branch 2 taken 93760 times.
✓ Branch 3 taken 2904 times.
|
99474 | if ((h->current_mba == 1) || (h->current_mba == 12) || |
| 414 |
4/4✓ Branch 0 taken 90931 times.
✓ Branch 1 taken 2829 times.
✓ Branch 2 taken 632 times.
✓ Branch 3 taken 90299 times.
|
93760 | (h->current_mba == 23) || (h->mba_diff != 1)) { |
| 415 | 9175 | h->current_mv_x = 0; | |
| 416 | 9175 | h->current_mv_y = 0; | |
| 417 | } | ||
| 418 | |||
| 419 | 99474 | h->current_mv_x = decode_mv_component(&h->gb, h->current_mv_x); | |
| 420 | 99474 | h->current_mv_y = decode_mv_component(&h->gb, h->current_mv_y); | |
| 421 | } else { | ||
| 422 | 18467 | h->current_mv_x = 0; | |
| 423 | 18467 | h->current_mv_y = 0; | |
| 424 | } | ||
| 425 | |||
| 426 | // Read cbp | ||
| 427 |
2/2✓ Branch 0 taken 81588 times.
✓ Branch 1 taken 36353 times.
|
117941 | if (HAS_CBP(s->mtype)) |
| 428 | 81588 | cbp = get_vlc2(&h->gb, h261_cbp_vlc, H261_CBP_VLC_BITS, 1) + 1; | |
| 429 | |||
| 430 |
2/2✓ Branch 0 taken 16912 times.
✓ Branch 1 taken 101029 times.
|
117941 | if (s->mb_intra) { |
| 431 | 16912 | s->cur_pic.mb_type[xy] = MB_TYPE_INTRA; | |
| 432 | 16912 | goto intra; | |
| 433 | } | ||
| 434 | |||
| 435 | //set motion vectors | ||
| 436 | 101029 | s->mv_dir = MV_DIR_FORWARD; | |
| 437 | 101029 | s->mv_type = MV_TYPE_16X16; | |
| 438 | 101029 | s->cur_pic.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_FORWARD_MV; | |
| 439 | 101029 | s->mv[0][0][0] = h->current_mv_x * 2; // gets divided by 2 in motion compensation | |
| 440 | 101029 | s->mv[0][0][1] = h->current_mv_y * 2; | |
| 441 | |||
| 442 |
1/2✓ Branch 0 taken 101029 times.
✗ Branch 1 not taken.
|
101029 | if (s->cur_pic.motion_val[0]) { |
| 443 | ✗ | int b_stride = 2*s->mb_width + 1; | |
| 444 | ✗ | int b_xy = 2 * s->mb_x + (2 * s->mb_y) * b_stride; | |
| 445 | ✗ | s->cur_pic.motion_val[0][b_xy][0] = s->mv[0][0][0]; | |
| 446 | ✗ | s->cur_pic.motion_val[0][b_xy][1] = s->mv[0][0][1]; | |
| 447 | } | ||
| 448 | |||
| 449 | 101029 | intra: | |
| 450 | /* decode each block */ | ||
| 451 |
4/4✓ Branch 0 taken 101029 times.
✓ Branch 1 taken 16912 times.
✓ Branch 2 taken 81588 times.
✓ Branch 3 taken 19441 times.
|
117941 | if (s->mb_intra || HAS_CBP(s->mtype)) { |
| 452 | 98500 | s->bdsp.clear_blocks(h->block[0]); | |
| 453 |
2/2✓ Branch 0 taken 591000 times.
✓ Branch 1 taken 98500 times.
|
689500 | for (i = 0; i < 6; i++) { |
| 454 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 591000 times.
|
591000 | if (h261_decode_block(h, h->block[i], i, cbp & 32) < 0) |
| 455 | ✗ | return SLICE_ERROR; | |
| 456 | 591000 | cbp += cbp; | |
| 457 | } | ||
| 458 | } else { | ||
| 459 |
2/2✓ Branch 0 taken 116646 times.
✓ Branch 1 taken 19441 times.
|
136087 | for (i = 0; i < 6; i++) |
| 460 | 116646 | s->block_last_index[i] = -1; | |
| 461 | } | ||
| 462 | |||
| 463 | 117941 | ff_mpv_reconstruct_mb(s, h->block); | |
| 464 | |||
| 465 | 117941 | return SLICE_OK; | |
| 466 | } | ||
| 467 | |||
| 468 | /** | ||
| 469 | * Decode the H.261 picture header. | ||
| 470 | * @return <0 if no startcode found | ||
| 471 | */ | ||
| 472 | 300 | static int h261_decode_picture_header(H261DecContext *h, int *is_key) | |
| 473 | { | ||
| 474 | 300 | MpegEncContext *const s = &h->s; | |
| 475 | 300 | uint32_t startcode = 0; | |
| 476 | |||
| 477 |
1/2✓ Branch 1 taken 6000 times.
✗ Branch 2 not taken.
|
6000 | for (int i = get_bits_left(&h->gb); i > 24; i -= 1) { |
| 478 | 6000 | startcode = ((startcode << 1) | get_bits(&h->gb, 1)) & 0x000FFFFF; | |
| 479 | |||
| 480 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 5700 times.
|
6000 | if (startcode == 0x10) |
| 481 | 300 | break; | |
| 482 | } | ||
| 483 | |||
| 484 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 300 times.
|
300 | if (startcode != 0x10) { |
| 485 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n"); | |
| 486 | ✗ | return -1; | |
| 487 | } | ||
| 488 | |||
| 489 | /* temporal reference */ | ||
| 490 | 300 | skip_bits(&h->gb, 5); /* picture timestamp */ | |
| 491 | |||
| 492 | /* PTYPE starts here */ | ||
| 493 | 300 | skip_bits1(&h->gb); /* split screen off */ | |
| 494 | 300 | skip_bits1(&h->gb); /* camera off */ | |
| 495 | 300 | *is_key = get_bits1(&h->gb); /* freeze picture release off */ | |
| 496 | |||
| 497 | 300 | int format = get_bits1(&h->gb); | |
| 498 | |||
| 499 | // only 2 formats possible | ||
| 500 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 300 times.
|
300 | if (format == 0) { // QCIF |
| 501 | ✗ | s->width = 176; | |
| 502 | ✗ | s->height = 144; | |
| 503 | } else { // CIF | ||
| 504 | 300 | s->width = 352; | |
| 505 | 300 | s->height = 288; | |
| 506 | } | ||
| 507 | |||
| 508 | 300 | skip_bits1(&h->gb); /* still image mode off */ | |
| 509 | 300 | skip_bits1(&h->gb); /* Reserved */ | |
| 510 | |||
| 511 | /* PEI */ | ||
| 512 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 300 times.
|
300 | if (skip_1stop_8data_bits(&h->gb) < 0) |
| 513 | ✗ | return AVERROR_INVALIDDATA; | |
| 514 | |||
| 515 | 300 | h->gob_number = 0; | |
| 516 | 300 | return 0; | |
| 517 | } | ||
| 518 | |||
| 519 | 3600 | static int h261_decode_gob(H261DecContext *h) | |
| 520 | { | ||
| 521 | 3600 | MpegEncContext *const s = &h->s; | |
| 522 | |||
| 523 | /* decode mb's */ | ||
| 524 |
1/2✓ Branch 0 taken 121541 times.
✗ Branch 1 not taken.
|
121541 | while (h->current_mba <= MBA_STUFFING) { |
| 525 | int ret; | ||
| 526 | /* DCT & quantize */ | ||
| 527 | 121541 | ret = h261_decode_mb(h); | |
| 528 |
2/2✓ Branch 0 taken 3600 times.
✓ Branch 1 taken 117941 times.
|
121541 | if (ret < 0) { |
| 529 |
1/2✓ Branch 0 taken 3600 times.
✗ Branch 1 not taken.
|
3600 | if (ret == SLICE_END) { |
| 530 | 3600 | h261_decode_mb_skipped(h, h->current_mba, 33); | |
| 531 | 3600 | return 0; | |
| 532 | } | ||
| 533 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", | |
| 534 | ✗ | s->mb_x + s->mb_y * s->mb_stride); | |
| 535 | ✗ | return -1; | |
| 536 | } | ||
| 537 | |||
| 538 | 117941 | h261_decode_mb_skipped(h, | |
| 539 | 117941 | h->current_mba - h->mba_diff, | |
| 540 | 117941 | h->current_mba - 1); | |
| 541 | } | ||
| 542 | |||
| 543 | ✗ | return -1; | |
| 544 | } | ||
| 545 | |||
| 546 | 300 | static int h261_decode_frame(AVCodecContext *avctx, AVFrame *pict, | |
| 547 | int *got_frame, AVPacket *avpkt) | ||
| 548 | { | ||
| 549 | 300 | H261DecContext *const h = avctx->priv_data; | |
| 550 | 300 | const uint8_t *buf = avpkt->data; | |
| 551 | 300 | int buf_size = avpkt->size; | |
| 552 | 300 | MpegEncContext *s = &h->s; | |
| 553 | int ret, is_key; | ||
| 554 | |||
| 555 | ff_dlog(avctx, "*****frame %"PRId64" size=%d\n", avctx->frame_num, buf_size); | ||
| 556 | ff_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]); | ||
| 557 | |||
| 558 | 300 | h->gob_start_code_skipped = 0; | |
| 559 | |||
| 560 | 300 | init_get_bits(&h->gb, buf, buf_size * 8); | |
| 561 | |||
| 562 | 300 | ret = h261_decode_picture_header(h, &is_key); | |
| 563 | |||
| 564 | /* skip if the header was thrashed */ | ||
| 565 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 300 times.
|
300 | if (ret < 0) { |
| 566 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "header damaged\n"); | |
| 567 | ✗ | return -1; | |
| 568 | } | ||
| 569 | |||
| 570 |
2/4✓ Branch 0 taken 300 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 300 times.
|
300 | if (s->width != avctx->coded_width || s->height != avctx->coded_height) { |
| 571 | ✗ | ff_mpv_common_end(s); | |
| 572 | } | ||
| 573 | |||
| 574 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 294 times.
|
300 | if (!s->context_initialized) { |
| 575 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if ((ret = ff_mpv_common_init(s)) < 0) |
| 576 | ✗ | return ret; | |
| 577 | |||
| 578 | 6 | ret = ff_set_dimensions(avctx, s->width, s->height); | |
| 579 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (ret < 0) |
| 580 | ✗ | return ret; | |
| 581 | } | ||
| 582 | |||
| 583 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
300 | if ((avctx->skip_frame >= AVDISCARD_NONINTRA && !is_key) || |
| 584 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 300 times.
|
300 | avctx->skip_frame >= AVDISCARD_ALL) |
| 585 | ✗ | return buf_size; | |
| 586 | |||
| 587 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 300 times.
|
300 | if (ff_mpv_frame_start(s, avctx) < 0) |
| 588 | ✗ | return -1; | |
| 589 | |||
| 590 | 300 | ff_mpeg_er_frame_start(s); | |
| 591 | |||
| 592 | /* decode each macroblock */ | ||
| 593 | 300 | s->mb_x = 0; | |
| 594 | 300 | s->mb_y = 0; | |
| 595 | |||
| 596 |
3/4✓ Branch 0 taken 3900 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3600 times.
✓ Branch 3 taken 300 times.
|
3900 | while (h->gob_number < (s->mb_height == 18 ? 12 : 5)) { |
| 597 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3600 times.
|
3600 | if (h261_decode_gob_header(h) < 0) |
| 598 | ✗ | break; | |
| 599 | 3600 | h261_decode_gob(h); | |
| 600 | } | ||
| 601 | 300 | ff_mpv_frame_end(s); | |
| 602 | |||
| 603 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 270 times.
|
300 | if (is_key) { |
| 604 | 30 | s->cur_pic.ptr->f->pict_type = AV_PICTURE_TYPE_I; | |
| 605 | 30 | s->cur_pic.ptr->f->flags |= AV_FRAME_FLAG_KEY; | |
| 606 | } | ||
| 607 | |||
| 608 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 300 times.
|
300 | if ((ret = av_frame_ref(pict, s->cur_pic.ptr->f)) < 0) |
| 609 | ✗ | return ret; | |
| 610 | 300 | ff_print_debug_info(s, s->cur_pic.ptr, pict); | |
| 611 | |||
| 612 | 300 | *got_frame = 1; | |
| 613 | |||
| 614 | 300 | return buf_size; | |
| 615 | } | ||
| 616 | |||
| 617 | const FFCodec ff_h261_decoder = { | ||
| 618 | .p.name = "h261", | ||
| 619 | CODEC_LONG_NAME("H.261"), | ||
| 620 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
| 621 | .p.id = AV_CODEC_ID_H261, | ||
| 622 | .priv_data_size = sizeof(H261DecContext), | ||
| 623 | .init = h261_decode_init, | ||
| 624 | FF_CODEC_DECODE_CB(h261_decode_frame), | ||
| 625 | .close = ff_mpv_decode_close, | ||
| 626 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
| 627 | .p.max_lowres = 3, | ||
| 628 | .caps_internal = FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM, | ||
| 629 | }; | ||
| 630 |