[FFmpeg-devel] [PATCH]v308 and yuv4 encoders and decoders
Carl Eugen Hoyos
cehoyos at ag.or.at
Fri Dec 30 00:46:26 CET 2011
Hi!
Attached implements v308 as defined in QuickTime and yuv4 which only exists in
libquicktime afaict, see ticket #470 for samples.
To be committed in two parts.
Please comment, Carl Eugen
-------------- next part --------------
diff --git a/Changelog b/Changelog
index 4ab7882..3816fd1 100644
--- a/Changelog
+++ b/Changelog
@@ -11,6 +11,8 @@ version next:
- thumbnail video filter
- XML output in ffprobe
- asplit audio filter
+- v308 Quicktime Uncompressed 4:4:4 encoder and decoder
+- yuv4 libquicktime packed 4:2:0 encoder and decoder
version 0.9:
diff --git a/doc/general.texi b/doc/general.texi
index b04d7d7..d9c9cb0 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -606,6 +606,7 @@ following image formats are supported:
@tab Codec used in DOS CD-ROM FlashBack game.
@item Ut Video @tab @tab X
@item v210 QuickTime uncompressed 4:2:2 10-bit @tab X @tab X
+ at item v308 QuickTime uncompressed 4:4:4 @tab X @tab X
@item v410 QuickTime uncompressed 4:4:4 10-bit @tab X @tab X
@item VBLE Lossless Codec @tab @tab X
@item VMware Screen Codec / VMware Video @tab @tab X
@@ -624,6 +625,8 @@ following image formats are supported:
@item WMV7 @tab X @tab X
@item YAMAHA SMAF @tab X @tab X
@item Psygnosis YOP Video @tab @tab X
+ at item yuv4 @tab X @tab X
+ @tab libquicktime uncompressed packed 4:2:0
@item ZLIB @tab X @tab X
@tab part of LCL, encoder experimental
@item Zip Motion Blocks Video @tab X @tab X
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 2b7a8c7..524aec8 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -415,6 +415,8 @@ OBJS-$(CONFIG_ULTI_DECODER) += ulti.o
OBJS-$(CONFIG_UTVIDEO_DECODER) += utvideo.o
OBJS-$(CONFIG_V210_DECODER) += v210dec.o
OBJS-$(CONFIG_V210_ENCODER) += v210enc.o
+OBJS-$(CONFIG_V308_DECODER) += v308dec.o
+OBJS-$(CONFIG_V308_ENCODER) += v308enc.o
OBJS-$(CONFIG_V410_DECODER) += v410dec.o
OBJS-$(CONFIG_V410_ENCODER) += v410enc.o
OBJS-$(CONFIG_V210X_DECODER) += v210x.o
@@ -468,6 +470,8 @@ OBJS-$(CONFIG_XL_DECODER) += xl.o
OBJS-$(CONFIG_XSUB_DECODER) += xsubdec.o
OBJS-$(CONFIG_XSUB_ENCODER) += xsubenc.o
OBJS-$(CONFIG_YOP_DECODER) += yop.o
+OBJS-$(CONFIG_YUV4_DECODER) += yuv4dec.o
+OBJS-$(CONFIG_YUV4_ENCODER) += yuv4enc.o
OBJS-$(CONFIG_ZLIB_DECODER) += lcldec.o
OBJS-$(CONFIG_ZLIB_ENCODER) += lclenc.o
OBJS-$(CONFIG_ZMBV_DECODER) += zmbv.o
diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 1f47aee..6e700b7 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -216,6 +216,7 @@ void avcodec_register_all(void)
REGISTER_DECODER (UTVIDEO, utvideo);
REGISTER_ENCDEC (V210, v210);
REGISTER_DECODER (V210X, v210x);
+ REGISTER_ENCDEC (V308, v308);
REGISTER_ENCDEC (V410, v410);
REGISTER_DECODER (VB, vb);
REGISTER_DECODER (VBLE, vble);
@@ -244,6 +245,7 @@ void avcodec_register_all(void)
REGISTER_DECODER (XAN_WC4, xan_wc4);
REGISTER_DECODER (XL, xl);
REGISTER_DECODER (YOP, yop);
+ REGISTER_ENCDEC (YUV4, yuv4);
REGISTER_ENCDEC (ZLIB, zlib);
REGISTER_ENCDEC (ZMBV, zmbv);
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 690ea38..d2915ad 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -259,6 +259,8 @@ enum CodecID {
CODEC_ID_ESCAPE130 = MKBETAG('E','1','3','0'),
CODEC_ID_G2M = MKBETAG( 0 ,'G','2','M'),
+ CODEC_ID_V308 = MKBETAG('V','3','0','8'),
+ CODEC_ID_YUV4 = MKBETAG('Y','U','V','4'),
/* various PCM "codecs" */
CODEC_ID_FIRST_AUDIO = 0x10000, ///< A dummy id pointing at the start of audio codecs
diff --git a/libavcodec/v308dec.c b/libavcodec/v308dec.c
index e69de29..5c2f86c 100644
--- a/libavcodec/v308dec.c
+++ b/libavcodec/v308dec.c
@@ -0,0 +1,105 @@
+/*
+ * v308 decoder
+ * Copyright (c) 2011 Carl Eugen Hoyos for FFmpeg
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "avcodec.h"
+
+static av_cold int v308_decode_init(AVCodecContext *avctx)
+{
+ avctx->pix_fmt = PIX_FMT_YUV444P;
+
+ avctx->coded_frame = avcodec_alloc_frame();
+
+ if (!avctx->coded_frame) {
+ av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
+ return AVERROR(ENOMEM);
+ }
+
+ return 0;
+}
+
+static int v308_decode_frame(AVCodecContext *avctx, void *data,
+ int *data_size, AVPacket *avpkt)
+{
+ AVFrame *pic = avctx->coded_frame;
+ const uint8_t *src = avpkt->data;
+ uint8_t *y, *u, *v;
+ int i, j;
+
+ if (pic->data[0])
+ avctx->release_buffer(avctx, pic);
+
+ if (avpkt->size < 3 * avctx->height * avctx->width) {
+ av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
+ return AVERROR(EINVAL);
+ }
+
+ pic->reference = 0;
+
+ if (avctx->get_buffer(avctx, pic) < 0) {
+ av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
+ return AVERROR(ENOMEM);
+ }
+
+ pic->key_frame = 1;
+ pic->pict_type = FF_I_TYPE;
+
+ y = pic->data[0];
+ u = pic->data[1];
+ v = pic->data[2];
+
+ for (i = 0; i < avctx->height; i++) {
+ for (j = 0; j < avctx->width; j++) {
+ v[j] = *src++;
+ y[j] = *src++;
+ u[j] = *src++;
+ }
+
+ y += pic->linesize[0];
+ u += pic->linesize[1];
+ v += pic->linesize[2];
+ }
+
+ *data_size = sizeof(AVFrame);
+ *(AVFrame *)data = *pic;
+
+ return avpkt->size;
+}
+
+static av_cold int v308_decode_close(AVCodecContext *avctx)
+{
+ if (avctx->coded_frame->data[0])
+ avctx->release_buffer(avctx, avctx->coded_frame);
+
+ av_freep(&avctx->coded_frame);
+
+ return 0;
+}
+
+AVCodec ff_v308_decoder = {
+ .name = "v308",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .id = CODEC_ID_V308,
+ .init = v308_decode_init,
+ .decode = v308_decode_frame,
+ .close = v308_decode_close,
+ .capabilities = CODEC_CAP_DR1,
+ .long_name = NULL_IF_CONFIG_SMALL("Uncompressed packed 4:4:4"),
+};
diff --git a/libavcodec/v308enc.c b/libavcodec/v308enc.c
index e69de29..bbb4dc7 100644
--- a/libavcodec/v308enc.c
+++ b/libavcodec/v308enc.c
@@ -0,0 +1,95 @@
+/*
+ * v308 encoder
+ *
+ * Copyright (c) 2011 Carl Eugen Hoyos for FFmpeg
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/intreadwrite.h"
+#include "avcodec.h"
+
+static av_cold int v308_encode_init(AVCodecContext *avctx)
+{
+ if (avctx->width & 1) {
+ av_log(avctx, AV_LOG_ERROR, "v308 requires width to be even.\n");
+ return AVERROR_INVALIDDATA;
+ }
+
+ avctx->coded_frame = avcodec_alloc_frame();
+
+ if (!avctx->coded_frame) {
+ av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
+ return AVERROR(ENOMEM);
+ }
+
+ return 0;
+}
+
+static int v308_encode_frame(AVCodecContext *avctx, uint8_t *buf,
+ int buf_size, void *data)
+{
+ AVFrame *pic = data;
+ uint8_t *dst = buf;
+ uint8_t *y, *u, *v;
+ int i, j;
+ int output_size = 0;
+
+ if (buf_size < avctx->width * avctx->height * 3) {
+ av_log(avctx, AV_LOG_ERROR, "Out buffer is too small.\n");
+ return AVERROR(ENOMEM);
+ }
+
+ avctx->coded_frame->reference = 0;
+ avctx->coded_frame->key_frame = 1;
+ avctx->coded_frame->pict_type = FF_I_TYPE;
+
+ y = pic->data[0];
+ u = pic->data[1];
+ v = pic->data[2];
+
+ for (i = 0; i < avctx->height; i++) {
+ for (j = 0; j < avctx->width; j++) {
+ AV_WB24(dst, v[j] << 16 | y[j] << 8 | u[j]);
+ dst += 3;
+ output_size += 3;
+ }
+ y += pic->linesize[0];
+ u += pic->linesize[1];
+ v += pic->linesize[2];
+ }
+
+ return output_size;
+}
+
+static av_cold int v308_encode_close(AVCodecContext *avctx)
+{
+ av_freep(&avctx->coded_frame);
+
+ return 0;
+}
+
+AVCodec ff_v308_encoder = {
+ .name = "v308",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .id = CODEC_ID_V308,
+ .init = v308_encode_init,
+ .encode = v308_encode_frame,
+ .close = v308_encode_close,
+ .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_YUV444P, PIX_FMT_NONE },
+ .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:4:4"),
+};
diff --git a/libavcodec/yuv4dec.c b/libavcodec/yuv4dec.c
index e69de29..e998a65 100644
--- a/libavcodec/yuv4dec.c
+++ b/libavcodec/yuv4dec.c
@@ -0,0 +1,108 @@
+/*
+ * yuv4 decoder
+ * Copyright (c) 2011 Carl Eugen Hoyos for FFmpeg
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "avcodec.h"
+
+static av_cold int yuv4_decode_init(AVCodecContext *avctx)
+{
+ avctx->pix_fmt = PIX_FMT_YUV420P;
+
+ avctx->coded_frame = avcodec_alloc_frame();
+
+ if (!avctx->coded_frame) {
+ av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
+ return AVERROR(ENOMEM);
+ }
+
+ return 0;
+}
+
+static int yuv4_decode_frame(AVCodecContext *avctx, void *data,
+ int *data_size, AVPacket *avpkt)
+{
+ AVFrame *pic = avctx->coded_frame;
+ const uint8_t *src = avpkt->data;
+ uint8_t *y, *u, *v;
+ int i, j;
+
+ if (pic->data[0])
+ avctx->release_buffer(avctx, pic);
+
+ if (avpkt->size < 3 * (avctx->width + 1 >> 1) * (avctx->height + 1 >> 1)) {
+ av_log(avctx, AV_LOG_ERROR, "Insufficient input data.\n");
+ return AVERROR(EINVAL);
+ }
+
+ pic->reference = 0;
+
+ if (avctx->get_buffer(avctx, pic) < 0) {
+ av_log(avctx, AV_LOG_ERROR, "Could not allocate buffer.\n");
+ return AVERROR(ENOMEM);
+ }
+
+ pic->key_frame = 1;
+ pic->pict_type = FF_I_TYPE;
+
+ y = pic->data[0];
+ u = pic->data[1];
+ v = pic->data[2];
+
+ for (i = 0; i < (avctx->height + 1) >> 1; i++) {
+ for (j = 0; j < (avctx->width + 1) >> 1; j++) {
+ u[j] = *src++ ^ 0x80;
+ v[j] = *src++ ^ 0x80;
+ y[2 * j] = *src++;
+ y[2 * j + 1] = *src++;
+ y[pic->linesize[0] + 2 * j] = *src++;
+ y[pic->linesize[0] + 2 * j + 1] = *src++;
+ }
+
+ y += 2 * pic->linesize[0];
+ u += pic->linesize[1];
+ v += pic->linesize[2];
+ }
+
+ *data_size = sizeof(AVFrame);
+ *(AVFrame *)data = *pic;
+
+ return avpkt->size;
+}
+
+static av_cold int yuv4_decode_close(AVCodecContext *avctx)
+{
+ if (avctx->coded_frame->data[0])
+ avctx->release_buffer(avctx, avctx->coded_frame);
+
+ av_freep(&avctx->coded_frame);
+
+ return 0;
+}
+
+AVCodec ff_yuv4_decoder = {
+ .name = "yuv4",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .id = CODEC_ID_YUV4,
+ .init = yuv4_decode_init,
+ .decode = yuv4_decode_frame,
+ .close = yuv4_decode_close,
+ .capabilities = CODEC_CAP_DR1,
+ .long_name = NULL_IF_CONFIG_SMALL("Uncompressed packed 4:2:0"),
+};
diff --git a/libavcodec/yuv4enc.c b/libavcodec/yuv4enc.c
index e69de29..52bb1c9 100644
--- a/libavcodec/yuv4enc.c
+++ b/libavcodec/yuv4enc.c
@@ -0,0 +1,93 @@
+/*
+ * yuv4 encoder
+ *
+ * Copyright (c) 2011 Carl Eugen Hoyos for FFmpeg
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "avcodec.h"
+
+static av_cold int yuv4_encode_init(AVCodecContext *avctx)
+{
+ avctx->coded_frame = avcodec_alloc_frame();
+
+ if (!avctx->coded_frame) {
+ av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
+ return AVERROR(ENOMEM);
+ }
+
+ return 0;
+}
+
+static int yuv4_encode_frame(AVCodecContext *avctx, uint8_t *buf,
+ int buf_size, void *data)
+{
+ AVFrame *pic = data;
+ uint8_t *dst = buf;
+ uint8_t *y, *u, *v;
+ int i, j;
+ int output_size = 0;
+
+ if (buf_size < (avctx->width + 1 >> 1) * (avctx->height + 1 >> 1) * 3 / 2) {
+ av_log(avctx, AV_LOG_ERROR, "Out buffer is too small.\n");
+ return AVERROR(ENOMEM);
+ }
+
+ avctx->coded_frame->reference = 0;
+ avctx->coded_frame->key_frame = 1;
+ avctx->coded_frame->pict_type = FF_I_TYPE;
+
+ y = pic->data[0];
+ u = pic->data[1];
+ v = pic->data[2];
+
+ for (i = 0; i < avctx->height + 1 >> 1; i++) {
+ for (j = 0; j < avctx->width + 1 >> 1; j++) {
+ *dst++ = u[j] ^ 0x80;
+ *dst++ = v[j] ^ 0x80;
+ *dst++ = y[2 * j];
+ *dst++ = y[2 * j + 1];
+ *dst++ = y[pic->linesize[0] + 2 * j];
+ *dst++ = y[pic->linesize[0] + 2 * j + 1];
+ output_size += 6;
+ }
+ y += 2 * pic->linesize[0];
+ u += pic->linesize[1];
+ v += pic->linesize[2];
+ }
+
+ return output_size;
+}
+
+static av_cold int yuv4_encode_close(AVCodecContext *avctx)
+{
+ av_freep(&avctx->coded_frame);
+
+ return 0;
+}
+
+AVCodec ff_yuv4_encoder = {
+ .name = "yuv4",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .id = CODEC_ID_YUV4,
+ .init = yuv4_encode_init,
+ .encode = yuv4_encode_frame,
+ .close = yuv4_encode_close,
+ .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_YUV420P, PIX_FMT_NONE },
+ .long_name = NULL_IF_CONFIG_SMALL("Packed 4:2:0"),
+};
diff --git a/libavformat/isom.c b/libavformat/isom.c
index 0a01e78..7dd7885 100644
--- a/libavformat/isom.c
+++ b/libavformat/isom.c
@@ -90,7 +90,9 @@ const AVCodecTag codec_movvideo_tags[] = {
{ CODEC_ID_R10K, MKTAG('R', '1', '0', 'g') }, /* UNCOMPRESSED 10BIT RGB */
{ CODEC_ID_R210, MKTAG('r', '2', '1', '0') }, /* UNCOMPRESSED 10BIT RGB */
{ CODEC_ID_V210, MKTAG('v', '2', '1', '0') }, /* UNCOMPRESSED 10BIT 4:2:2 */
+ { CODEC_ID_V308, MKTAG('v', '3', '0', '8') }, /* UNCOMPRESSED 4:4:4 */
{ CODEC_ID_V410, MKTAG('v', '4', '1', '0') }, /* UNCOMPRESSED 10BIT 4:4:4 */
+ { CODEC_ID_YUV4, MKTAG('y', 'u', 'v', '4') }, /* packed yuv420p from libquicktime */
{ CODEC_ID_MJPEG, MKTAG('j', 'p', 'e', 'g') }, /* PhotoJPEG */
{ CODEC_ID_MJPEG, MKTAG('m', 'j', 'p', 'a') }, /* Motion-JPEG (format A) */
diff --git a/libavformat/riff.c b/libavformat/riff.c
index 4f475ba..d311f3a 100644
--- a/libavformat/riff.c
+++ b/libavformat/riff.c
@@ -290,6 +290,8 @@ const AVCodecTag ff_codec_bmp_tags[] = {
{ CODEC_ID_VBLE, MKTAG('V', 'B', 'L', 'E') },
{ CODEC_ID_ESCAPE130, MKTAG('E', '1', '3', '0') },
{ CODEC_ID_DXTORY, MKTAG('x', 't', 'o', 'r') },
+ { CODEC_ID_V308, MKTAG('v', '3', '0', '8') },
+ { CODEC_ID_YUV4, MKTAG('y', 'u', 'v', '4') },
{ CODEC_ID_NONE, 0 }
};
More information about the ffmpeg-devel
mailing list