annotate ffmpeg/libavcodec/v210enc.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 * V210 encoder
yading@10 3 *
yading@10 4 * Copyright (C) 2009 Michael Niedermayer <michaelni@gmx.at>
yading@10 5 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
yading@10 6 *
yading@10 7 * This file is part of FFmpeg.
yading@10 8 *
yading@10 9 * FFmpeg is free software; you can redistribute it and/or
yading@10 10 * modify it under the terms of the GNU Lesser General Public
yading@10 11 * License as published by the Free Software Foundation; either
yading@10 12 * version 2.1 of the License, or (at your option) any later version.
yading@10 13 *
yading@10 14 * FFmpeg is distributed in the hope that it will be useful,
yading@10 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 17 * Lesser General Public License for more details.
yading@10 18 *
yading@10 19 * You should have received a copy of the GNU Lesser General Public
yading@10 20 * License along with FFmpeg; if not, write to the Free Software
yading@10 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 22 */
yading@10 23
yading@10 24 #include "avcodec.h"
yading@10 25 #include "bytestream.h"
yading@10 26 #include "internal.h"
yading@10 27
yading@10 28 static av_cold int encode_init(AVCodecContext *avctx)
yading@10 29 {
yading@10 30 if (avctx->width & 1) {
yading@10 31 av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
yading@10 32 return AVERROR(EINVAL);
yading@10 33 }
yading@10 34
yading@10 35 if (avctx->bits_per_raw_sample != 10)
yading@10 36 av_log(avctx, AV_LOG_WARNING, "bits per raw sample: %d != 10-bit\n",
yading@10 37 avctx->bits_per_raw_sample);
yading@10 38
yading@10 39 avctx->coded_frame = avcodec_alloc_frame();
yading@10 40 if (!avctx->coded_frame)
yading@10 41 return AVERROR(ENOMEM);
yading@10 42
yading@10 43 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
yading@10 44
yading@10 45 return 0;
yading@10 46 }
yading@10 47
yading@10 48 static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
yading@10 49 const AVFrame *pic, int *got_packet)
yading@10 50 {
yading@10 51 int aligned_width = ((avctx->width + 47) / 48) * 48;
yading@10 52 int stride = aligned_width * 8 / 3;
yading@10 53 int line_padding = stride - ((avctx->width * 8 + 11) / 12) * 4;
yading@10 54 int h, w, ret;
yading@10 55 const uint16_t *y = (const uint16_t*)pic->data[0];
yading@10 56 const uint16_t *u = (const uint16_t*)pic->data[1];
yading@10 57 const uint16_t *v = (const uint16_t*)pic->data[2];
yading@10 58 PutByteContext p;
yading@10 59
yading@10 60 if ((ret = ff_alloc_packet2(avctx, pkt, avctx->height * stride)) < 0)
yading@10 61 return ret;
yading@10 62
yading@10 63 bytestream2_init_writer(&p, pkt->data, pkt->size);
yading@10 64
yading@10 65 #define CLIP(v) av_clip(v, 4, 1019)
yading@10 66
yading@10 67 #define WRITE_PIXELS(a, b, c) \
yading@10 68 do { \
yading@10 69 val = CLIP(*a++); \
yading@10 70 val |= (CLIP(*b++) << 10) | \
yading@10 71 (CLIP(*c++) << 20); \
yading@10 72 bytestream2_put_le32u(&p, val); \
yading@10 73 } while (0)
yading@10 74
yading@10 75 for (h = 0; h < avctx->height; h++) {
yading@10 76 uint32_t val;
yading@10 77 for (w = 0; w < avctx->width - 5; w += 6) {
yading@10 78 WRITE_PIXELS(u, y, v);
yading@10 79 WRITE_PIXELS(y, u, y);
yading@10 80 WRITE_PIXELS(v, y, u);
yading@10 81 WRITE_PIXELS(y, v, y);
yading@10 82 }
yading@10 83 if (w < avctx->width - 1) {
yading@10 84 WRITE_PIXELS(u, y, v);
yading@10 85
yading@10 86 val = CLIP(*y++);
yading@10 87 if (w == avctx->width - 2)
yading@10 88 bytestream2_put_le32u(&p, val);
yading@10 89 if (w < avctx->width - 3) {
yading@10 90 val |= (CLIP(*u++) << 10) | (CLIP(*y++) << 20);
yading@10 91 bytestream2_put_le32u(&p, val);
yading@10 92
yading@10 93 val = CLIP(*v++) | (CLIP(*y++) << 10);
yading@10 94 bytestream2_put_le32u(&p, val);
yading@10 95 }
yading@10 96 }
yading@10 97
yading@10 98 bytestream2_set_buffer(&p, 0, line_padding);
yading@10 99
yading@10 100 y += pic->linesize[0] / 2 - avctx->width;
yading@10 101 u += pic->linesize[1] / 2 - avctx->width / 2;
yading@10 102 v += pic->linesize[2] / 2 - avctx->width / 2;
yading@10 103 }
yading@10 104
yading@10 105 pkt->flags |= AV_PKT_FLAG_KEY;
yading@10 106 *got_packet = 1;
yading@10 107 return 0;
yading@10 108 }
yading@10 109
yading@10 110 static av_cold int encode_close(AVCodecContext *avctx)
yading@10 111 {
yading@10 112 av_freep(&avctx->coded_frame);
yading@10 113
yading@10 114 return 0;
yading@10 115 }
yading@10 116
yading@10 117 AVCodec ff_v210_encoder = {
yading@10 118 .name = "v210",
yading@10 119 .type = AVMEDIA_TYPE_VIDEO,
yading@10 120 .id = AV_CODEC_ID_V210,
yading@10 121 .init = encode_init,
yading@10 122 .encode2 = encode_frame,
yading@10 123 .close = encode_close,
yading@10 124 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV422P10, AV_PIX_FMT_NONE },
yading@10 125 .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
yading@10 126 };