annotate ffmpeg/libavcodec/libutvideoenc.cpp @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Copyright (c) 2012 Derek Buitenhuis
yading@10 3 *
yading@10 4 * This file is part of FFmpeg.
yading@10 5 *
yading@10 6 * FFmpeg is free software; you can redistribute it and/or
yading@10 7 * modify it under the terms of the GNU General Public
yading@10 8 * License as published by the Free Software Foundation;
yading@10 9 * version 2 of the License.
yading@10 10 *
yading@10 11 * FFmpeg is distributed in the hope that it will be useful,
yading@10 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 14 * General Public License for more details.
yading@10 15 *
yading@10 16 * You should have received a copy of the GNU General Public
yading@10 17 * License along with FFmpeg; if not, write to the Free Software
yading@10 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 19 */
yading@10 20
yading@10 21 /**
yading@10 22 * @file
yading@10 23 * Known FOURCCs:
yading@10 24 * 'ULY0' (YCbCr 4:2:0), 'ULY2' (YCbCr 4:2:2), 'ULRG' (RGB), 'ULRA' (RGBA)
yading@10 25 */
yading@10 26
yading@10 27 extern "C" {
yading@10 28 #include "libavutil/avassert.h"
yading@10 29 #include "avcodec.h"
yading@10 30 #include "internal.h"
yading@10 31 }
yading@10 32
yading@10 33 #include "libutvideo.h"
yading@10 34 #include "put_bits.h"
yading@10 35
yading@10 36 static av_cold int utvideo_encode_init(AVCodecContext *avctx)
yading@10 37 {
yading@10 38 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
yading@10 39 UtVideoExtra *info;
yading@10 40 uint32_t flags, in_format;
yading@10 41
yading@10 42 switch (avctx->pix_fmt) {
yading@10 43 case AV_PIX_FMT_YUV420P:
yading@10 44 in_format = UTVF_YV12;
yading@10 45 avctx->bits_per_coded_sample = 12;
yading@10 46 avctx->codec_tag = MKTAG('U', 'L', 'Y', '0');
yading@10 47 break;
yading@10 48 case AV_PIX_FMT_YUYV422:
yading@10 49 in_format = UTVF_YUYV;
yading@10 50 avctx->bits_per_coded_sample = 16;
yading@10 51 avctx->codec_tag = MKTAG('U', 'L', 'Y', '2');
yading@10 52 break;
yading@10 53 case AV_PIX_FMT_BGR24:
yading@10 54 in_format = UTVF_NFCC_BGR_BU;
yading@10 55 avctx->bits_per_coded_sample = 24;
yading@10 56 avctx->codec_tag = MKTAG('U', 'L', 'R', 'G');
yading@10 57 break;
yading@10 58 case AV_PIX_FMT_RGB32:
yading@10 59 in_format = UTVF_NFCC_BGRA_BU;
yading@10 60 avctx->bits_per_coded_sample = 32;
yading@10 61 avctx->codec_tag = MKTAG('U', 'L', 'R', 'A');
yading@10 62 break;
yading@10 63 default:
yading@10 64 return AVERROR(EINVAL);
yading@10 65 }
yading@10 66
yading@10 67 /* Check before we alloc anything */
yading@10 68 if (avctx->prediction_method != 0 && avctx->prediction_method != 2) {
yading@10 69 av_log(avctx, AV_LOG_ERROR, "Invalid prediction method.\n");
yading@10 70 return AVERROR(EINVAL);
yading@10 71 }
yading@10 72
yading@10 73 flags = ((avctx->prediction_method + 1) << 8) | (avctx->thread_count - 1);
yading@10 74
yading@10 75 avctx->priv_data = utv;
yading@10 76 avctx->coded_frame = avcodec_alloc_frame();
yading@10 77
yading@10 78 /* Alloc extradata buffer */
yading@10 79 info = (UtVideoExtra *)av_malloc(sizeof(*info));
yading@10 80
yading@10 81 if (info == NULL) {
yading@10 82 av_log(avctx, AV_LOG_ERROR, "Could not allocate extradata buffer.\n");
yading@10 83 return AVERROR(ENOMEM);
yading@10 84 }
yading@10 85
yading@10 86 /*
yading@10 87 * We use this buffer to hold the data that Ut Video returns,
yading@10 88 * since we cannot decode planes separately with it.
yading@10 89 */
yading@10 90 utv->buf_size = avpicture_get_size(avctx->pix_fmt,
yading@10 91 avctx->width, avctx->height);
yading@10 92 utv->buffer = (uint8_t *)av_malloc(utv->buf_size);
yading@10 93
yading@10 94 if (utv->buffer == NULL) {
yading@10 95 av_log(avctx, AV_LOG_ERROR, "Could not allocate output buffer.\n");
yading@10 96 return AVERROR(ENOMEM);
yading@10 97 }
yading@10 98
yading@10 99 /*
yading@10 100 * Create a Ut Video instance. Since the function wants
yading@10 101 * an "interface name" string, pass it the name of the lib.
yading@10 102 */
yading@10 103 utv->codec = CCodec::CreateInstance(UNFCC(avctx->codec_tag), "libavcodec");
yading@10 104
yading@10 105 /* Initialize encoder */
yading@10 106 utv->codec->EncodeBegin(in_format, avctx->width, avctx->height,
yading@10 107 CBGROSSWIDTH_WINDOWS);
yading@10 108
yading@10 109 /* Get extradata from encoder */
yading@10 110 avctx->extradata_size = utv->codec->EncodeGetExtraDataSize();
yading@10 111 utv->codec->EncodeGetExtraData(info, avctx->extradata_size, in_format,
yading@10 112 avctx->width, avctx->height,
yading@10 113 CBGROSSWIDTH_WINDOWS);
yading@10 114 avctx->extradata = (uint8_t *)info;
yading@10 115
yading@10 116 /* Set flags */
yading@10 117 utv->codec->SetState(&flags, sizeof(flags));
yading@10 118
yading@10 119 return 0;
yading@10 120 }
yading@10 121
yading@10 122 static int utvideo_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
yading@10 123 const AVFrame *pic, int *got_packet)
yading@10 124 {
yading@10 125 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
yading@10 126 int w = avctx->width, h = avctx->height;
yading@10 127 int ret, rgb_size, i;
yading@10 128 bool keyframe;
yading@10 129 uint8_t *y, *u, *v;
yading@10 130 uint8_t *dst;
yading@10 131
yading@10 132 /* Alloc buffer */
yading@10 133 if ((ret = ff_alloc_packet2(avctx, pkt, utv->buf_size)) < 0)
yading@10 134 return ret;
yading@10 135
yading@10 136 dst = pkt->data;
yading@10 137
yading@10 138 /* Move input if needed data into Ut Video friendly buffer */
yading@10 139 switch (avctx->pix_fmt) {
yading@10 140 case AV_PIX_FMT_YUV420P:
yading@10 141 y = utv->buffer;
yading@10 142 u = y + w * h;
yading@10 143 v = u + w * h / 4;
yading@10 144 for (i = 0; i < h; i++) {
yading@10 145 memcpy(y, pic->data[0] + i * pic->linesize[0], w);
yading@10 146 y += w;
yading@10 147 }
yading@10 148 for (i = 0; i < h / 2; i++) {
yading@10 149 memcpy(u, pic->data[2] + i * pic->linesize[2], w >> 1);
yading@10 150 memcpy(v, pic->data[1] + i * pic->linesize[1], w >> 1);
yading@10 151 u += w >> 1;
yading@10 152 v += w >> 1;
yading@10 153 }
yading@10 154 break;
yading@10 155 case AV_PIX_FMT_YUYV422:
yading@10 156 for (i = 0; i < h; i++)
yading@10 157 memcpy(utv->buffer + i * (w << 1),
yading@10 158 pic->data[0] + i * pic->linesize[0], w << 1);
yading@10 159 break;
yading@10 160 case AV_PIX_FMT_BGR24:
yading@10 161 case AV_PIX_FMT_RGB32:
yading@10 162 /* Ut Video takes bottom-up BGR */
yading@10 163 rgb_size = avctx->pix_fmt == AV_PIX_FMT_BGR24 ? 3 : 4;
yading@10 164 for (i = 0; i < h; i++)
yading@10 165 memcpy(utv->buffer + (h - i - 1) * w * rgb_size,
yading@10 166 pic->data[0] + i * pic->linesize[0],
yading@10 167 w * rgb_size);
yading@10 168 break;
yading@10 169 default:
yading@10 170 return AVERROR(EINVAL);
yading@10 171 }
yading@10 172
yading@10 173 /* Encode frame */
yading@10 174 pkt->size = utv->codec->EncodeFrame(dst, &keyframe, utv->buffer);
yading@10 175
yading@10 176 if (!pkt->size) {
yading@10 177 av_log(avctx, AV_LOG_ERROR, "EncodeFrame failed!\n");
yading@10 178 return AVERROR_INVALIDDATA;
yading@10 179 }
yading@10 180
yading@10 181 /*
yading@10 182 * Ut Video is intra-only and every frame is a keyframe,
yading@10 183 * and the API always returns true. In case something
yading@10 184 * durastic changes in the future, such as inter support,
yading@10 185 * assert that this is true.
yading@10 186 */
yading@10 187 av_assert2(keyframe == true);
yading@10 188 avctx->coded_frame->key_frame = 1;
yading@10 189 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
yading@10 190
yading@10 191 pkt->flags |= AV_PKT_FLAG_KEY;
yading@10 192 *got_packet = 1;
yading@10 193 return 0;
yading@10 194 }
yading@10 195
yading@10 196 static av_cold int utvideo_encode_close(AVCodecContext *avctx)
yading@10 197 {
yading@10 198 UtVideoContext *utv = (UtVideoContext *)avctx->priv_data;
yading@10 199
yading@10 200 av_freep(&avctx->coded_frame);
yading@10 201 av_freep(&avctx->extradata);
yading@10 202 av_freep(&utv->buffer);
yading@10 203
yading@10 204 utv->codec->EncodeEnd();
yading@10 205 CCodec::DeleteInstance(utv->codec);
yading@10 206
yading@10 207 return 0;
yading@10 208 }
yading@10 209
yading@10 210 AVCodec ff_libutvideo_encoder = {
yading@10 211 "libutvideo",
yading@10 212 NULL_IF_CONFIG_SMALL("Ut Video"),
yading@10 213 AVMEDIA_TYPE_VIDEO,
yading@10 214 AV_CODEC_ID_UTVIDEO,
yading@10 215 CODEC_CAP_AUTO_THREADS | CODEC_CAP_LOSSLESS,
yading@10 216 NULL, /* supported_framerates */
yading@10 217 (const enum AVPixelFormat[]) {
yading@10 218 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUYV422, AV_PIX_FMT_BGR24,
yading@10 219 AV_PIX_FMT_RGB32, AV_PIX_FMT_NONE
yading@10 220 },
yading@10 221 NULL, /* supported_samplerates */
yading@10 222 NULL, /* sample_fmts */
yading@10 223 NULL, /* channel_layouts */
yading@10 224 0, /* max_lowres */
yading@10 225 NULL, /* priv_class */
yading@10 226 NULL, /* profiles */
yading@10 227 sizeof(UtVideoContext),
yading@10 228 NULL, /* next */
yading@10 229 NULL, /* init_thread_copy */
yading@10 230 NULL, /* update_thread_context */
yading@10 231 NULL, /* defaults */
yading@10 232 NULL, /* init_static_data */
yading@10 233 utvideo_encode_init,
yading@10 234 NULL, /* encode */
yading@10 235 utvideo_encode_frame,
yading@10 236 NULL, /* decode */
yading@10 237 utvideo_encode_close,
yading@10 238 NULL, /* flush */
yading@10 239 };