yading@10
|
1 /*
|
yading@10
|
2 * libquicktime yuv4 encoder
|
yading@10
|
3 *
|
yading@10
|
4 * Copyright (c) 2011 Carl Eugen Hoyos
|
yading@10
|
5 *
|
yading@10
|
6 * This file is part of FFmpeg.
|
yading@10
|
7 *
|
yading@10
|
8 * FFmpeg is free software; you can redistribute it and/or
|
yading@10
|
9 * modify it under the terms of the GNU Lesser General Public
|
yading@10
|
10 * License as published by the Free Software Foundation; either
|
yading@10
|
11 * version 2.1 of the License, or (at your option) any later version.
|
yading@10
|
12 *
|
yading@10
|
13 * FFmpeg is distributed in the hope that it will be useful,
|
yading@10
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@10
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@10
|
16 * Lesser General Public License for more details.
|
yading@10
|
17 *
|
yading@10
|
18 * You should have received a copy of the GNU Lesser General Public
|
yading@10
|
19 * License along with FFmpeg; if not, write to the Free Software
|
yading@10
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@10
|
21 */
|
yading@10
|
22
|
yading@10
|
23 #include "avcodec.h"
|
yading@10
|
24 #include "internal.h"
|
yading@10
|
25
|
yading@10
|
26 static av_cold int yuv4_encode_init(AVCodecContext *avctx)
|
yading@10
|
27 {
|
yading@10
|
28 avctx->coded_frame = avcodec_alloc_frame();
|
yading@10
|
29
|
yading@10
|
30 if (!avctx->coded_frame) {
|
yading@10
|
31 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
|
yading@10
|
32 return AVERROR(ENOMEM);
|
yading@10
|
33 }
|
yading@10
|
34
|
yading@10
|
35 return 0;
|
yading@10
|
36 }
|
yading@10
|
37
|
yading@10
|
38 static int yuv4_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
|
yading@10
|
39 const AVFrame *pic, int *got_packet)
|
yading@10
|
40 {
|
yading@10
|
41 uint8_t *dst;
|
yading@10
|
42 uint8_t *y, *u, *v;
|
yading@10
|
43 int i, j, ret;
|
yading@10
|
44
|
yading@10
|
45 if ((ret = ff_alloc_packet2(avctx, pkt, 6 * (avctx->width + 1 >> 1) * (avctx->height + 1 >> 1))) < 0)
|
yading@10
|
46 return ret;
|
yading@10
|
47 dst = pkt->data;
|
yading@10
|
48
|
yading@10
|
49 avctx->coded_frame->key_frame = 1;
|
yading@10
|
50 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
|
yading@10
|
51
|
yading@10
|
52 y = pic->data[0];
|
yading@10
|
53 u = pic->data[1];
|
yading@10
|
54 v = pic->data[2];
|
yading@10
|
55
|
yading@10
|
56 for (i = 0; i < avctx->height + 1 >> 1; i++) {
|
yading@10
|
57 for (j = 0; j < avctx->width + 1 >> 1; j++) {
|
yading@10
|
58 *dst++ = u[j] ^ 0x80;
|
yading@10
|
59 *dst++ = v[j] ^ 0x80;
|
yading@10
|
60 *dst++ = y[ 2 * j ];
|
yading@10
|
61 *dst++ = y[ 2 * j + 1];
|
yading@10
|
62 *dst++ = y[pic->linesize[0] + 2 * j ];
|
yading@10
|
63 *dst++ = y[pic->linesize[0] + 2 * j + 1];
|
yading@10
|
64 }
|
yading@10
|
65 y += 2 * pic->linesize[0];
|
yading@10
|
66 u += pic->linesize[1];
|
yading@10
|
67 v += pic->linesize[2];
|
yading@10
|
68 }
|
yading@10
|
69
|
yading@10
|
70 pkt->flags |= AV_PKT_FLAG_KEY;
|
yading@10
|
71 *got_packet = 1;
|
yading@10
|
72 return 0;
|
yading@10
|
73 }
|
yading@10
|
74
|
yading@10
|
75 static av_cold int yuv4_encode_close(AVCodecContext *avctx)
|
yading@10
|
76 {
|
yading@10
|
77 av_freep(&avctx->coded_frame);
|
yading@10
|
78
|
yading@10
|
79 return 0;
|
yading@10
|
80 }
|
yading@10
|
81
|
yading@10
|
82 AVCodec ff_yuv4_encoder = {
|
yading@10
|
83 .name = "yuv4",
|
yading@10
|
84 .type = AVMEDIA_TYPE_VIDEO,
|
yading@10
|
85 .id = AV_CODEC_ID_YUV4,
|
yading@10
|
86 .init = yuv4_encode_init,
|
yading@10
|
87 .encode2 = yuv4_encode_frame,
|
yading@10
|
88 .close = yuv4_encode_close,
|
yading@10
|
89 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
|
yading@10
|
90 .long_name = NULL_IF_CONFIG_SMALL("Uncompressed packed 4:2:0"),
|
yading@10
|
91 };
|