annotate ffmpeg/libavcodec/h261dec.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * H261 decoder
yading@10 3 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
yading@10 4 * Copyright (c) 2004 Maarten Daniels
yading@10 5 *
yading@10 6 * This file is part of FFmpeg.
yading@10 7 *
yading@10 8 * FFmpeg is free software; you can redistribute it and/or
yading@10 9 * modify it under the terms of the GNU Lesser General Public
yading@10 10 * License as published by the Free Software Foundation; either
yading@10 11 * version 2.1 of the License, or (at your option) any later version.
yading@10 12 *
yading@10 13 * FFmpeg is distributed in the hope that it will be useful,
yading@10 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 16 * Lesser General Public License for more details.
yading@10 17 *
yading@10 18 * You should have received a copy of the GNU Lesser General Public
yading@10 19 * License along with FFmpeg; if not, write to the Free Software
yading@10 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 21 */
yading@10 22
yading@10 23 /**
yading@10 24 * @file
yading@10 25 * H.261 decoder.
yading@10 26 */
yading@10 27
yading@10 28 #include "libavutil/avassert.h"
yading@10 29 #include "avcodec.h"
yading@10 30 #include "mpegvideo.h"
yading@10 31 #include "h263.h"
yading@10 32 #include "h261.h"
yading@10 33
yading@10 34 #define H261_MBA_VLC_BITS 9
yading@10 35 #define H261_MTYPE_VLC_BITS 6
yading@10 36 #define H261_MV_VLC_BITS 7
yading@10 37 #define H261_CBP_VLC_BITS 9
yading@10 38 #define TCOEFF_VLC_BITS 9
yading@10 39 #define MBA_STUFFING 33
yading@10 40 #define MBA_STARTCODE 34
yading@10 41
yading@10 42 static VLC h261_mba_vlc;
yading@10 43 static VLC h261_mtype_vlc;
yading@10 44 static VLC h261_mv_vlc;
yading@10 45 static VLC h261_cbp_vlc;
yading@10 46
yading@10 47 static av_cold void h261_decode_init_vlc(H261Context *h)
yading@10 48 {
yading@10 49 static int done = 0;
yading@10 50
yading@10 51 if (!done) {
yading@10 52 done = 1;
yading@10 53 INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
yading@10 54 ff_h261_mba_bits, 1, 1,
yading@10 55 ff_h261_mba_code, 1, 1, 662);
yading@10 56 INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
yading@10 57 ff_h261_mtype_bits, 1, 1,
yading@10 58 ff_h261_mtype_code, 1, 1, 80);
yading@10 59 INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
yading@10 60 &ff_h261_mv_tab[0][1], 2, 1,
yading@10 61 &ff_h261_mv_tab[0][0], 2, 1, 144);
yading@10 62 INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
yading@10 63 &ff_h261_cbp_tab[0][1], 2, 1,
yading@10 64 &ff_h261_cbp_tab[0][0], 2, 1, 512);
yading@10 65 INIT_VLC_RL(ff_h261_rl_tcoeff, 552);
yading@10 66 }
yading@10 67 }
yading@10 68
yading@10 69 static av_cold int h261_decode_init(AVCodecContext *avctx)
yading@10 70 {
yading@10 71 H261Context *h = avctx->priv_data;
yading@10 72 MpegEncContext *const s = &h->s;
yading@10 73
yading@10 74 // set defaults
yading@10 75 ff_MPV_decode_defaults(s);
yading@10 76 s->avctx = avctx;
yading@10 77 s->width = s->avctx->coded_width;
yading@10 78 s->height = s->avctx->coded_height;
yading@10 79 s->codec_id = s->avctx->codec->id;
yading@10 80 s->out_format = FMT_H261;
yading@10 81 s->low_delay = 1;
yading@10 82 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
yading@10 83 s->codec_id = avctx->codec->id;
yading@10 84
yading@10 85 ff_h261_common_init();
yading@10 86 h261_decode_init_vlc(h);
yading@10 87
yading@10 88 h->gob_start_code_skipped = 0;
yading@10 89
yading@10 90 return 0;
yading@10 91 }
yading@10 92
yading@10 93 /**
yading@10 94 * Decode the group of blocks header or slice header.
yading@10 95 * @return <0 if an error occurred
yading@10 96 */
yading@10 97 static int h261_decode_gob_header(H261Context *h)
yading@10 98 {
yading@10 99 unsigned int val;
yading@10 100 MpegEncContext *const s = &h->s;
yading@10 101
yading@10 102 if (!h->gob_start_code_skipped) {
yading@10 103 /* Check for GOB Start Code */
yading@10 104 val = show_bits(&s->gb, 15);
yading@10 105 if (val)
yading@10 106 return -1;
yading@10 107
yading@10 108 /* We have a GBSC */
yading@10 109 skip_bits(&s->gb, 16);
yading@10 110 }
yading@10 111
yading@10 112 h->gob_start_code_skipped = 0;
yading@10 113
yading@10 114 h->gob_number = get_bits(&s->gb, 4); /* GN */
yading@10 115 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
yading@10 116
yading@10 117 /* Check if gob_number is valid */
yading@10 118 if (s->mb_height == 18) { // CIF
yading@10 119 if ((h->gob_number <= 0) || (h->gob_number > 12))
yading@10 120 return -1;
yading@10 121 } else { // QCIF
yading@10 122 if ((h->gob_number != 1) && (h->gob_number != 3) &&
yading@10 123 (h->gob_number != 5))
yading@10 124 return -1;
yading@10 125 }
yading@10 126
yading@10 127 /* GEI */
yading@10 128 while (get_bits1(&s->gb) != 0)
yading@10 129 skip_bits(&s->gb, 8);
yading@10 130
yading@10 131 if (s->qscale == 0) {
yading@10 132 av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
yading@10 133 if (s->avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
yading@10 134 return -1;
yading@10 135 }
yading@10 136
yading@10 137 /* For the first transmitted macroblock in a GOB, MBA is the absolute
yading@10 138 * address. For subsequent macroblocks, MBA is the difference between
yading@10 139 * the absolute addresses of the macroblock and the last transmitted
yading@10 140 * macroblock. */
yading@10 141 h->current_mba = 0;
yading@10 142 h->mba_diff = 0;
yading@10 143
yading@10 144 return 0;
yading@10 145 }
yading@10 146
yading@10 147 /**
yading@10 148 * Decode the group of blocks / video packet header.
yading@10 149 * @return <0 if no resync found
yading@10 150 */
yading@10 151 static int ff_h261_resync(H261Context *h)
yading@10 152 {
yading@10 153 MpegEncContext *const s = &h->s;
yading@10 154 int left, ret;
yading@10 155
yading@10 156 if (h->gob_start_code_skipped) {
yading@10 157 ret = h261_decode_gob_header(h);
yading@10 158 if (ret >= 0)
yading@10 159 return 0;
yading@10 160 } else {
yading@10 161 if (show_bits(&s->gb, 15) == 0) {
yading@10 162 ret = h261_decode_gob_header(h);
yading@10 163 if (ret >= 0)
yading@10 164 return 0;
yading@10 165 }
yading@10 166 // OK, it is not where it is supposed to be ...
yading@10 167 s->gb = s->last_resync_gb;
yading@10 168 align_get_bits(&s->gb);
yading@10 169 left = get_bits_left(&s->gb);
yading@10 170
yading@10 171 for (; left > 15 + 1 + 4 + 5; left -= 8) {
yading@10 172 if (show_bits(&s->gb, 15) == 0) {
yading@10 173 GetBitContext bak = s->gb;
yading@10 174
yading@10 175 ret = h261_decode_gob_header(h);
yading@10 176 if (ret >= 0)
yading@10 177 return 0;
yading@10 178
yading@10 179 s->gb = bak;
yading@10 180 }
yading@10 181 skip_bits(&s->gb, 8);
yading@10 182 }
yading@10 183 }
yading@10 184
yading@10 185 return -1;
yading@10 186 }
yading@10 187
yading@10 188 /**
yading@10 189 * Decode skipped macroblocks.
yading@10 190 * @return 0
yading@10 191 */
yading@10 192 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2)
yading@10 193 {
yading@10 194 MpegEncContext *const s = &h->s;
yading@10 195 int i;
yading@10 196
yading@10 197 s->mb_intra = 0;
yading@10 198
yading@10 199 for (i = mba1; i < mba2; i++) {
yading@10 200 int j, xy;
yading@10 201
yading@10 202 s->mb_x = ((h->gob_number - 1) % 2) * 11 + i % 11;
yading@10 203 s->mb_y = ((h->gob_number - 1) / 2) * 3 + i / 11;
yading@10 204 xy = s->mb_x + s->mb_y * s->mb_stride;
yading@10 205 ff_init_block_index(s);
yading@10 206 ff_update_block_index(s);
yading@10 207
yading@10 208 for (j = 0; j < 6; j++)
yading@10 209 s->block_last_index[j] = -1;
yading@10 210
yading@10 211 s->mv_dir = MV_DIR_FORWARD;
yading@10 212 s->mv_type = MV_TYPE_16X16;
yading@10 213 s->current_picture.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
yading@10 214 s->mv[0][0][0] = 0;
yading@10 215 s->mv[0][0][1] = 0;
yading@10 216 s->mb_skipped = 1;
yading@10 217 h->mtype &= ~MB_TYPE_H261_FIL;
yading@10 218
yading@10 219 ff_MPV_decode_mb(s, s->block);
yading@10 220 }
yading@10 221
yading@10 222 return 0;
yading@10 223 }
yading@10 224
yading@10 225 static const int mvmap[17] = {
yading@10 226 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
yading@10 227 };
yading@10 228
yading@10 229 static int decode_mv_component(GetBitContext *gb, int v)
yading@10 230 {
yading@10 231 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
yading@10 232
yading@10 233 /* check if mv_diff is valid */
yading@10 234 if (mv_diff < 0)
yading@10 235 return v;
yading@10 236
yading@10 237 mv_diff = mvmap[mv_diff];
yading@10 238
yading@10 239 if (mv_diff && !get_bits1(gb))
yading@10 240 mv_diff = -mv_diff;
yading@10 241
yading@10 242 v += mv_diff;
yading@10 243 if (v <= -16)
yading@10 244 v += 32;
yading@10 245 else if (v >= 16)
yading@10 246 v -= 32;
yading@10 247
yading@10 248 return v;
yading@10 249 }
yading@10 250
yading@10 251 /**
yading@10 252 * Decode a macroblock.
yading@10 253 * @return <0 if an error occurred
yading@10 254 */
yading@10 255 static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded)
yading@10 256 {
yading@10 257 MpegEncContext *const s = &h->s;
yading@10 258 int code, level, i, j, run;
yading@10 259 RLTable *rl = &ff_h261_rl_tcoeff;
yading@10 260 const uint8_t *scan_table;
yading@10 261
yading@10 262 /* For the variable length encoding there are two code tables, one being
yading@10 263 * used for the first transmitted LEVEL in INTER, INTER + MC and
yading@10 264 * INTER + MC + FIL blocks, the second for all other LEVELs except the
yading@10 265 * first one in INTRA blocks which is fixed length coded with 8 bits.
yading@10 266 * NOTE: The two code tables only differ in one VLC so we handle that
yading@10 267 * manually. */
yading@10 268 scan_table = s->intra_scantable.permutated;
yading@10 269 if (s->mb_intra) {
yading@10 270 /* DC coef */
yading@10 271 level = get_bits(&s->gb, 8);
yading@10 272 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
yading@10 273 if ((level & 0x7F) == 0) {
yading@10 274 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n",
yading@10 275 level, s->mb_x, s->mb_y);
yading@10 276 return -1;
yading@10 277 }
yading@10 278 /* The code 1000 0000 is not used, the reconstruction level of 1024
yading@10 279 * being coded as 1111 1111. */
yading@10 280 if (level == 255)
yading@10 281 level = 128;
yading@10 282 block[0] = level;
yading@10 283 i = 1;
yading@10 284 } else if (coded) {
yading@10 285 // Run Level Code
yading@10 286 // EOB Not possible for first level when cbp is available (that's why the table is different)
yading@10 287 // 0 1 1s
yading@10 288 // * * 0*
yading@10 289 int check = show_bits(&s->gb, 2);
yading@10 290 i = 0;
yading@10 291 if (check & 0x2) {
yading@10 292 skip_bits(&s->gb, 2);
yading@10 293 block[0] = (check & 0x1) ? -1 : 1;
yading@10 294 i = 1;
yading@10 295 }
yading@10 296 } else {
yading@10 297 i = 0;
yading@10 298 }
yading@10 299 if (!coded) {
yading@10 300 s->block_last_index[n] = i - 1;
yading@10 301 return 0;
yading@10 302 }
yading@10 303 for (;;) {
yading@10 304 code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
yading@10 305 if (code < 0) {
yading@10 306 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n",
yading@10 307 s->mb_x, s->mb_y);
yading@10 308 return -1;
yading@10 309 }
yading@10 310 if (code == rl->n) {
yading@10 311 /* escape */
yading@10 312 /* The remaining combinations of (run, level) are encoded with a
yading@10 313 * 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits
yading@10 314 * level. */
yading@10 315 run = get_bits(&s->gb, 6);
yading@10 316 level = get_sbits(&s->gb, 8);
yading@10 317 } else if (code == 0) {
yading@10 318 break;
yading@10 319 } else {
yading@10 320 run = rl->table_run[code];
yading@10 321 level = rl->table_level[code];
yading@10 322 if (get_bits1(&s->gb))
yading@10 323 level = -level;
yading@10 324 }
yading@10 325 i += run;
yading@10 326 if (i >= 64) {
yading@10 327 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n",
yading@10 328 s->mb_x, s->mb_y);
yading@10 329 return -1;
yading@10 330 }
yading@10 331 j = scan_table[i];
yading@10 332 block[j] = level;
yading@10 333 i++;
yading@10 334 }
yading@10 335 s->block_last_index[n] = i - 1;
yading@10 336 return 0;
yading@10 337 }
yading@10 338
yading@10 339 static int h261_decode_mb(H261Context *h)
yading@10 340 {
yading@10 341 MpegEncContext *const s = &h->s;
yading@10 342 int i, cbp, xy;
yading@10 343
yading@10 344 cbp = 63;
yading@10 345 // Read mba
yading@10 346 do {
yading@10 347 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table,
yading@10 348 H261_MBA_VLC_BITS, 2);
yading@10 349
yading@10 350 /* Check for slice end */
yading@10 351 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
yading@10 352 if (h->mba_diff == MBA_STARTCODE) { // start code
yading@10 353 h->gob_start_code_skipped = 1;
yading@10 354 return SLICE_END;
yading@10 355 }
yading@10 356 } while (h->mba_diff == MBA_STUFFING); // stuffing
yading@10 357
yading@10 358 if (h->mba_diff < 0) {
yading@10 359 if (get_bits_left(&s->gb) <= 7)
yading@10 360 return SLICE_END;
yading@10 361
yading@10 362 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
yading@10 363 return SLICE_ERROR;
yading@10 364 }
yading@10 365
yading@10 366 h->mba_diff += 1;
yading@10 367 h->current_mba += h->mba_diff;
yading@10 368
yading@10 369 if (h->current_mba > MBA_STUFFING)
yading@10 370 return SLICE_ERROR;
yading@10 371
yading@10 372 s->mb_x = ((h->gob_number - 1) % 2) * 11 + ((h->current_mba - 1) % 11);
yading@10 373 s->mb_y = ((h->gob_number - 1) / 2) * 3 + ((h->current_mba - 1) / 11);
yading@10 374 xy = s->mb_x + s->mb_y * s->mb_stride;
yading@10 375 ff_init_block_index(s);
yading@10 376 ff_update_block_index(s);
yading@10 377
yading@10 378 // Read mtype
yading@10 379 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
yading@10 380 if (h->mtype < 0) {
yading@10 381 av_log(s->avctx, AV_LOG_ERROR, "illegal mtype %d\n", h->mtype);
yading@10 382 return SLICE_ERROR;
yading@10 383 }
yading@10 384 h->mtype = ff_h261_mtype_map[h->mtype];
yading@10 385
yading@10 386 // Read mquant
yading@10 387 if (IS_QUANT(h->mtype))
yading@10 388 ff_set_qscale(s, get_bits(&s->gb, 5));
yading@10 389
yading@10 390 s->mb_intra = IS_INTRA4x4(h->mtype);
yading@10 391
yading@10 392 // Read mv
yading@10 393 if (IS_16X16(h->mtype)) {
yading@10 394 /* Motion vector data is included for all MC macroblocks. MVD is
yading@10 395 * obtained from the macroblock vector by subtracting the vector
yading@10 396 * of the preceding macroblock. For this calculation the vector
yading@10 397 * of the preceding macroblock is regarded as zero in the
yading@10 398 * following three situations:
yading@10 399 * 1) evaluating MVD for macroblocks 1, 12 and 23;
yading@10 400 * 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
yading@10 401 * 3) MTYPE of the previous macroblock was not MC. */
yading@10 402 if ((h->current_mba == 1) || (h->current_mba == 12) ||
yading@10 403 (h->current_mba == 23) || (h->mba_diff != 1)) {
yading@10 404 h->current_mv_x = 0;
yading@10 405 h->current_mv_y = 0;
yading@10 406 }
yading@10 407
yading@10 408 h->current_mv_x = decode_mv_component(&s->gb, h->current_mv_x);
yading@10 409 h->current_mv_y = decode_mv_component(&s->gb, h->current_mv_y);
yading@10 410 } else {
yading@10 411 h->current_mv_x = 0;
yading@10 412 h->current_mv_y = 0;
yading@10 413 }
yading@10 414
yading@10 415 // Read cbp
yading@10 416 if (HAS_CBP(h->mtype))
yading@10 417 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
yading@10 418
yading@10 419 if (s->mb_intra) {
yading@10 420 s->current_picture.mb_type[xy] = MB_TYPE_INTRA;
yading@10 421 goto intra;
yading@10 422 }
yading@10 423
yading@10 424 //set motion vectors
yading@10 425 s->mv_dir = MV_DIR_FORWARD;
yading@10 426 s->mv_type = MV_TYPE_16X16;
yading@10 427 s->current_picture.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
yading@10 428 s->mv[0][0][0] = h->current_mv_x * 2; // gets divided by 2 in motion compensation
yading@10 429 s->mv[0][0][1] = h->current_mv_y * 2;
yading@10 430
yading@10 431 intra:
yading@10 432 /* decode each block */
yading@10 433 if (s->mb_intra || HAS_CBP(h->mtype)) {
yading@10 434 s->dsp.clear_blocks(s->block[0]);
yading@10 435 for (i = 0; i < 6; i++) {
yading@10 436 if (h261_decode_block(h, s->block[i], i, cbp & 32) < 0)
yading@10 437 return SLICE_ERROR;
yading@10 438 cbp += cbp;
yading@10 439 }
yading@10 440 } else {
yading@10 441 for (i = 0; i < 6; i++)
yading@10 442 s->block_last_index[i] = -1;
yading@10 443 }
yading@10 444
yading@10 445 ff_MPV_decode_mb(s, s->block);
yading@10 446
yading@10 447 return SLICE_OK;
yading@10 448 }
yading@10 449
yading@10 450 /**
yading@10 451 * Decode the H.261 picture header.
yading@10 452 * @return <0 if no startcode found
yading@10 453 */
yading@10 454 static int h261_decode_picture_header(H261Context *h)
yading@10 455 {
yading@10 456 MpegEncContext *const s = &h->s;
yading@10 457 int format, i;
yading@10 458 uint32_t startcode = 0;
yading@10 459
yading@10 460 for (i = get_bits_left(&s->gb); i > 24; i -= 1) {
yading@10 461 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
yading@10 462
yading@10 463 if (startcode == 0x10)
yading@10 464 break;
yading@10 465 }
yading@10 466
yading@10 467 if (startcode != 0x10) {
yading@10 468 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
yading@10 469 return -1;
yading@10 470 }
yading@10 471
yading@10 472 /* temporal reference */
yading@10 473 i = get_bits(&s->gb, 5); /* picture timestamp */
yading@10 474 if (i < (s->picture_number & 31))
yading@10 475 i += 32;
yading@10 476 s->picture_number = (s->picture_number & ~31) + i;
yading@10 477
yading@10 478 s->avctx->time_base = (AVRational) { 1001, 30000 };
yading@10 479 s->current_picture.f.pts = s->picture_number;
yading@10 480
yading@10 481 /* PTYPE starts here */
yading@10 482 skip_bits1(&s->gb); /* split screen off */
yading@10 483 skip_bits1(&s->gb); /* camera off */
yading@10 484 skip_bits1(&s->gb); /* freeze picture release off */
yading@10 485
yading@10 486 format = get_bits1(&s->gb);
yading@10 487
yading@10 488 // only 2 formats possible
yading@10 489 if (format == 0) { // QCIF
yading@10 490 s->width = 176;
yading@10 491 s->height = 144;
yading@10 492 s->mb_width = 11;
yading@10 493 s->mb_height = 9;
yading@10 494 } else { // CIF
yading@10 495 s->width = 352;
yading@10 496 s->height = 288;
yading@10 497 s->mb_width = 22;
yading@10 498 s->mb_height = 18;
yading@10 499 }
yading@10 500
yading@10 501 s->mb_num = s->mb_width * s->mb_height;
yading@10 502
yading@10 503 skip_bits1(&s->gb); /* still image mode off */
yading@10 504 skip_bits1(&s->gb); /* Reserved */
yading@10 505
yading@10 506 /* PEI */
yading@10 507 while (get_bits1(&s->gb) != 0)
yading@10 508 skip_bits(&s->gb, 8);
yading@10 509
yading@10 510 /* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first
yading@10 511 * frame, the codec crashes if it does not contain all I-blocks
yading@10 512 * (e.g. when a packet is lost). */
yading@10 513 s->pict_type = AV_PICTURE_TYPE_P;
yading@10 514
yading@10 515 h->gob_number = 0;
yading@10 516 return 0;
yading@10 517 }
yading@10 518
yading@10 519 static int h261_decode_gob(H261Context *h)
yading@10 520 {
yading@10 521 MpegEncContext *const s = &h->s;
yading@10 522
yading@10 523 ff_set_qscale(s, s->qscale);
yading@10 524
yading@10 525 /* decode mb's */
yading@10 526 while (h->current_mba <= MBA_STUFFING) {
yading@10 527 int ret;
yading@10 528 /* DCT & quantize */
yading@10 529 ret = h261_decode_mb(h);
yading@10 530 if (ret < 0) {
yading@10 531 if (ret == SLICE_END) {
yading@10 532 h261_decode_mb_skipped(h, h->current_mba, 33);
yading@10 533 return 0;
yading@10 534 }
yading@10 535 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n",
yading@10 536 s->mb_x + s->mb_y * s->mb_stride);
yading@10 537 return -1;
yading@10 538 }
yading@10 539
yading@10 540 h261_decode_mb_skipped(h,
yading@10 541 h->current_mba - h->mba_diff,
yading@10 542 h->current_mba - 1);
yading@10 543 }
yading@10 544
yading@10 545 return -1;
yading@10 546 }
yading@10 547
yading@10 548 /**
yading@10 549 * returns the number of bytes consumed for building the current frame
yading@10 550 */
yading@10 551 static int get_consumed_bytes(MpegEncContext *s, int buf_size)
yading@10 552 {
yading@10 553 int pos = get_bits_count(&s->gb) >> 3;
yading@10 554 if (pos == 0)
yading@10 555 pos = 1; // avoid infinite loops (i doubt that is needed but ...)
yading@10 556 if (pos + 10 > buf_size)
yading@10 557 pos = buf_size; // oops ;)
yading@10 558
yading@10 559 return pos;
yading@10 560 }
yading@10 561
yading@10 562 static int h261_decode_frame(AVCodecContext *avctx, void *data,
yading@10 563 int *got_frame, AVPacket *avpkt)
yading@10 564 {
yading@10 565 const uint8_t *buf = avpkt->data;
yading@10 566 int buf_size = avpkt->size;
yading@10 567 H261Context *h = avctx->priv_data;
yading@10 568 MpegEncContext *s = &h->s;
yading@10 569 int ret;
yading@10 570 AVFrame *pict = data;
yading@10 571
yading@10 572 av_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
yading@10 573 av_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
yading@10 574 s->flags = avctx->flags;
yading@10 575 s->flags2 = avctx->flags2;
yading@10 576
yading@10 577 h->gob_start_code_skipped = 0;
yading@10 578
yading@10 579 retry:
yading@10 580 init_get_bits(&s->gb, buf, buf_size * 8);
yading@10 581
yading@10 582 if (!s->context_initialized)
yading@10 583 // we need the IDCT permutaton for reading a custom matrix
yading@10 584 if (ff_MPV_common_init(s) < 0)
yading@10 585 return -1;
yading@10 586
yading@10 587 /* We need to set current_picture_ptr before reading the header,
yading@10 588 * otherwise we cannot store anything in there. */
yading@10 589 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
yading@10 590 int i = ff_find_unused_picture(s, 0);
yading@10 591 if (i < 0)
yading@10 592 return i;
yading@10 593 s->current_picture_ptr = &s->picture[i];
yading@10 594 }
yading@10 595
yading@10 596 ret = h261_decode_picture_header(h);
yading@10 597
yading@10 598 /* skip if the header was thrashed */
yading@10 599 if (ret < 0) {
yading@10 600 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
yading@10 601 return -1;
yading@10 602 }
yading@10 603
yading@10 604 if (s->width != avctx->coded_width || s->height != avctx->coded_height) {
yading@10 605 ParseContext pc = s->parse_context; // FIXME move this demuxing hack to libavformat
yading@10 606 s->parse_context.buffer = 0;
yading@10 607 ff_MPV_common_end(s);
yading@10 608 s->parse_context = pc;
yading@10 609 }
yading@10 610 if (!s->context_initialized) {
yading@10 611 avcodec_set_dimensions(avctx, s->width, s->height);
yading@10 612
yading@10 613 goto retry;
yading@10 614 }
yading@10 615
yading@10 616 // for skipping the frame
yading@10 617 s->current_picture.f.pict_type = s->pict_type;
yading@10 618 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
yading@10 619
yading@10 620 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
yading@10 621 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
yading@10 622 avctx->skip_frame >= AVDISCARD_ALL)
yading@10 623 return get_consumed_bytes(s, buf_size);
yading@10 624
yading@10 625 if (ff_MPV_frame_start(s, avctx) < 0)
yading@10 626 return -1;
yading@10 627
yading@10 628 ff_mpeg_er_frame_start(s);
yading@10 629
yading@10 630 /* decode each macroblock */
yading@10 631 s->mb_x = 0;
yading@10 632 s->mb_y = 0;
yading@10 633
yading@10 634 while (h->gob_number < (s->mb_height == 18 ? 12 : 5)) {
yading@10 635 if (ff_h261_resync(h) < 0)
yading@10 636 break;
yading@10 637 h261_decode_gob(h);
yading@10 638 }
yading@10 639 ff_MPV_frame_end(s);
yading@10 640
yading@10 641 av_assert0(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
yading@10 642 av_assert0(s->current_picture.f.pict_type == s->pict_type);
yading@10 643
yading@10 644 if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
yading@10 645 return ret;
yading@10 646 ff_print_debug_info(s, s->current_picture_ptr, pict);
yading@10 647
yading@10 648 *got_frame = 1;
yading@10 649
yading@10 650 return get_consumed_bytes(s, buf_size);
yading@10 651 }
yading@10 652
yading@10 653 static av_cold int h261_decode_end(AVCodecContext *avctx)
yading@10 654 {
yading@10 655 H261Context *h = avctx->priv_data;
yading@10 656 MpegEncContext *s = &h->s;
yading@10 657
yading@10 658 ff_MPV_common_end(s);
yading@10 659 return 0;
yading@10 660 }
yading@10 661
yading@10 662 AVCodec ff_h261_decoder = {
yading@10 663 .name = "h261",
yading@10 664 .type = AVMEDIA_TYPE_VIDEO,
yading@10 665 .id = AV_CODEC_ID_H261,
yading@10 666 .priv_data_size = sizeof(H261Context),
yading@10 667 .init = h261_decode_init,
yading@10 668 .close = h261_decode_end,
yading@10 669 .decode = h261_decode_frame,
yading@10 670 .capabilities = CODEC_CAP_DR1,
yading@10 671 .max_lowres = 3,
yading@10 672 .long_name = NULL_IF_CONFIG_SMALL("H.261"),
yading@10 673 };