第一步:
在libavcodec目录下新建mkencoder.c并加入代码:
-
-
-
-
-
-
-
-
- #include "avcodec.h"
- #include "libavutil/pixdesc.h"
-
- static av_cold int mk_encode_init(AVCodecContext *avctx)
- {
- printf("init mk encoder");
- const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
-
- avctx->coded_frame = av_frame_alloc();
- avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
- avctx->bits_per_coded_sample = av_get_bits_per_pixel(desc);
- if(!avctx->codec_tag)
- avctx->codec_tag = avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
- return 0;
- }
-
- static int mk_encode(AVCodecContext *avctx, AVPacket *pkt,
- const AVFrame *frame, int *got_packet)
- {
- int ret = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
-
- if (ret < 0)
- return ret;
-
- if (pkt->data == NULL && pkt->size == 0)
- {
- av_new_packet(pkt,ret);
- pkt->size = ret;
- }
-
-
-
-
- if ((ret = avpicture_layout((const AVPicture *)frame, avctx->pix_fmt, avctx->width,
- avctx->height, pkt->data, pkt->size)) < 0)
- return ret;
-
-
-
-
-
-
-
-
- pkt->flags |= AV_PKT_FLAG_KEY;
- *got_packet = 1;
- return 0;
- }
-
- static av_cold int mk_close(AVCodecContext *avctx)
- {
- printf("close mk encoder");
- av_frame_free(&avctx->coded_frame);
- return 0;
- }
-
- AVCodec ff_mkvideo_encoder = {
- .name = "mkvideo",
- .long_name = NULL_IF_CONFIG_SMALL("mk video"),
- .type = AVMEDIA_TYPE_VIDEO,
- .id = AV_CODEC_ID_MKVIDEO,
- .init = mk_encode_init,
- .encode2 = mk_encode,
- .close = mk_close,
- };
第二步:
在avcodec.h里的 enum AVCodecID 最后加入:
第三步:
在allcodec.c的void avcodec_register_all(void)函数中加入
- REGISTER_ENCODER(MKVIDEO, mkvideo);
第四步:
在makefile里加入
- OBJS-$(CONFIG_MKVIDEO_ENCODER) += mkencoder.o
第五步:
在ffmpeg根目录config.h里加入
- #define CONFIG_MKVIDEO_ENCODER 1
第六步:
在codec_desc.c的static const AVCodecDescriptor codec_descriptors[] 中
加入:
- {
- .id = AV_CODEC_ID_MKVIDEO,
- .type = AVMEDIA_TYPE_VIDEO,
- .name = "mkvideo",
- .long_name = NULL_IF_CONFIG_SMALL("mk video"),
- .props = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
- },
第七步:
重新编译ffmpeg。
测试:
运行命令:ffmpeg -encoders:在显示中找到自己的encoder:
- VFS... mjpeg MJPEG (Motion JPEG)
- V..... mkvideo mk video
- V.S... mpeg1video MPEG-1 video
- V.S... mpeg2video MPEG-2 video
运行命令:
- ffmpeg -i test.avi -c:v mkvideo -y test.mk
把生成的test.mk改后缀为yuv,用yuvplayer播放。
播放成功!