]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/rawdec.c
avcodec/dca: clear X96 channels if nothing was decoded
[ffmpeg] / libavcodec / rawdec.c
index 93cbedfed8adfabee0835b320001c606713a882a..765e567d1fac841f017f0f73f245ecd448489fe2 100644 (file)
@@ -41,7 +41,11 @@ typedef struct RawVideoContext {
     AVBufferRef *palette;
     int frame_size;  /* size of the frame in bytes */
     int flip;
-    int is_1_2_4_8_bpp; // 1, 2, 4 and 8 bpp in avi/mov
+    int is_1_2_4_8_bpp; // 1, 2, 4 and 8 bpp in avi/mov, 1 and 8 bpp in nut
+    int is_mono;
+    int is_pal8;
+    int is_nut_mono;
+    int is_nut_pal8;
     int is_yuv2;
     int is_lt_16bpp; // 16bpp pixfmt and bits_per_coded_sample < 16
     int tff;
@@ -108,13 +112,22 @@ static av_cold int raw_init_decoder(AVCodecContext *avctx)
         avctx->codec_tag == MKTAG('W','R','A','W'))
         context->flip = 1;
 
+    if (avctx->pix_fmt == AV_PIX_FMT_MONOWHITE ||
+        avctx->pix_fmt == AV_PIX_FMT_MONOBLACK)
+        context->is_mono = 1;
+    else if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
+        context->is_pal8 = 1;
+
+    if (avctx->codec_tag == MKTAG('B','1','W','0') ||
+        avctx->codec_tag == MKTAG('B','0','W','1'))
+        context->is_nut_mono = 1;
+    else if (avctx->codec_tag == MKTAG('P','A','L',8))
+        context->is_nut_pal8 = 1;
+
     if (avctx->codec_tag == AV_RL32("yuv2") &&
         avctx->pix_fmt   == AV_PIX_FMT_YUYV422)
         context->is_yuv2 = 1;
 
-    if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && avctx->bits_per_coded_sample == 1)
-        avctx->pix_fmt = AV_PIX_FMT_NONE;
-
     return 0;
 }
 
@@ -160,43 +173,44 @@ static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
     const uint8_t *buf             = avpkt->data;
     int buf_size                   = avpkt->size;
     int linesize_align             = 4;
-    int avpkt_stride;
+    int stride;
     int res, len;
     int need_copy;
 
     AVFrame   *frame   = data;
 
+    if (avctx->width <= 0) {
+        av_log(avctx, AV_LOG_ERROR, "width is not set\n");
+        return AVERROR_INVALIDDATA;
+    }
     if (avctx->height <= 0) {
         av_log(avctx, AV_LOG_ERROR, "height is not set\n");
         return AVERROR_INVALIDDATA;
     }
-    avpkt_stride = avpkt->size / avctx->height;
 
-    if (avpkt_stride == 0) {
-        av_log(avctx, AV_LOG_ERROR, "Packet too small (%d) height (%d)\n", avpkt->size, avctx->height);
+    if (context->is_nut_mono)
+        stride = avctx->width / 8 + (avctx->width & 7 ? 1 : 0);
+    else if (context->is_nut_pal8)
+        stride = avctx->width;
+    else
+        stride = avpkt->size / avctx->height;
+
+    av_log(avctx, AV_LOG_DEBUG, "PACKET SIZE: %d, STRIDE: %d\n", avpkt->size, stride);
+
+    if (stride == 0 || avpkt->size < stride * avctx->height) {
+        av_log(avctx, AV_LOG_ERROR, "Packet too small (%d)\n", avpkt->size);
         return AVERROR_INVALIDDATA;
     }
-    if (avctx->pix_fmt == AV_PIX_FMT_NONE &&
-        avctx->bits_per_coded_sample == 1 &&
-        avctx->frame_number == 0 &&
-        context->palette &&
-        AV_RB64(context->palette->data) == 0xFFFFFFFF00000000
-    ) {
-        const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
-        if (!pal) {
-            avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
-        } else
-            avctx->pix_fmt = AV_PIX_FMT_PAL8;
-    }
 
     desc = av_pix_fmt_desc_get(avctx->pix_fmt);
 
     if ((avctx->bits_per_coded_sample == 8 || avctx->bits_per_coded_sample == 4
-            || avctx->bits_per_coded_sample == 2 || avctx->bits_per_coded_sample == 1) &&
-        (avctx->pix_fmt == AV_PIX_FMT_PAL8 || avctx->pix_fmt == AV_PIX_FMT_MONOWHITE) &&
-        (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' '))) {
+            || avctx->bits_per_coded_sample <= 2) &&
+        (context->is_mono || context->is_pal8) &&
+        (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' ') ||
+                context->is_nut_mono || context->is_nut_pal8)) {
         context->is_1_2_4_8_bpp = 1;
-        if (avctx->bits_per_coded_sample == 1 && avctx->pix_fmt == AV_PIX_FMT_MONOWHITE) {
+        if (context->is_mono) {
             int row_bytes = avctx->width / 8 + (avctx->width & 7 ? 1 : 0);
             context->frame_size = av_image_get_buffer_size(avctx->pix_fmt,
                                                            FFALIGN(row_bytes, 16) * 8,
@@ -240,19 +254,18 @@ static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
     if (!frame->buf[0])
         return AVERROR(ENOMEM);
 
-    // 1, 2, 4 and 8 bpp in avi/mov
+    // 1, 2, 4 and 8 bpp in avi/mov, 1 and 8 bpp in nut
     if (context->is_1_2_4_8_bpp) {
         int i, j, row_pix = 0;
         uint8_t *dst = frame->buf[0]->data;
-        buf_size = context->frame_size -
-                (avctx->pix_fmt == AV_PIX_FMT_PAL8 ? AVPALETTE_SIZE : 0);
-        if (avctx->bits_per_coded_sample == 8 || avctx->pix_fmt == AV_PIX_FMT_MONOWHITE) {
-            int pix_per_byte = avctx->pix_fmt == AV_PIX_FMT_MONOWHITE ? 8 : 1;
+        buf_size = context->frame_size - (context->is_pal8 ? AVPALETTE_SIZE : 0);
+        if (avctx->bits_per_coded_sample == 8 || context->is_nut_pal8 || context->is_mono) {
+            int pix_per_byte = context->is_mono ? 8 : 1;
             for (i = 0, j = 0; j < buf_size && i<avpkt->size; i++, j++) {
                 dst[j] = buf[i];
                 row_pix += pix_per_byte;
                 if (row_pix >= avctx->width) {
-                    i += avpkt_stride - (i % avpkt_stride) - 1;
+                    i += stride - (i % stride) - 1;
                     j += 16 - (j % 16) - 1;
                     row_pix = 0;
                 }
@@ -263,7 +276,7 @@ static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
                 dst[2 * j + 1] = buf[i] & 15;
                 row_pix += 2;
                 if (row_pix >= avctx->width) {
-                    i += avpkt_stride - (i % avpkt_stride) - 1;
+                    i += stride - (i % stride) - 1;
                     j += 8 - (j % 8) - 1;
                     row_pix = 0;
                 }
@@ -276,7 +289,7 @@ static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
                 dst[4 * j + 3] = buf[i]      & 3;
                 row_pix += 4;
                 if (row_pix >= avctx->width) {
-                    i += avpkt_stride - (i % avpkt_stride) - 1;
+                    i += stride - (i % stride) - 1;
                     j += 4 - (j % 4) - 1;
                     row_pix = 0;
                 }
@@ -294,7 +307,7 @@ static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
                 dst[8 * j + 7] = buf[i]      & 1;
                 row_pix += 8;
                 if (row_pix >= avctx->width) {
-                    i += avpkt_stride - (i % avpkt_stride) - 1;
+                    i += stride - (i % stride) - 1;
                     j += 2 - (j % 2) - 1;
                     row_pix = 0;
                 }
@@ -352,7 +365,6 @@ static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
     if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE,
                                                      NULL);
-
         if (pal) {
             av_buffer_unref(&context->palette);
             context->palette = av_buffer_alloc(AVPALETTE_SIZE);
@@ -362,15 +374,24 @@ static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
             }
             memcpy(context->palette->data, pal, AVPALETTE_SIZE);
             frame->palette_has_changed = 1;
+        } else if (context->is_nut_pal8) {
+            int vid_size = avctx->width * avctx->height;
+            if (avpkt->size - vid_size) {
+                pal = avpkt->data + vid_size;
+                memcpy(context->palette->data, pal, avpkt->size - vid_size);
+                frame->palette_has_changed = 1;
+            }
         }
     }
 
-    if ((avctx->pix_fmt==AV_PIX_FMT_BGR24    ||
-        avctx->pix_fmt==AV_PIX_FMT_GRAY8    ||
-        avctx->pix_fmt==AV_PIX_FMT_RGB555LE ||
-        avctx->pix_fmt==AV_PIX_FMT_RGB555BE ||
-        avctx->pix_fmt==AV_PIX_FMT_RGB565LE ||
+    if ((avctx->pix_fmt==AV_PIX_FMT_RGB24    ||
+        avctx->pix_fmt==AV_PIX_FMT_BGR24     ||
+        avctx->pix_fmt==AV_PIX_FMT_GRAY8     ||
+        avctx->pix_fmt==AV_PIX_FMT_RGB555LE  ||
+        avctx->pix_fmt==AV_PIX_FMT_RGB555BE  ||
+        avctx->pix_fmt==AV_PIX_FMT_RGB565LE  ||
         avctx->pix_fmt==AV_PIX_FMT_MONOWHITE ||
+        avctx->pix_fmt==AV_PIX_FMT_MONOBLACK ||
         avctx->pix_fmt==AV_PIX_FMT_PAL8) &&
         FFALIGN(frame->linesize[0], linesize_align) * avctx->height <= buf_size)
         frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align);