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 #include "internal.h"
00025
00026 static av_cold int yuv4_encode_init(AVCodecContext *avctx)
00027 {
00028 avctx->coded_frame = avcodec_alloc_frame();
00029
00030 if (!avctx->coded_frame) {
00031 av_log(avctx, AV_LOG_ERROR, "Could not allocate frame.\n");
00032 return AVERROR(ENOMEM);
00033 }
00034
00035 return 0;
00036 }
00037
00038 static int yuv4_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00039 const AVFrame *pic, int *got_packet)
00040 {
00041 uint8_t *dst;
00042 uint8_t *y, *u, *v;
00043 int i, j, ret;
00044
00045 if ((ret = ff_alloc_packet2(avctx, pkt, 6 * (avctx->width + 1 >> 1) * (avctx->height + 1 >> 1))) < 0)
00046 return ret;
00047 dst = pkt->data;
00048
00049 avctx->coded_frame->reference = 0;
00050 avctx->coded_frame->key_frame = 1;
00051 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00052
00053 y = pic->data[0];
00054 u = pic->data[1];
00055 v = pic->data[2];
00056
00057 for (i = 0; i < avctx->height + 1 >> 1; i++) {
00058 for (j = 0; j < avctx->width + 1 >> 1; j++) {
00059 *dst++ = u[j] ^ 0x80;
00060 *dst++ = v[j] ^ 0x80;
00061 *dst++ = y[ 2 * j ];
00062 *dst++ = y[ 2 * j + 1];
00063 *dst++ = y[pic->linesize[0] + 2 * j ];
00064 *dst++ = y[pic->linesize[0] + 2 * j + 1];
00065 }
00066 y += 2 * pic->linesize[0];
00067 u += pic->linesize[1];
00068 v += pic->linesize[2];
00069 }
00070
00071 pkt->flags |= AV_PKT_FLAG_KEY;
00072 *got_packet = 1;
00073 return 0;
00074 }
00075
00076 static av_cold int yuv4_encode_close(AVCodecContext *avctx)
00077 {
00078 av_freep(&avctx->coded_frame);
00079
00080 return 0;
00081 }
00082
00083 AVCodec ff_yuv4_encoder = {
00084 .name = "yuv4",
00085 .type = AVMEDIA_TYPE_VIDEO,
00086 .id = CODEC_ID_YUV4,
00087 .init = yuv4_encode_init,
00088 .encode2 = yuv4_encode_frame,
00089 .close = yuv4_encode_close,
00090 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_YUV420P, PIX_FMT_NONE },
00091 .long_name = NULL_IF_CONFIG_SMALL("Uncompressed packed 4:2:0"),
00092 };