Browse code

yuv4 libquicktime packed 4:2:0 encoder and decoder.

Reviewed-by: Derek Buitenhuis
Reviewed-by: Paul B Mahol

Carl Eugen Hoyos authored on 2012/01/04 23:35:15
Showing 10 changed files
... ...
@@ -18,6 +18,7 @@ version next:
18 18
 - ffprobe -show_error option
19 19
 - Avid 1:1 10-bit RGB Packer decoder
20 20
 - v308 Quicktime Uncompressed 4:4:4 encoder and decoder
21
+- yuv4 libquicktime packed 4:2:0 encoder and decoder
21 22
 
22 23
 
23 24
 version 0.9:
... ...
@@ -628,6 +628,8 @@ following image formats are supported:
628 628
 @item WMV7                   @tab  X  @tab  X
629 629
 @item YAMAHA SMAF            @tab  X  @tab  X
630 630
 @item Psygnosis YOP Video    @tab     @tab  X
631
+@item yuv4                   @tab  X  @tab  X
632
+    @tab libquicktime uncompressed packed 4:2:0
631 633
 @item ZLIB                   @tab  X  @tab  X
632 634
     @tab part of LCL, encoder experimental
633 635
 @item Zip Motion Blocks Video  @tab   X @tab  X
... ...
@@ -473,6 +473,8 @@ OBJS-$(CONFIG_XSUB_ENCODER)            += xsubenc.o
473 473
 OBJS-$(CONFIG_Y41P_DECODER)            += y41pdec.o
474 474
 OBJS-$(CONFIG_Y41P_ENCODER)            += y41penc.o
475 475
 OBJS-$(CONFIG_YOP_DECODER)             += yop.o
476
+OBJS-$(CONFIG_YUV4_DECODER)            += yuv4dec.o
477
+OBJS-$(CONFIG_YUV4_ENCODER)            += yuv4enc.o
476 478
 OBJS-$(CONFIG_ZLIB_DECODER)            += lcldec.o
477 479
 OBJS-$(CONFIG_ZLIB_ENCODER)            += lclenc.o
478 480
 OBJS-$(CONFIG_ZMBV_DECODER)            += zmbv.o
... ...
@@ -247,6 +247,7 @@ void avcodec_register_all(void)
247 247
     REGISTER_DECODER (XL, xl);
248 248
     REGISTER_ENCDEC  (Y41P, y41p);
249 249
     REGISTER_DECODER (YOP, yop);
250
+    REGISTER_ENCDEC  (YUV4, yuv4);
250 251
     REGISTER_ENCDEC  (ZLIB, zlib);
251 252
     REGISTER_ENCDEC  (ZMBV, zmbv);
252 253
 
... ...
@@ -262,6 +262,7 @@ enum CodecID {
262 262
 
263 263
     CODEC_ID_G2M        = MKBETAG( 0 ,'G','2','M'),
264 264
     CODEC_ID_V308       = MKBETAG('V','3','0','8'),
265
+    CODEC_ID_YUV4       = MKBETAG('Y','U','V','4'),
265 266
 
266 267
     /* various PCM "codecs" */
267 268
     CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
... ...
@@ -21,7 +21,7 @@
21 21
 #define AVCODEC_VERSION_H
22 22
 
23 23
 #define LIBAVCODEC_VERSION_MAJOR 53
24
-#define LIBAVCODEC_VERSION_MINOR 52
24
+#define LIBAVCODEC_VERSION_MINOR 53
25 25
 #define LIBAVCODEC_VERSION_MICRO 100
26 26
 
27 27
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
28 28
new file mode 100644
... ...
@@ -0,0 +1,109 @@
0
+/*
1
+ * libquicktime yuv4 decoder
2
+ *
3
+ * Copyright (c) 2011 Carl Eugen Hoyos
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#include "avcodec.h"
23
+
24
+static av_cold int yuv4_decode_init(AVCodecContext *avctx)
25
+{
26
+    avctx->pix_fmt = PIX_FMT_YUV420P;
27
+
28
+    avctx->coded_frame = avcodec_alloc_frame();
29
+
30
+    if (!avctx->coded_frame) {
31
+        av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
32
+        return AVERROR(ENOMEM);
33
+    }
34
+
35
+    return 0;
36
+}
37
+
38
+static int yuv4_decode_frame(AVCodecContext *avctx, void *data,
39
+                             int *data_size, AVPacket *avpkt)
40
+{
41
+    AVFrame *pic = avctx->coded_frame;
42
+    const uint8_t *src = avpkt->data;
43
+    uint8_t *y, *u, *v;
44
+    int i, j;
45
+
46
+    if (pic->data[0])
47
+        avctx->release_buffer(avctx, pic);
48
+
49
+    if (avpkt->size < 6 * (avctx->width + 1 >> 1) * (avctx->height + 1 >> 1)) {
50
+        av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
51
+        return AVERROR(EINVAL);
52
+    }
53
+
54
+    pic->reference = 0;
55
+
56
+    if (avctx->get_buffer(avctx, pic) < 0) {
57
+        av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
58
+        return AVERROR(ENOMEM);
59
+    }
60
+
61
+    pic->key_frame = 1;
62
+    pic->pict_type = FF_I_TYPE;
63
+
64
+    y = pic->data[0];
65
+    u = pic->data[1];
66
+    v = pic->data[2];
67
+
68
+    for (i = 0; i < (avctx->height + 1) >> 1; i++) {
69
+        for (j = 0; j < (avctx->width + 1) >> 1; j++) {
70
+            u[j] = *src++ ^ 0x80;
71
+            v[j] = *src++ ^ 0x80;
72
+            y[                   2 * j    ] = *src++;
73
+            y[                   2 * j + 1] = *src++;
74
+            y[pic->linesize[0] + 2 * j    ] = *src++;
75
+            y[pic->linesize[0] + 2 * j + 1] = *src++;
76
+        }
77
+
78
+        y += 2 * pic->linesize[0];
79
+        u +=     pic->linesize[1];
80
+        v +=     pic->linesize[2];
81
+    }
82
+
83
+    *data_size = sizeof(AVFrame);
84
+    *(AVFrame *)data = *pic;
85
+
86
+    return avpkt->size;
87
+}
88
+
89
+static av_cold int yuv4_decode_close(AVCodecContext *avctx)
90
+{
91
+    if (avctx->coded_frame->data[0])
92
+        avctx->release_buffer(avctx, avctx->coded_frame);
93
+
94
+    av_freep(&avctx->coded_frame);
95
+
96
+    return 0;
97
+}
98
+
99
+AVCodec ff_yuv4_decoder = {
100
+    .name         = "yuv4",
101
+    .type         = AVMEDIA_TYPE_VIDEO,
102
+    .id           = CODEC_ID_YUV4,
103
+    .init         = yuv4_decode_init,
104
+    .decode       = yuv4_decode_frame,
105
+    .close        = yuv4_decode_close,
106
+    .capabilities = CODEC_CAP_DR1,
107
+    .long_name    = NULL_IF_CONFIG_SMALL("Uncompressed packed 4:2:0"),
108
+};
0 109
new file mode 100644
... ...
@@ -0,0 +1,93 @@
0
+/*
1
+ * libquicktime yuv4 encoder
2
+ *
3
+ * Copyright (c) 2011 Carl Eugen Hoyos
4
+ *
5
+ * This file is part of FFmpeg.
6
+ *
7
+ * FFmpeg is free software; you can redistribute it and/or
8
+ * modify it under the terms of the GNU Lesser General Public
9
+ * License as published by the Free Software Foundation; either
10
+ * version 2.1 of the License, or (at your option) any later version.
11
+ *
12
+ * FFmpeg is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
+ * Lesser General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU Lesser General Public
18
+ * License along with FFmpeg; if not, write to the Free Software
19
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ */
21
+
22
+#include "avcodec.h"
23
+
24
+static av_cold int yuv4_encode_init(AVCodecContext *avctx)
25
+{
26
+    avctx->coded_frame = avcodec_alloc_frame();
27
+
28
+    if (!avctx->coded_frame) {
29
+        av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
30
+        return AVERROR(ENOMEM);
31
+    }
32
+
33
+    return 0;
34
+}
35
+
36
+static int yuv4_encode_frame(AVCodecContext *avctx, uint8_t *buf,
37
+                             int buf_size, void *data)
38
+{
39
+    AVFrame *pic = data;
40
+    uint8_t *dst = buf;
41
+    uint8_t *y, *u, *v;
42
+    int i, j;
43
+    int output_size = 0;
44
+
45
+    if (buf_size < 6 * (avctx->width + 1 >> 1) * (avctx->height + 1 >> 1)) {
46
+        av_log(avctx, AV_LOG_ERROR, "Out buffer is too small.\n");
47
+        return AVERROR(ENOMEM);
48
+    }
49
+
50
+    avctx->coded_frame->reference = 0;
51
+    avctx->coded_frame->key_frame = 1;
52
+    avctx->coded_frame->pict_type = FF_I_TYPE;
53
+
54
+    y = pic->data[0];
55
+    u = pic->data[1];
56
+    v = pic->data[2];
57
+
58
+    for (i = 0; i < avctx->height + 1 >> 1; i++) {
59
+        for (j = 0; j < avctx->width + 1 >> 1; j++) {
60
+            *dst++ = u[j] ^ 0x80;
61
+            *dst++ = v[j] ^ 0x80;
62
+            *dst++ = y[                   2 * j    ];
63
+            *dst++ = y[                   2 * j + 1];
64
+            *dst++ = y[pic->linesize[0] + 2 * j    ];
65
+            *dst++ = y[pic->linesize[0] + 2 * j + 1];
66
+            output_size += 6;
67
+        }
68
+        y += 2 * pic->linesize[0];
69
+        u +=     pic->linesize[1];
70
+        v +=     pic->linesize[2];
71
+    }
72
+
73
+    return output_size;
74
+}
75
+
76
+static av_cold int yuv4_encode_close(AVCodecContext *avctx)
77
+{
78
+    av_freep(&avctx->coded_frame);
79
+
80
+    return 0;
81
+}
82
+
83
+AVCodec ff_yuv4_encoder = {
84
+    .name         = "yuv4",
85
+    .type         = AVMEDIA_TYPE_VIDEO,
86
+    .id           = CODEC_ID_YUV4,
87
+    .init         = yuv4_encode_init,
88
+    .encode       = yuv4_encode_frame,
89
+    .close        = yuv4_encode_close,
90
+    .pix_fmts     = (const enum PixelFormat[]){ PIX_FMT_YUV420P, PIX_FMT_NONE },
91
+    .long_name    = NULL_IF_CONFIG_SMALL("Uncompressed packed 4:2:0"),
92
+};
... ...
@@ -94,6 +94,7 @@ const AVCodecTag codec_movvideo_tags[] = {
94 94
     { CODEC_ID_V308,   MKTAG('v', '3', '0', '8') }, /* UNCOMPRESSED 4:4:4 */
95 95
     { CODEC_ID_V410,   MKTAG('v', '4', '1', '0') }, /* UNCOMPRESSED 10BIT 4:4:4 */
96 96
     { CODEC_ID_Y41P,   MKTAG('Y', '4', '1', 'P') }, /* UNCOMPRESSED 12BIT 4:1:1 */
97
+    { CODEC_ID_YUV4,   MKTAG('y', 'u', 'v', '4') }, /* libquicktime packed yuv420p */
97 98
 
98 99
     { CODEC_ID_MJPEG,  MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
99 100
     { CODEC_ID_MJPEG,  MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
... ...
@@ -201,6 +201,7 @@ const AVCodecTag ff_codec_bmp_tags[] = {
201 201
     { CODEC_ID_V210,         MKTAG('v', '2', '1', '0') },
202 202
     { CODEC_ID_V308,         MKTAG('v', '3', '0', '8') },
203 203
     { CODEC_ID_V410,         MKTAG('v', '4', '1', '0') },
204
+    { CODEC_ID_YUV4,         MKTAG('y', 'u', 'v', '4') },
204 205
     { CODEC_ID_INDEO3,       MKTAG('I', 'V', '3', '1') },
205 206
     { CODEC_ID_INDEO3,       MKTAG('I', 'V', '3', '2') },
206 207
     { CODEC_ID_INDEO4,       MKTAG('I', 'V', '4', '1') },