Reuse the already computed value of macroblocks; use an array instead of a switch, evaluate 64 * sizeof(MJpegHuffmanCode) at compile-time.
Unified · Split
| @@ -277,25 +277,21 @@ fail: · 276 unmodified lines | ||
| 277 | 277 | return ret; |
| 278 | 278 | } |
| 279 | 279 | |
| 280 | −static int alloc_huffman(MpegEncContext *s) | |
| 280 | +static int alloc_huffman(MJPEGEncContext *const m2) | |
| 281 | 281 | { |
| 282 | − MJpegContext *m = s->mjpeg_ctx; | |
| 283 | − size_t num_mbs, num_blocks, num_codes; | |
| 284 | − int blocks_per_mb; | |
| 285 | − | |
| 286 | − switch (s->chroma_format) { | |
| 287 | − case CHROMA_420: blocks_per_mb = 6; break; | |
| 288 | − case CHROMA_422: blocks_per_mb = 8; break; | |
| 289 | − case CHROMA_444: blocks_per_mb = 12; break; | |
| 290 | − default: av_assert0(0); | |
| 282 | + MJpegContext *const m = &m2->mjpeg; | |
| 283 | + MpegEncContext *const s = &m2->mpeg.s; | |
| 284 | + static const char blocks_per_mb[] = { | |
| 285 | + [CHROMA_420] = 6, [CHROMA_422] = 8, [CHROMA_444] = 12 | |
| 291 | 286 | }; |
| 287 | + size_t num_blocks, num_codes; | |
| 292 | 288 | |
| 293 | 289 | // Make sure we have enough space to hold this frame. |
| 294 | − num_mbs = s->mb_width * s->mb_height; | |
| 295 | − num_blocks = num_mbs * blocks_per_mb; | |
| 290 | + num_blocks = s->mb_num * blocks_per_mb[s->chroma_format]; | |
| 296 | 291 | num_codes = num_blocks * 64; |
| 297 | 292 | |
| 298 | − m->huff_buffer = av_malloc_array(num_codes, sizeof(MJpegHuffmanCode)); | |
| 293 | + m->huff_buffer = av_malloc_array(num_codes, | |
| 294 | + 64 /* codes per MB */ * sizeof(MJpegHuffmanCode)); | |
| 299 | 295 | if (!m->huff_buffer) |
| 300 | 296 | return AVERROR(ENOMEM); |
| 301 | 297 | return 0; |
| @@ -369,7 +365,7 @@ static av_cold int mjpeg_encode_init(AVCodecContext *avctx) · 67 unmodified lines | ||
| 369 | 365 | m->huffman = HUFFMAN_TABLE_DEFAULT; |
| 370 | 366 | |
| 371 | 367 | if (m->huffman == HUFFMAN_TABLE_OPTIMAL) |
| 372 | − return alloc_huffman(s); | |
| 368 | + return alloc_huffman(m2); | |
| 373 | 369 | |
| 374 | 370 | return 0; |
| 375 | 371 | } |