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