00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "avcodec.h"
00024
00025 static av_cold int yuv4_encode_init(AVCodecContext *avctx)
00026 {
00027 avctx->coded_frame = avcodec_alloc_frame();
00028
00029 if (!avctx->coded_frame) {
00030 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
00031 return AVERROR(ENOMEM);
00032 }
00033
00034 return 0;
00035 }
00036
00037 static int yuv4_encode_frame(AVCodecContext *avctx, uint8_t *buf,
00038 int buf_size, void *data)
00039 {
00040 AVFrame *pic = data;
00041 uint8_t *dst = buf;
00042 uint8_t *y, *u, *v;
00043 int i, j;
00044 int output_size = 0;
00045
00046 if (buf_size < 6 * (avctx->width + 1 >> 1) * (avctx->height + 1 >> 1)) {
00047 av_log(avctx, AV_LOG_ERROR, "Out buffer is too small.\n");
00048 return AVERROR(ENOMEM);
00049 }
00050
00051 avctx->coded_frame->reference = 0;
00052 avctx->coded_frame->key_frame = 1;
00053 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00054
00055 y = pic->data[0];
00056 u = pic->data[1];
00057 v = pic->data[2];
00058
00059 for (i = 0; i < avctx->height + 1 >> 1; i++) {
00060 for (j = 0; j < avctx->width + 1 >> 1; j++) {
00061 *dst++ = u[j] ^ 0x80;
00062 *dst++ = v[j] ^ 0x80;
00063 *dst++ = y[ 2 * j ];
00064 *dst++ = y[ 2 * j + 1];
00065 *dst++ = y[pic->linesize[0] + 2 * j ];
00066 *dst++ = y[pic->linesize[0] + 2 * j + 1];
00067 output_size += 6;
00068 }
00069 y += 2 * pic->linesize[0];
00070 u += pic->linesize[1];
00071 v += pic->linesize[2];
00072 }
00073
00074 return output_size;
00075 }
00076
00077 static av_cold int yuv4_encode_close(AVCodecContext *avctx)
00078 {
00079 av_freep(&avctx->coded_frame);
00080
00081 return 0;
00082 }
00083
00084 AVCodec ff_yuv4_encoder = {
00085 .name = "yuv4",
00086 .type = AVMEDIA_TYPE_VIDEO,
00087 .id = CODEC_ID_YUV4,
00088 .init = yuv4_encode_init,
00089 .encode = yuv4_encode_frame,
00090 .close = yuv4_encode_close,
00091 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_YUV420P, PIX_FMT_NONE },
00092 .long_name = NULL_IF_CONFIG_SMALL("Uncompressed packed 4:2:0"),
00093 };