annotate ffmpeg/libavcodec/rawdec.c @ 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 * Raw Video Decoder
yading@10 3 * Copyright (c) 2001 Fabrice Bellard
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 /**
yading@10 23 * @file
yading@10 24 * Raw Video Decoder
yading@10 25 */
yading@10 26
yading@10 27 #include "avcodec.h"
yading@10 28 #include "raw.h"
yading@10 29 #include "libavutil/avassert.h"
yading@10 30 #include "libavutil/buffer.h"
yading@10 31 #include "libavutil/common.h"
yading@10 32 #include "libavutil/intreadwrite.h"
yading@10 33 #include "libavutil/imgutils.h"
yading@10 34 #include "libavutil/opt.h"
yading@10 35
yading@10 36 typedef struct RawVideoContext {
yading@10 37 AVClass *av_class;
yading@10 38 AVBufferRef *palette;
yading@10 39 int frame_size; /* size of the frame in bytes */
yading@10 40 int flip;
yading@10 41 int is_2_4_bpp; // 2 or 4 bpp raw in avi/mov
yading@10 42 int is_yuv2;
yading@10 43 int tff;
yading@10 44 } RawVideoContext;
yading@10 45
yading@10 46 static const AVOption options[]={
yading@10 47 {"top", "top field first", offsetof(RawVideoContext, tff), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_VIDEO_PARAM},
yading@10 48 {NULL}
yading@10 49 };
yading@10 50
yading@10 51 static const AVClass class = {
yading@10 52 .class_name = "rawdec",
yading@10 53 .option = options,
yading@10 54 .version = LIBAVUTIL_VERSION_INT,
yading@10 55 };
yading@10 56
yading@10 57 static const PixelFormatTag pix_fmt_bps_avi[] = {
yading@10 58 { AV_PIX_FMT_MONOWHITE, 1 },
yading@10 59 { AV_PIX_FMT_PAL8, 2 },
yading@10 60 { AV_PIX_FMT_PAL8, 4 },
yading@10 61 { AV_PIX_FMT_PAL8, 8 },
yading@10 62 { AV_PIX_FMT_RGB444LE, 12 },
yading@10 63 { AV_PIX_FMT_RGB555LE, 15 },
yading@10 64 { AV_PIX_FMT_RGB555LE, 16 },
yading@10 65 { AV_PIX_FMT_BGR24, 24 },
yading@10 66 { AV_PIX_FMT_BGRA, 32 },
yading@10 67 { AV_PIX_FMT_NONE, 0 },
yading@10 68 };
yading@10 69
yading@10 70 static const PixelFormatTag pix_fmt_bps_mov[] = {
yading@10 71 { AV_PIX_FMT_MONOWHITE, 1 },
yading@10 72 { AV_PIX_FMT_PAL8, 2 },
yading@10 73 { AV_PIX_FMT_PAL8, 4 },
yading@10 74 { AV_PIX_FMT_PAL8, 8 },
yading@10 75 // FIXME swscale does not support 16 bit in .mov, sample 16bit.mov
yading@10 76 // http://developer.apple.com/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
yading@10 77 { AV_PIX_FMT_RGB555BE, 16 },
yading@10 78 { AV_PIX_FMT_RGB24, 24 },
yading@10 79 { AV_PIX_FMT_ARGB, 32 },
yading@10 80 { AV_PIX_FMT_MONOWHITE,33 },
yading@10 81 { AV_PIX_FMT_NONE, 0 },
yading@10 82 };
yading@10 83
yading@10 84 enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags,
yading@10 85 unsigned int fourcc)
yading@10 86 {
yading@10 87 while (tags->pix_fmt >= 0) {
yading@10 88 if (tags->fourcc == fourcc)
yading@10 89 return tags->pix_fmt;
yading@10 90 tags++;
yading@10 91 }
yading@10 92 return AV_PIX_FMT_NONE;
yading@10 93 }
yading@10 94
yading@10 95 #if LIBAVCODEC_VERSION_MAJOR < 55
yading@10 96 enum AVPixelFormat ff_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
yading@10 97 {
yading@10 98 return avpriv_find_pix_fmt(tags, fourcc);
yading@10 99 }
yading@10 100 #endif
yading@10 101
yading@10 102 static av_cold int raw_init_decoder(AVCodecContext *avctx)
yading@10 103 {
yading@10 104 RawVideoContext *context = avctx->priv_data;
yading@10 105 const AVPixFmtDescriptor *desc;
yading@10 106
yading@10 107 if ( avctx->codec_tag == MKTAG('r','a','w',' ')
yading@10 108 || avctx->codec_tag == MKTAG('N','O','1','6'))
yading@10 109 avctx->pix_fmt = avpriv_find_pix_fmt(pix_fmt_bps_mov,
yading@10 110 avctx->bits_per_coded_sample);
yading@10 111 else if (avctx->codec_tag == MKTAG('W', 'R', 'A', 'W'))
yading@10 112 avctx->pix_fmt = avpriv_find_pix_fmt(pix_fmt_bps_avi,
yading@10 113 avctx->bits_per_coded_sample);
yading@10 114 else if (avctx->codec_tag)
yading@10 115 avctx->pix_fmt = avpriv_find_pix_fmt(ff_raw_pix_fmt_tags, avctx->codec_tag);
yading@10 116 else if (avctx->pix_fmt == AV_PIX_FMT_NONE && avctx->bits_per_coded_sample)
yading@10 117 avctx->pix_fmt = avpriv_find_pix_fmt(pix_fmt_bps_avi,
yading@10 118 avctx->bits_per_coded_sample);
yading@10 119
yading@10 120 desc = av_pix_fmt_desc_get(avctx->pix_fmt);
yading@10 121 if (!desc) {
yading@10 122 av_log(avctx, AV_LOG_ERROR, "Invalid pixel format.\n");
yading@10 123 return AVERROR(EINVAL);
yading@10 124 }
yading@10 125
yading@10 126 if (desc->flags & (PIX_FMT_PAL | PIX_FMT_PSEUDOPAL)) {
yading@10 127 context->palette = av_buffer_alloc(AVPALETTE_SIZE);
yading@10 128 if (!context->palette)
yading@10 129 return AVERROR(ENOMEM);
yading@10 130 if (desc->flags & PIX_FMT_PSEUDOPAL)
yading@10 131 avpriv_set_systematic_pal2((uint32_t*)context->palette->data, avctx->pix_fmt);
yading@10 132 else
yading@10 133 memset(context->palette->data, 0, AVPALETTE_SIZE);
yading@10 134 }
yading@10 135
yading@10 136 context->frame_size = avpicture_get_size(avctx->pix_fmt, avctx->width,
yading@10 137 avctx->height);
yading@10 138 if ((avctx->bits_per_coded_sample == 4 || avctx->bits_per_coded_sample == 2) &&
yading@10 139 avctx->pix_fmt == AV_PIX_FMT_PAL8 &&
yading@10 140 (!avctx->codec_tag || avctx->codec_tag == MKTAG('r','a','w',' ')))
yading@10 141 context->is_2_4_bpp = 1;
yading@10 142
yading@10 143 if ((avctx->extradata_size >= 9 &&
yading@10 144 !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9)) ||
yading@10 145 avctx->codec_tag == MKTAG('c','y','u','v') ||
yading@10 146 avctx->codec_tag == MKTAG(3, 0, 0, 0) ||
yading@10 147 avctx->codec_tag == MKTAG('W','R','A','W'))
yading@10 148 context->flip = 1;
yading@10 149
yading@10 150 if (avctx->codec_tag == AV_RL32("yuv2") &&
yading@10 151 avctx->pix_fmt == AV_PIX_FMT_YUYV422)
yading@10 152 context->is_yuv2 = 1;
yading@10 153
yading@10 154 return 0;
yading@10 155 }
yading@10 156
yading@10 157 static void flip(AVCodecContext *avctx, AVPicture *picture)
yading@10 158 {
yading@10 159 picture->data[0] += picture->linesize[0] * (avctx->height - 1);
yading@10 160 picture->linesize[0] *= -1;
yading@10 161 }
yading@10 162
yading@10 163 static int raw_decode(AVCodecContext *avctx, void *data, int *got_frame,
yading@10 164 AVPacket *avpkt)
yading@10 165 {
yading@10 166 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
yading@10 167 RawVideoContext *context = avctx->priv_data;
yading@10 168 const uint8_t *buf = avpkt->data;
yading@10 169 int buf_size = avpkt->size;
yading@10 170 int linesize_align = 4;
yading@10 171 int res, len;
yading@10 172 int need_copy = !avpkt->buf || context->is_2_4_bpp || context->is_yuv2;
yading@10 173
yading@10 174 AVFrame *frame = data;
yading@10 175 AVPicture *picture = data;
yading@10 176
yading@10 177 frame->pict_type = AV_PICTURE_TYPE_I;
yading@10 178 frame->key_frame = 1;
yading@10 179 frame->reordered_opaque = avctx->reordered_opaque;
yading@10 180 frame->pkt_pts = avctx->pkt->pts;
yading@10 181 av_frame_set_pkt_pos (frame, avctx->pkt->pos);
yading@10 182 av_frame_set_pkt_duration(frame, avctx->pkt->duration);
yading@10 183
yading@10 184 if (context->tff >= 0) {
yading@10 185 frame->interlaced_frame = 1;
yading@10 186 frame->top_field_first = context->tff;
yading@10 187 }
yading@10 188
yading@10 189 if ((res = av_image_check_size(avctx->width, avctx->height, 0, avctx)) < 0)
yading@10 190 return res;
yading@10 191
yading@10 192 if (need_copy)
yading@10 193 frame->buf[0] = av_buffer_alloc(context->frame_size);
yading@10 194 else
yading@10 195 frame->buf[0] = av_buffer_ref(avpkt->buf);
yading@10 196 if (!frame->buf[0])
yading@10 197 return AVERROR(ENOMEM);
yading@10 198
yading@10 199 //2bpp and 4bpp raw in avi and mov (yes this is ugly ...)
yading@10 200 if (context->is_2_4_bpp) {
yading@10 201 int i;
yading@10 202 uint8_t *dst = frame->buf[0]->data;
yading@10 203 buf_size = context->frame_size - AVPALETTE_SIZE;
yading@10 204 if (avctx->bits_per_coded_sample == 4) {
yading@10 205 for (i = 0; 2 * i + 1 < buf_size && i<avpkt->size; i++) {
yading@10 206 dst[2 * i + 0] = buf[i] >> 4;
yading@10 207 dst[2 * i + 1] = buf[i] & 15;
yading@10 208 }
yading@10 209 linesize_align = 8;
yading@10 210 } else {
yading@10 211 av_assert0(avctx->bits_per_coded_sample == 2);
yading@10 212 for (i = 0; 4 * i + 3 < buf_size && i<avpkt->size; i++) {
yading@10 213 dst[4 * i + 0] = buf[i] >> 6;
yading@10 214 dst[4 * i + 1] = buf[i] >> 4 & 3;
yading@10 215 dst[4 * i + 2] = buf[i] >> 2 & 3;
yading@10 216 dst[4 * i + 3] = buf[i] & 3;
yading@10 217 }
yading@10 218 linesize_align = 16;
yading@10 219 }
yading@10 220 buf = dst;
yading@10 221 } else if (need_copy) {
yading@10 222 memcpy(frame->buf[0]->data, buf, FFMIN(buf_size, context->frame_size));
yading@10 223 buf = frame->buf[0]->data;
yading@10 224 }
yading@10 225
yading@10 226 if (avctx->codec_tag == MKTAG('A', 'V', '1', 'x') ||
yading@10 227 avctx->codec_tag == MKTAG('A', 'V', 'u', 'p'))
yading@10 228 buf += buf_size - context->frame_size;
yading@10 229
yading@10 230 len = context->frame_size - (avctx->pix_fmt==AV_PIX_FMT_PAL8 ? AVPALETTE_SIZE : 0);
yading@10 231 if (buf_size < len) {
yading@10 232 av_log(avctx, AV_LOG_ERROR, "Invalid buffer size, packet size %d < expected frame_size %d\n", buf_size, len);
yading@10 233 av_buffer_unref(&frame->buf[0]);
yading@10 234 return AVERROR(EINVAL);
yading@10 235 }
yading@10 236
yading@10 237 if ((res = avpicture_fill(picture, buf, avctx->pix_fmt,
yading@10 238 avctx->width, avctx->height)) < 0) {
yading@10 239 av_buffer_unref(&frame->buf[0]);
yading@10 240 return res;
yading@10 241 }
yading@10 242
yading@10 243 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
yading@10 244 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE,
yading@10 245 NULL);
yading@10 246
yading@10 247 if (pal) {
yading@10 248 av_buffer_unref(&context->palette);
yading@10 249 context->palette = av_buffer_alloc(AVPALETTE_SIZE);
yading@10 250 if (!context->palette) {
yading@10 251 av_buffer_unref(&frame->buf[0]);
yading@10 252 return AVERROR(ENOMEM);
yading@10 253 }
yading@10 254 memcpy(context->palette->data, pal, AVPALETTE_SIZE);
yading@10 255 frame->palette_has_changed = 1;
yading@10 256 }
yading@10 257 }
yading@10 258
yading@10 259 if ((avctx->pix_fmt==AV_PIX_FMT_BGR24 ||
yading@10 260 avctx->pix_fmt==AV_PIX_FMT_GRAY8 ||
yading@10 261 avctx->pix_fmt==AV_PIX_FMT_RGB555LE ||
yading@10 262 avctx->pix_fmt==AV_PIX_FMT_RGB555BE ||
yading@10 263 avctx->pix_fmt==AV_PIX_FMT_RGB565LE ||
yading@10 264 avctx->pix_fmt==AV_PIX_FMT_MONOWHITE ||
yading@10 265 avctx->pix_fmt==AV_PIX_FMT_PAL8) &&
yading@10 266 FFALIGN(frame->linesize[0], linesize_align) * avctx->height <= buf_size)
yading@10 267 frame->linesize[0] = FFALIGN(frame->linesize[0], linesize_align);
yading@10 268
yading@10 269 if (avctx->pix_fmt == AV_PIX_FMT_NV12 && avctx->codec_tag == MKTAG('N', 'V', '1', '2') &&
yading@10 270 FFALIGN(frame->linesize[0], linesize_align) * avctx->height +
yading@10 271 FFALIGN(frame->linesize[1], linesize_align) * ((avctx->height + 1) / 2) <= buf_size) {
yading@10 272 int la0 = FFALIGN(frame->linesize[0], linesize_align);
yading@10 273 frame->data[1] += (la0 - frame->linesize[0]) * avctx->height;
yading@10 274 frame->linesize[0] = la0;
yading@10 275 frame->linesize[1] = FFALIGN(frame->linesize[1], linesize_align);
yading@10 276 }
yading@10 277
yading@10 278 if ((avctx->pix_fmt == AV_PIX_FMT_PAL8 && buf_size < context->frame_size) ||
yading@10 279 (desc->flags & PIX_FMT_PSEUDOPAL)) {
yading@10 280 frame->buf[1] = av_buffer_ref(context->palette);
yading@10 281 if (!frame->buf[1]) {
yading@10 282 av_buffer_unref(&frame->buf[0]);
yading@10 283 return AVERROR(ENOMEM);
yading@10 284 }
yading@10 285 frame->data[1] = frame->buf[1]->data;
yading@10 286 }
yading@10 287
yading@10 288 if (avctx->pix_fmt == AV_PIX_FMT_BGR24 &&
yading@10 289 ((frame->linesize[0] + 3) & ~3) * avctx->height <= buf_size)
yading@10 290 frame->linesize[0] = (frame->linesize[0] + 3) & ~3;
yading@10 291
yading@10 292 if (context->flip)
yading@10 293 flip(avctx, picture);
yading@10 294
yading@10 295 if (avctx->codec_tag == MKTAG('Y', 'V', '1', '2') ||
yading@10 296 avctx->codec_tag == MKTAG('Y', 'V', '1', '6') ||
yading@10 297 avctx->codec_tag == MKTAG('Y', 'V', '2', '4') ||
yading@10 298 avctx->codec_tag == MKTAG('Y', 'V', 'U', '9'))
yading@10 299 FFSWAP(uint8_t *, picture->data[1], picture->data[2]);
yading@10 300
yading@10 301 if (avctx->codec_tag == AV_RL32("I420") && (avctx->width+1)*(avctx->height+1) * 3/2 == buf_size) {
yading@10 302 picture->data[1] = picture->data[1] + (avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height;
yading@10 303 picture->data[2] = picture->data[2] + ((avctx->width+1)*(avctx->height+1) -avctx->width*avctx->height)*5/4;
yading@10 304 }
yading@10 305
yading@10 306 if (avctx->codec_tag == AV_RL32("yuv2") &&
yading@10 307 avctx->pix_fmt == AV_PIX_FMT_YUYV422) {
yading@10 308 int x, y;
yading@10 309 uint8_t *line = picture->data[0];
yading@10 310 for (y = 0; y < avctx->height; y++) {
yading@10 311 for (x = 0; x < avctx->width; x++)
yading@10 312 line[2 * x + 1] ^= 0x80;
yading@10 313 line += picture->linesize[0];
yading@10 314 }
yading@10 315 }
yading@10 316 if (avctx->codec_tag == AV_RL32("YVYU") &&
yading@10 317 avctx->pix_fmt == AV_PIX_FMT_YUYV422) {
yading@10 318 int x, y;
yading@10 319 uint8_t *line = picture->data[0];
yading@10 320 for(y = 0; y < avctx->height; y++) {
yading@10 321 for(x = 0; x < avctx->width - 1; x += 2)
yading@10 322 FFSWAP(uint8_t, line[2*x + 1], line[2*x + 3]);
yading@10 323 line += picture->linesize[0];
yading@10 324 }
yading@10 325 }
yading@10 326
yading@10 327 if (avctx->field_order > AV_FIELD_PROGRESSIVE) { /* we have interlaced material flagged in container */
yading@10 328 frame->interlaced_frame = 1;
yading@10 329 if (avctx->field_order == AV_FIELD_TT || avctx->field_order == AV_FIELD_TB)
yading@10 330 frame->top_field_first = 1;
yading@10 331 }
yading@10 332
yading@10 333 *got_frame = 1;
yading@10 334 return buf_size;
yading@10 335 }
yading@10 336
yading@10 337 static av_cold int raw_close_decoder(AVCodecContext *avctx)
yading@10 338 {
yading@10 339 RawVideoContext *context = avctx->priv_data;
yading@10 340
yading@10 341 av_buffer_unref(&context->palette);
yading@10 342 return 0;
yading@10 343 }
yading@10 344
yading@10 345 AVCodec ff_rawvideo_decoder = {
yading@10 346 .name = "rawvideo",
yading@10 347 .type = AVMEDIA_TYPE_VIDEO,
yading@10 348 .id = AV_CODEC_ID_RAWVIDEO,
yading@10 349 .priv_data_size = sizeof(RawVideoContext),
yading@10 350 .init = raw_init_decoder,
yading@10 351 .close = raw_close_decoder,
yading@10 352 .decode = raw_decode,
yading@10 353 .long_name = NULL_IF_CONFIG_SMALL("raw video"),
yading@10 354 .priv_class = &class,
yading@10 355 };