- else if (avctx->pix_fmt == PIX_FMT_NONE && avctx->bits_per_coded_sample)
- avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi, avctx->bits_per_coded_sample);
-
- ff_set_systematic_pal2(context->palette, avctx->pix_fmt);
- context->length = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
- if((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
- avctx->pix_fmt==PIX_FMT_PAL8 &&
- (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))){
- context->buffer = av_malloc(context->length);
- if (!context->buffer)
- return -1;
+ else if (avctx->pix_fmt == AV_PIX_FMT_NONE && avctx->bits_per_coded_sample)
+ avctx->pix_fmt = find_pix_fmt(pix_fmt_bps_avi,
+ avctx->bits_per_coded_sample);
+
+ desc = av_pix_fmt_desc_get(avctx->pix_fmt);
+ if (!desc) {
+ av_log(avctx, AV_LOG_ERROR, "Invalid pixel format.\n");
+ return AVERROR(EINVAL);
+ }
+
+ if (desc->flags & (AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_PSEUDOPAL)) {
+ context->palette = av_buffer_alloc(AVPALETTE_SIZE);
+ if (!context->palette)
+ return AVERROR(ENOMEM);
+ if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
+ avpriv_set_systematic_pal2((uint32_t*)context->palette->data, avctx->pix_fmt);
+ else
+ memset(context->palette->data, 0, AVPALETTE_SIZE);