Browse code

Add CPiA video decoder

The cpia video decoder is intended to be used with the v4l2 demuxer.
There are some small changes to the v4l2 demuxer to support the
variable frame length of the format.
Fixes ticket #1537

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Stephan Hilb authored on 2012/08/24 01:40:22
Showing 9 changed files
... ...
@@ -55,6 +55,7 @@ version next:
55 55
 - Matroska demuxer now identifies SRT subtitles as AV_CODEC_ID_SUBRIP
56 56
   instead of AV_CODEC_ID_TEXT
57 57
 - smartblur filter ported from MPlayer
58
+- CPiA decoder
58 59
 
59 60
 
60 61
 version 0.11:
... ...
@@ -134,6 +134,7 @@ Codecs:
134 134
   cljr                                  Alex Beregszaszi
135 135
   cllc.c                                Derek Buitenhuis
136 136
   cook.c, cookdata.h                    Benjamin Larsson
137
+  cpia.c                                Stephan Hilb
137 138
   crystalhd.c                           Philip Langdale
138 139
   cscd.c                                Reimar Doeffinger
139 140
   dca.c                                 Kostya Shishkov, Benjamin Larsson
... ...
@@ -440,4 +441,5 @@ Reynaldo H. Verdejo Pinochet  6E27 CD34 170C C78E 4D4F 5F40 C18E 077F 3114 452A
440 440
 Robert Swain                  EE7A 56EA 4A81 A7B5 2001 A521 67FA 362D A2FC 3E71
441 441
 Sascha Sommer                 38A0 F88B 868E 9D3A 97D4 D6A0 E823 706F 1E07 0D3C
442 442
 Stefano Sabatini              9A43 10F8 D32C D33C 48E7 C52C 5DF2 8E4D B2EE 066B
443
+Stephan Hilb                  4F38 0B3A 5F39 B99B F505 E562 8D5C 5554 4E17 8863
443 444
 Tomas Härdin                  D133 29CA 4EEC 9DB4 7076 F697 B04B 7403 3313 41FD
... ...
@@ -512,6 +512,7 @@ following image formats are supported:
512 512
 @item Cinepak                @tab     @tab  X
513 513
 @item Cirrus Logic AccuPak   @tab  X  @tab  X
514 514
     @tab fourcc: CLJR
515
+@item CPiA Video Format      @tab     @tab  X
515 516
 @item Creative YUV (CYUV)    @tab     @tab  X
516 517
 @item DFA                    @tab     @tab  X
517 518
     @tab Codec used in Chronomaster game.
... ...
@@ -141,6 +141,7 @@ OBJS-$(CONFIG_CLJR_DECODER)            += cljr.o
141 141
 OBJS-$(CONFIG_CLJR_ENCODER)            += cljr.o
142 142
 OBJS-$(CONFIG_CLLC_DECODER)            += cllc.o
143 143
 OBJS-$(CONFIG_COOK_DECODER)            += cook.o
144
+OBJS-$(CONFIG_CPIA_DECODER)            += cpia.o
144 145
 OBJS-$(CONFIG_CSCD_DECODER)            += cscd.o
145 146
 OBJS-$(CONFIG_CYUV_DECODER)            += cyuv.o
146 147
 OBJS-$(CONFIG_DCA_DECODER)             += dcadec.o dca.o dcadsp.o      \
... ...
@@ -97,6 +97,7 @@ void avcodec_register_all(void)
97 97
     REGISTER_DECODER (CINEPAK, cinepak);
98 98
     REGISTER_ENCDEC  (CLJR, cljr);
99 99
     REGISTER_DECODER (CLLC, cllc);
100
+    REGISTER_DECODER (CPIA, cpia);
100 101
     REGISTER_DECODER (CSCD, cscd);
101 102
     REGISTER_DECODER (CYUV, cyuv);
102 103
     REGISTER_DECODER (DFA, dfa);
... ...
@@ -280,6 +280,7 @@ enum AVCodecID {
280 280
     AV_CODEC_ID_SANM       = MKBETAG('S','A','N','M'),
281 281
     AV_CODEC_ID_PAF_VIDEO  = MKBETAG('P','A','F','V'),
282 282
     AV_CODEC_ID_AVRN       = MKBETAG('A','V','R','n'),
283
+    AV_CODEC_ID_CPIA       = MKBETAG('C','P','I','A'),
283 284
 
284 285
     /* various PCM "codecs" */
285 286
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
... ...
@@ -1183,6 +1183,12 @@ static const AVCodecDescriptor codec_descriptors[] = {
1183 1183
         .name      = "avrn",
1184 1184
         .long_name = NULL_IF_CONFIG_SMALL("Avid AVI Codec"),
1185 1185
     },
1186
+    {
1187
+        .id        = AV_CODEC_ID_CPIA,
1188
+        .type      = AVMEDIA_TYPE_VIDEO,
1189
+        .name      = "cpia",
1190
+        .long_name = NULL_IF_CONFIG_SMALL("CPiA video format"),
1191
+    },
1186 1192
 
1187 1193
     /* various PCM "codecs" */
1188 1194
     {
1189 1195
new file mode 100644
... ...
@@ -0,0 +1,218 @@
0
+/*
1
+ * CPiA video decoder.
2
+ * Copyright (c) 2010 Hans de Goede <hdegoede@redhat.com>
3
+ *
4
+ * This decoder is based on the LGPL code available at
5
+ * https://v4l4j.googlecode.com/svn/v4l4j/trunk/libvideo/libv4lconvert/cpia1.c
6
+ *
7
+ * This file is part of FFmpeg.
8
+ *
9
+ * FFmpeg is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU Lesser General Public
11
+ * License as published by the Free Software Foundation; either
12
+ * version 2.1 of the License, or (at your option) any later version.
13
+ *
14
+ * FFmpeg is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
+ * Lesser General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU Lesser General Public
20
+ * License along with FFmpeg; if not, write to the Free Software
21
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
+ */
23
+
24
+#include "avcodec.h"
25
+#include "get_bits.h"
26
+
27
+
28
+#define FRAME_HEADER_SIZE 64
29
+#define MAGIC_0         0x19    /**< First header byte */
30
+#define MAGIC_1         0x68    /**< Second header byte */
31
+#define SUBSAMPLE_420      0
32
+#define SUBSAMPLE_422      1
33
+#define YUVORDER_YUYV      0
34
+#define YUVORDER_UYVY      1
35
+#define NOT_COMPRESSED     0
36
+#define COMPRESSED         1
37
+#define NO_DECIMATION      0
38
+#define DECIMATION_ENAB    1
39
+#define EOL             0xfd    /**< End Of Line marker */
40
+#define EOI             0xff    /**< End Of Image marker */
41
+
42
+
43
+typedef struct {
44
+    AVFrame frame;
45
+} CpiaContext;
46
+
47
+
48
+static int cpia_decode_frame(AVCodecContext* avctx,
49
+        void* data, int* data_size, AVPacket* avpkt)
50
+{
51
+    CpiaContext* const cpia = avctx->priv_data;
52
+    int i,j,ret;
53
+
54
+    uint8_t* const header = avpkt->data;
55
+    uint8_t* src;
56
+    int src_size;
57
+    uint16_t linelength;
58
+    uint8_t skip;
59
+
60
+    AVFrame* const frame = &cpia->frame;
61
+    uint8_t *y, *u, *v, *y_end, *u_end, *v_end;
62
+
63
+    // Get buffer filled with previous frame
64
+    if ((ret = avctx->reget_buffer(avctx, frame)) < 0) {
65
+        av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed!\n");
66
+        return ret;
67
+    }
68
+
69
+    // Check header
70
+    if ( avpkt->size < FRAME_HEADER_SIZE
71
+      || header[0] != MAGIC_0 || header[1] != MAGIC_1
72
+      || (header[17] != SUBSAMPLE_420 && header[17] != SUBSAMPLE_422)
73
+      || (header[18] != YUVORDER_YUYV && header[18] != YUVORDER_UYVY)
74
+      || (header[28] != NOT_COMPRESSED && header[28] != COMPRESSED)
75
+      || (header[29] != NO_DECIMATION && header[29] != DECIMATION_ENAB)
76
+    ) {
77
+        av_log(avctx, AV_LOG_ERROR, "Invalid header!\n");
78
+        return AVERROR_INVALIDDATA;
79
+    }
80
+
81
+    // currently unsupported properties
82
+    if (header[17] == SUBSAMPLE_422) {
83
+        av_log(avctx, AV_LOG_ERROR, "Unsupported subsample!\n");
84
+        return AVERROR_PATCHWELCOME;
85
+    }
86
+    if (header[18] == YUVORDER_UYVY) {
87
+        av_log(avctx, AV_LOG_ERROR, "Unsupported YUV byte order!\n");
88
+        return AVERROR_PATCHWELCOME;
89
+    }
90
+    if (header[29] == DECIMATION_ENAB) {
91
+        av_log(avctx, AV_LOG_ERROR, "Decimation unsupported!\n");
92
+        return AVERROR_PATCHWELCOME;
93
+    }
94
+
95
+    src = header + FRAME_HEADER_SIZE;
96
+    src_size = avpkt->size - FRAME_HEADER_SIZE;
97
+
98
+    if (header[28] == NOT_COMPRESSED) {
99
+        frame->pict_type = AV_PICTURE_TYPE_I;
100
+        frame->key_frame = 1;
101
+    } else {
102
+        frame->pict_type = AV_PICTURE_TYPE_P;
103
+        frame->key_frame = 0;
104
+    }
105
+
106
+
107
+    for ( i = 0;
108
+          i < frame->height;
109
+          i++, src += linelength, src_size -= linelength
110
+    ) {
111
+        // Read line length, two byte little endian
112
+        linelength = AV_RL16(src);
113
+        src += 2;
114
+
115
+        if (src_size < linelength) {
116
+            frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
117
+            av_log(avctx, AV_LOG_WARNING, "Frame ended enexpectedly!\n");
118
+            break;
119
+        }
120
+        if (src[linelength - 1] != EOL) {
121
+            frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
122
+            av_log(avctx, AV_LOG_WARNING, "Wrong line length %d or line not terminated properly (found 0x%02x)!\n", linelength, src[linelength - 1]);
123
+            break;
124
+        }
125
+
126
+        /* Update the data pointers. Y data is on every line.
127
+         * U and V data on every second line
128
+         */
129
+        y = &frame->data[0][i * frame->linesize[0]];
130
+        u = &frame->data[1][(i >> 1) * frame->linesize[1]];
131
+        v = &frame->data[2][(i >> 1) * frame->linesize[2]];
132
+        y_end = y + frame->linesize[0] - 1;
133
+        u_end = u + frame->linesize[1] - 1;
134
+        v_end = v + frame->linesize[2] - 1;
135
+
136
+        if ((i & 1) && header[17] == SUBSAMPLE_420) {
137
+            /* We are on a odd line and 420 subsample is used.
138
+             * On this line only Y values are specified, one per pixel.
139
+             */
140
+            for (j = 0; j < linelength - 1; j++) {
141
+                if (y > y_end) {
142
+                    frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
143
+                    av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n");
144
+                    break;
145
+                }
146
+                if ((src[j] & 1) && header[28] == COMPRESSED) {
147
+                    /* It seems that odd lines are always uncompressed, but
148
+                     * we do it according to specification anyways.
149
+                     */
150
+                    skip = src[j] >> 1;
151
+                    y += skip;
152
+                } else {
153
+                    *(y++) = src[j];
154
+                }
155
+            }
156
+        } else if (header[17] == SUBSAMPLE_420) {
157
+            /* We are on an even line and 420 subsample is used.
158
+             * On this line each pair of pixels is described by four bytes.
159
+             */
160
+            for (j = 0; j < linelength - 4; ) {
161
+                if (y + 1 > y_end || u > u_end || v > v_end) {
162
+                    frame->decode_error_flags = FF_DECODE_ERROR_INVALID_BITSTREAM;
163
+                    av_log(avctx, AV_LOG_WARNING, "Decoded data exceeded linesize!\n");
164
+                    break;
165
+                }
166
+                if ((src[j] & 1) && header[28] == COMPRESSED) {
167
+                    // Skip amount of pixels and move forward one byte
168
+                    skip = src[j] >> 1;
169
+                    y += skip;
170
+                    u += skip >> 1;
171
+                    v += skip >> 1;
172
+                    j++;
173
+                } else {
174
+                    // Set image data as specified and move forward 4 bytes
175
+                    *(y++) = src[j];
176
+                    *(u++) = src[j+1];
177
+                    *(y++) = src[j+2];
178
+                    *(v++) = src[j+3];
179
+                    j += 4;
180
+                }
181
+            }
182
+        }
183
+    }
184
+
185
+    *data_size = sizeof(AVFrame);
186
+    *(AVFrame*) data = *frame;
187
+
188
+    return avpkt->size;
189
+}
190
+
191
+static av_cold int cpia_decode_init(AVCodecContext *avctx)
192
+{
193
+    // output pixel format
194
+    avctx->pix_fmt = PIX_FMT_YUV420P;
195
+
196
+    /* The default timebase set by the v4l2 demuxer leads to probing which is buggy.
197
+     * Set some reasonable time_base to skip this.
198
+     */
199
+    if (avctx->time_base.num == 1 && avctx->time_base.den == 1000000) {
200
+        avctx->time_base.num = 1;
201
+        avctx->time_base.den = 60;
202
+    }
203
+
204
+    return 0;
205
+}
206
+
207
+
208
+AVCodec ff_cpia_decoder = {
209
+    .name           = "cpia",
210
+    .type           = AVMEDIA_TYPE_VIDEO,
211
+    .id             = AV_CODEC_ID_CPIA,
212
+    .priv_data_size = sizeof(CpiaContext),
213
+    .init           = cpia_decode_init,
214
+    .decode         = cpia_decode_frame,
215
+    .capabilities   = CODEC_CAP_DR1,
216
+    .long_name      = NULL_IF_CONFIG_SMALL("CPiA video format"),
217
+};
... ...
@@ -150,6 +150,7 @@ static struct fmt_map fmt_conversion_table[] = {
150 150
     { PIX_FMT_NV12,    AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_NV12    },
151 151
     { PIX_FMT_NONE,    AV_CODEC_ID_MJPEG,    V4L2_PIX_FMT_MJPEG   },
152 152
     { PIX_FMT_NONE,    AV_CODEC_ID_MJPEG,    V4L2_PIX_FMT_JPEG    },
153
+    { PIX_FMT_NONE,    AV_CODEC_ID_CPIA,     V4L2_PIX_FMT_CPIA1   },
153 154
 };
154 155
 
155 156
 static int device_open(AVFormatContext *ctx)
... ...
@@ -549,6 +550,13 @@ static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
549 549
         return AVERROR(errno);
550 550
     }
551 551
     av_assert0(buf.index < s->buffers);
552
+
553
+    /* CPIA is a compressed format and we don't know the exact number of bytes
554
+     * used by a frame, so set it here as the driver announces it.
555
+     */
556
+    if (ctx->video_codec_id == AV_CODEC_ID_CPIA)
557
+        s->frame_size = buf.bytesused;
558
+
552 559
     if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
553 560
         av_log(ctx, AV_LOG_ERROR,
554 561
                "The v4l2 frame is %d bytes, but %d bytes are expected\n",
... ...
@@ -767,7 +775,7 @@ static int v4l2_read_header(AVFormatContext *s1)
767 767
     AVStream *st;
768 768
     int res = 0;
769 769
     uint32_t desired_format;
770
-    enum AVCodecID codec_id;
770
+    enum AVCodecID codec_id = AV_CODEC_ID_NONE;
771 771
     enum PixelFormat pix_fmt = PIX_FMT_NONE;
772 772
 
773 773
     st = avformat_new_stream(s1, NULL);
... ...
@@ -828,6 +836,14 @@ static int v4l2_read_header(AVFormatContext *s1)
828 828
 
829 829
     desired_format = device_try_init(s1, pix_fmt, &s->width, &s->height,
830 830
                                      &codec_id);
831
+
832
+    /* If no pixel_format was specified, the codec_id was not known up
833
+     * until now. Set video_codec_id in the context, as codec_id will
834
+     * not be available outside this function
835
+     */
836
+    if (codec_id != AV_CODEC_ID_NONE && s1->video_codec_id == AV_CODEC_ID_NONE)
837
+        s1->video_codec_id = codec_id;
838
+
831 839
     if (desired_format == 0) {
832 840
         av_log(s1, AV_LOG_ERROR, "Cannot find a proper format for "
833 841
                "codec_id %d, pix_fmt %d.\n", s1->video_codec_id, pix_fmt);