| 266 | } |
| 267 | |
| 268 | static int cdg_decode_frame(AVCodecContext *avctx, |
| 269 | void *data, int *data_size, AVPacket *avpkt) |
| 270 | { |
| 271 | const uint8_t *buf = avpkt->data; |
| 272 | int buf_size = avpkt->size; |
| 273 | int ret; |
| 274 | uint8_t command, inst; |
| 275 | uint8_t cdg_data[CDG_DATA_SIZE]; |
| 276 | AVFrame new_frame; |
| 277 | CDGraphicsContext *cc = avctx->priv_data; |
| 278 | |
| 279 | if (buf_size < CDG_MINIMUM_PKT_SIZE) { |
| 280 | av_log(avctx, AV_LOG_ERROR, "buffer too small for decoder\n"); |
| 281 | return AVERROR(EINVAL); |
| 282 | } |
| 283 | |
| 284 | ret = avctx->reget_buffer(avctx, &cc->frame); |
| 285 | if (ret) { |
| 286 | av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | command = bytestream_get_byte(&buf); |
| 291 | inst = bytestream_get_byte(&buf); |
| 292 | inst &= CDG_MASK; |
| 293 | buf += 2; /// skipping 2 unneeded bytes |
| 294 | bytestream_get_buffer(&buf, cdg_data, buf_size - CDG_HEADER_SIZE); |
| 295 | |
| 296 | if ((command & CDG_MASK) == CDG_COMMAND) { |
| 297 | switch (inst) { |
| 298 | case CDG_INST_MEMORY_PRESET: |
| 299 | if (!(cdg_data[1] & 0x0F)) |
| 300 | memset(cc->frame.data[0], cdg_data[0] & 0x0F, |
| 301 | cc->frame.linesize[0] * CDG_FULL_HEIGHT); |
| 302 | break; |
| 303 | case CDG_INST_LOAD_PAL_LO: |
| 304 | case CDG_INST_LOAD_PAL_HIGH: |
| 305 | if (buf_size - CDG_HEADER_SIZE < CDG_DATA_SIZE) { |
| 306 | av_log(avctx, AV_LOG_ERROR, "buffer too small for loading palette\n"); |
| 307 | return AVERROR(EINVAL); |
| 308 | } |
| 309 | |
| 310 | cdg_load_palette(cc, cdg_data, inst == CDG_INST_LOAD_PAL_LO); |
| 311 | break; |
| 312 | case CDG_INST_BORDER_PRESET: |
| 313 | cdg_border_preset(cc, cdg_data); |
| 314 | break; |
| 315 | case CDG_INST_TILE_BLOCK_XOR: |
| 316 | case CDG_INST_TILE_BLOCK: |
| 317 | if (buf_size - CDG_HEADER_SIZE < CDG_DATA_SIZE) { |
| 318 | av_log(avctx, AV_LOG_ERROR, "buffer too small for drawing tile\n"); |
| 319 | return AVERROR(EINVAL); |
| 320 | } |
| 321 | |
| 322 | ret = cdg_tile_block(cc, cdg_data, inst == CDG_INST_TILE_BLOCK_XOR); |
| 323 | if (ret) { |
| 324 | av_log(avctx, AV_LOG_ERROR, "tile is out of range\n"); |
| 325 | return ret; |
nothing calls this directly
no test coverage detected