Parse frame and either copy data or decode JPEG. */
| 383 | |
| 384 | /* Parse frame and either copy data or decode JPEG. */ |
| 385 | static int tdsc_decode_tiles(AVCodecContext *avctx, int number_tiles) |
| 386 | { |
| 387 | TDSCContext *ctx = avctx->priv_data; |
| 388 | int i; |
| 389 | |
| 390 | /* Iterate over the number of tiles */ |
| 391 | for (i = 0; i < number_tiles; i++) { |
| 392 | int tile_size; |
| 393 | int tile_mode; |
| 394 | int x, y, x2, y2, w, h; |
| 395 | int ret; |
| 396 | |
| 397 | if (bytestream2_get_bytes_left(&ctx->gbc) < 4 || |
| 398 | bytestream2_get_le32(&ctx->gbc) != MKTAG('T','D','S','B') || |
| 399 | bytestream2_get_bytes_left(&ctx->gbc) < TDSB_HEADER_SIZE - 4) { |
| 400 | av_log(avctx, AV_LOG_ERROR, "TDSB tag is too small.\n"); |
| 401 | return AVERROR_INVALIDDATA; |
| 402 | } |
| 403 | |
| 404 | tile_size = bytestream2_get_le32(&ctx->gbc); |
| 405 | if (bytestream2_get_bytes_left(&ctx->gbc) < tile_size + 24LL) |
| 406 | return AVERROR_INVALIDDATA; |
| 407 | |
| 408 | tile_mode = bytestream2_get_le32(&ctx->gbc); |
| 409 | bytestream2_skip(&ctx->gbc, 4); // unknown |
| 410 | x = bytestream2_get_le32(&ctx->gbc); |
| 411 | y = bytestream2_get_le32(&ctx->gbc); |
| 412 | x2 = bytestream2_get_le32(&ctx->gbc); |
| 413 | y2 = bytestream2_get_le32(&ctx->gbc); |
| 414 | |
| 415 | if (x < 0 || y < 0 || x2 <= x || y2 <= y || |
| 416 | x2 > ctx->width || y2 > ctx->height |
| 417 | ) { |
| 418 | av_log(avctx, AV_LOG_ERROR, |
| 419 | "Invalid tile position (%d.%d %d.%d outside %dx%d).\n", |
| 420 | x, y, x2, y2, ctx->width, ctx->height); |
| 421 | return AVERROR_INVALIDDATA; |
| 422 | } |
| 423 | w = x2 - x; |
| 424 | h = y2 - y; |
| 425 | |
| 426 | ret = av_reallocp(&ctx->tilebuffer, tile_size); |
| 427 | if (!ctx->tilebuffer) |
| 428 | return ret; |
| 429 | |
| 430 | bytestream2_get_buffer(&ctx->gbc, ctx->tilebuffer, tile_size); |
| 431 | |
| 432 | if (tile_mode == MKTAG('G','E','P','J')) { |
| 433 | /* Decode JPEG tile and copy it in the reference frame */ |
| 434 | ret = tdsc_decode_jpeg_tile(avctx, tile_size, x, y, w, h); |
| 435 | if (ret < 0) |
| 436 | return ret; |
| 437 | } else if (tile_mode == MKTAG(' ','W','A','R')) { |
| 438 | if (3LL * w * h > tile_size) |
| 439 | return AVERROR_INVALIDDATA; |
| 440 | |
| 441 | /* Just copy the buffer to output */ |
| 442 | av_image_copy_plane(ctx->refframe->data[0] + x * 3 + |
no test coverage detected