本帖最后由 jf_1137202360 于 2023-3-22 10:03 编辑
前言
前面我们搭建了ffmpeg的开发环境,进行构建和测试。这一篇实际进行代码编写测试。
准备
我们可以按照之前的文章从源码构建ffmpeg安装,也可以直接下载编译好的库使用。
编码测试
代码代码如下
- #include
- #include
- #include
- #include
- #include
- #include
- static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
- FILE *outfile)
- {
- int ret;
- /* send the frame to the encoder */
- if (frame)
- printf("Send frame %3"PRId64"n", frame->pts);
- ret = avcodec_send_frame(enc_ctx, frame);
- if (ret < 0) {
- fprintf(stderr, "Error sending a frame for encodingn");
- exit(1);
- }
- while (ret >= 0) {
- ret = avcodec_receive_packet(enc_ctx, pkt);
- if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
- return;
- else if (ret < 0) {
- fprintf(stderr, "Error during encodingn");
- exit(1);
- }
- printf("Write packet %3"PRId64" (size=%5d)n", pkt->pts, pkt->size);
- fwrite(pkt->data, 1, pkt->size, outfile);
- av_packet_unref(pkt);
- }
- }
- int main(int argc, char **argv)
- {
- const char *filename, *codec_name;
- const AVCodec *codec;
- AVCodecContext *c= NULL;
- int i, ret, x, y;
- FILE *f;
- AVFrame *frame;
- AVPacket *pkt;
- uint8_t endcode[] = { 0, 0, 1, 0xb7 };
- if (argc <= 2) {
- fprintf(stderr, "Usage: %s
复制代码
编译
gcc encode_video.c -Iffmpeg-master-latest-linuxarm64-gpl-shared/include -Lffmpeg-master-latest-linuxarm64-gpl-shared/lib -lavcodec -lavutil -lswresample -o encode
运行
export LD_LIBRARY_PATH=/root/ffmpeg-master-latest-linuxarm64-gpl-shared/lib:$LD_LIBRARY_PATH
./encode encode.bin mpeg1video
结果
- root@ok3588:~# export LD_LIBRARY_PATH=/root/ffmpeg-master-latest-linuxarm64-gpl-shared/lib:$LD_LIBRARY_PATH
- root@ok3588:~# ./encode encode.bin mpeg1video
- Send frame 0
- Send frame 1
- Write packet 0 (size= 6731)
- Send frame 2
- Write packet 2 (size= 3727)
- Send frame 3
- Write packet 1 (size= 1698)
- Send frame 4
- Write packet 4 (size= 2744)
- Send frame 5
- Write packet 3 (size= 1676)
- Send frame 6
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- Write packet 6 (size= 2955)
- Send frame 7
- Write packet 5 (size= 1810)
- Send frame 8
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- Write packet 8 (size= 3170)
- Send frame 9
- Write packet 7 (size= 1937)
- Send frame 10
- Write packet 10 (size=12306)
- Send frame 11
- Write packet 9 (size= 2173)
- Send frame 12
- Write packet 12 (size= 3770)
- Send frame 13
- Write packet 11 (size= 2064)
- Send frame 14
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- Write packet 14 (size= 3340)
- Send frame 15
- Write packet 13 (size= 1941)
- Send frame 16
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- Write packet 16 (size= 3121)
- Send frame 17
- Write packet 15 (size= 1897)
- Send frame 18
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- Write packet 18 (size= 3392)
- Send frame 19
- Write packet 17 (size= 2172)
- Send frame 20
- Write packet 20 (size=12266)
- Send frame 21
- Write packet 19 (size= 2169)
- Send frame 22
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- Write packet 22 (size= 4000)
- Send frame 23
- Write packet 21 (size= 2018)
- Send frame 24
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255
- Write packet 24 (size= 3072)
- Write packet 23 (size= 1935)
- root@ok3588:~#
复制代码
解码测试
代码
代码如下
- #include
- #include
- #include
- #include
- #define INBUF_SIZE 4096
- static void pgm_save(unsigned char* buf, int wrap, int xsize, int ysize,
- char* filename)
- {
- FILE* f;
- int i;
- f = fopen(filename, "wb");
- fprintf(f, "P5n%d %dn%dn", xsize, ysize, 255);
- for (i = 0; i < ysize; i++)
- fwrite(buf + i * wrap, 1, xsize, f);
- fclose(f);
- }
- static void decode(AVCodecContext* dec_ctx, AVFrame* frame, AVPacket* pkt,
- const char* filename)
- {
- char buf[1024];
- int ret;
- ret = avcodec_send_packet(dec_ctx, pkt);
- if (ret < 0) {
- fprintf(stderr, "Error sending a packet for decodingn");
- exit(1);
- }
- while (ret >= 0) {
- ret = avcodec_receive_frame(dec_ctx, frame);
- if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
- return;
- else if (ret < 0) {
- fprintf(stderr, "Error during decodingn");
- exit(1);
- }
- printf("saving frame %3"PRId64"n", dec_ctx->frame_num);
- fflush(stdout);
- /* the picture is allocated by the decoder. no need to
- free it */
- snprintf(buf, sizeof(buf), "%s-%"PRId64, filename, dec_ctx->frame_num);
- pgm_save(frame->data[0], frame->linesize[0],
- frame->width, frame->height, buf);
- }
- }
- int main(int argc, char** argv)
- {
- const char* filename, * outfilename;
- const AVCodec* codec;
- AVCodecParserContext* parser;
- AVCodecContext* c = NULL;
- FILE* f;
- AVFrame* frame;
- uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
- uint8_t* data;
- size_t data_size;
- int ret;
- int eof;
- AVPacket* pkt;
- if (argc <= 2) {
- fprintf(stderr, "Usage: %s
复制代码
编译
gcc decode_video.c -Iffmpeg-master-latest-linuxarm64-gpl-shared/include -Lffmpeg-master-latest-linuxarm64-gpl-shared/lib -lavcodec -lavutil -lswresample -o decode
运行
export LD_LIBRARY_PATH=/root/ffmpeg-master-latest-linuxarm64-gpl-shared/lib:$LD_LIBRARY_PATH
./decode encode.bin decode.bin
结果
播放可以导出编码的文件到PC进行播放
.ffplay.exe -i encode.bin
总结 由于开发板运行Ubuntu系统性能非常强,能联网进行apt安装包,所以可以直接在开发板上进行开发,免去了交叉编译的麻烦,开发非常方便。通过ffmpeg编解码测试也可以体验到该开发板进行多媒体开发非常简繁方便,且运行高效。
0
|
|
|
|