飞凌嵌入式
直播中

jf_1137202360

8年用户 1358经验值
擅长:嵌入式技术
私信 关注
[技术]

【飞凌RK3588开发板试用】ffmpeg开发系列之三——编解码

本帖最后由 jf_1137202360 于 2023-3-22 10:03 编辑

前言

  前面我们搭建了ffmpeg的开发环境,进行构建和测试。这一篇实际进行代码编写测试。

准备

  我们可以按照之前的文章从源码构建ffmpeg安装,也可以直接下载编译好的库使用。
引用: wget https://github.com/BtbN/FFmpeg-B ... 4-gpl-shared.tar.xz
sudo apt-get install xz-utils
xz -d ffmpeg-master-latest-linuxARM64-gpl-shared.tar.xz
tar -xvf ffmpeg-master-latest-linuxarm64-gpl-shared.tar

编码测试

代码
引用: vi encode_video.c
代码如下
  1. #include
  2. #include
  3. #include

  4. #include

  5. #include
  6. #include

  7. static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
  8.                    FILE *outfile)
  9. {
  10.     int ret;

  11.     /* send the frame to the encoder */
  12.     if (frame)
  13.         printf("Send frame %3"PRId64"n", frame->pts);

  14.     ret = avcodec_send_frame(enc_ctx, frame);
  15.     if (ret < 0) {
  16.         fprintf(stderr, "Error sending a frame for encodingn");
  17.         exit(1);
  18.     }

  19.     while (ret >= 0) {
  20.         ret = avcodec_receive_packet(enc_ctx, pkt);
  21.         if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  22.             return;
  23.         else if (ret < 0) {
  24.             fprintf(stderr, "Error during encodingn");
  25.             exit(1);
  26.         }

  27.         printf("Write packet %3"PRId64" (size=%5d)n", pkt->pts, pkt->size);
  28.         fwrite(pkt->data, 1, pkt->size, outfile);
  29.         av_packet_unref(pkt);
  30.     }
  31. }

  32. int main(int argc, char **argv)
  33. {
  34.     const char *filename, *codec_name;
  35.     const AVCodec *codec;
  36.     AVCodecContext *c= NULL;
  37.     int i, ret, x, y;
  38.     FILE *f;
  39.     AVFrame *frame;
  40.     AVPacket *pkt;
  41.     uint8_t endcode[] = { 0, 0, 1, 0xb7 };

  42.     if (argc <= 2) {
  43.         fprintf(stderr, "Usage: %s n", argv[0]);
  44.         exit(0);
  45.     }
  46.     filename = argv[1];
  47.     codec_name = argv[2];

  48.     /* find the mpeg1video encoder */
  49.     codec = avcodec_find_encoder_by_name(codec_name);
  50.     if (!codec) {
  51.         fprintf(stderr, "Codec '%s' not foundn", codec_name);
  52.         exit(1);
  53.     }

  54.     c = avcodec_alloc_context3(codec);
  55.     if (!c) {
  56.         fprintf(stderr, "Could not allocate video codec contextn");
  57.         exit(1);
  58.     }

  59.     pkt = av_packet_alloc();
  60.     if (!pkt)
  61.         exit(1);

  62.     /* put sample parameters */
  63.     c->bit_rate = 400000;
  64.     /* resolution must be a multiple of two */
  65.     c->width = 352;
  66.     c->height = 288;
  67.     /* frames per second */
  68.     c->time_base = (AVRational){1, 25};
  69.     c->framerate = (AVRational){25, 1};

  70.     /* emit one intra frame every ten frames
  71.      * check frame pict_type before passing frame
  72.      * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
  73.      * then gop_size is ignored and the output of encoder
  74.      * will always be I frame irrespective to gop_size
  75.      */
  76.     c->gop_size = 10;
  77.     c->max_b_frames = 1;
  78.     c->pix_fmt = AV_PIX_FMT_YUV420P;

  79.     if (codec->id == AV_CODEC_ID_H264)
  80.         av_opt_set(c->priv_data, "preset", "slow", 0);

  81.     /* open it */
  82.     ret = avcodec_open2(c, codec, NULL);
  83.     if (ret < 0) {
  84.         fprintf(stderr, "Could not open codec: %sn", av_err2str(ret));
  85.         exit(1);
  86.     }

  87.     f = fopen(filename, "wb");
  88.     if (!f) {
  89.         fprintf(stderr, "Could not open %sn", filename);
  90.         exit(1);
  91.     }

  92.     frame = av_frame_alloc();
  93.     if (!frame) {
  94.         fprintf(stderr, "Could not allocate video framen");
  95.         exit(1);
  96.     }
  97.     frame->format = c->pix_fmt;
  98.     frame->width  = c->width;
  99.     frame->height = c->height;

  100.     ret = av_frame_get_buffer(frame, 0);
  101.     if (ret < 0) {
  102.         fprintf(stderr, "Could not allocate the video frame datan");
  103.         exit(1);
  104.     }

  105.     /* encode 1 second of video */
  106.     for (i = 0; i < 25; i++) {
  107.         fflush(stdout);

  108.         /* Make sure the frame data is writable.
  109.            On the first round, the frame is fresh from av_frame_get_buffer()
  110.            and therefore we know it is writable.
  111.            But on the next rounds, encode() will have called
  112.            avcodec_send_frame(), and the codec may have kept a reference to
  113.            the frame in its internal structures, that makes the frame
  114.            unwritable.
  115.            av_frame_make_writable() checks that and allocates a new buffer
  116.            for the frame only if necessary.
  117.          */
  118.         ret = av_frame_make_writable(frame);
  119.         if (ret < 0)
  120.             exit(1);

  121.         /* Prepare a dummy image.
  122.            In real code, this is where you would have your own logic for
  123.            filling the frame. FFmpeg does not care what you put in the
  124.            frame.
  125.          */
  126.         /* Y */
  127.         for (y = 0; y < c->height; y++) {
  128.             for (x = 0; x < c->width; x++) {
  129.                 frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
  130.             }
  131.         }

  132.         /* Cb and Cr */
  133.         for (y = 0; y < c->height/2; y++) {
  134.             for (x = 0; x < c->width/2; x++) {
  135.                 frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
  136.                 frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
  137.             }
  138.         }

  139.         frame->pts = i;

  140.         /* encode the image */
  141.         encode(c, frame, pkt, f);
  142.     }

  143.     /* flush the encoder */
  144.     encode(c, NULL, pkt, f);

  145.     /* Add sequence end code to have a real MPEG file.
  146.        It makes only sense because this tiny examples writes packets
  147.        directly. This is called "elementary stream" and only works for some
  148.        codecs. To create a valid file, you usually need to write packets
  149.        into a proper file format or protocol; see mux.c.
  150.      */
  151.     if (codec->id == AV_CODEC_ID_MPEG1VIDEO || codec->id == AV_CODEC_ID_MPEG2VIDEO)
  152.         fwrite(endcode, 1, sizeof(endcode), f);
  153.     fclose(f);

  154.     avcodec_free_context(&c);
  155.     av_frame_free(&frame);
  156.     av_packet_free(&pkt);

  157.     return 0;
  158. }

编译

引用: 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

结果
  1. root@ok3588:~# export LD_LIBRARY_PATH=/root/ffmpeg-master-latest-linuxarm64-gpl-shared/lib:$LD_LIBRARY_PATH

  2. root@ok3588:~# ./encode encode.bin mpeg1video

  3. Send frame   0

  4. Send frame   1

  5. Write packet   0 (size= 6731)

  6. Send frame   2

  7. Write packet   2 (size= 3727)

  8. Send frame   3

  9. Write packet   1 (size= 1698)

  10. Send frame   4

  11. Write packet   4 (size= 2744)

  12. Send frame   5

  13. Write packet   3 (size= 1676)

  14. Send frame   6

  15. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  16. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  17. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  18. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  19. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  20. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  21. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  22. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  23. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  24. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  25. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  26. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  27. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  28. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  29. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  30. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  31. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  32. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  33. Write packet   6 (size= 2955)

  34. Send frame   7

  35. Write packet   5 (size= 1810)

  36. Send frame   8

  37. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  38. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  39. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  40. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  41. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  42. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  43. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  44. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  45. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  46. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  47. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  48. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  49. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  50. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  51. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  52. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  53. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  54. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  55. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  56. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  57. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  58. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  59. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  60. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  61. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  62. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  63. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  64. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  65. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  66. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  67. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  68. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  69. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  70. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  71. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  72. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  73. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  74. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  75. Write packet   8 (size= 3170)

  76. Send frame   9

  77. Write packet   7 (size= 1937)

  78. Send frame  10

  79. Write packet  10 (size=12306)

  80. Send frame  11

  81. Write packet   9 (size= 2173)

  82. Send frame  12

  83. Write packet  12 (size= 3770)

  84. Send frame  13

  85. Write packet  11 (size= 2064)

  86. Send frame  14

  87. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  88. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  89. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  90. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  91. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  92. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  93. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  94. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  95. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  96. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  97. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  98. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  99. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  100. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  101. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  102. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  103. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  104. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  105. Write packet  14 (size= 3340)

  106. Send frame  15

  107. Write packet  13 (size= 1941)

  108. Send frame  16

  109. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  110. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  111. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  112. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  113. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  114. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  115. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  116. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  117. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  118. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  119. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  120. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  121. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  122. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  123. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  124. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  125. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  126. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  127. Write packet  16 (size= 3121)

  128. Send frame  17

  129. Write packet  15 (size= 1897)

  130. Send frame  18

  131. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  132. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  133. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  134. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  135. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  136. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  137. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  138. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  139. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  140. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  141. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  142. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  143. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  144. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  145. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  146. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  147. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  148. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  149. Write packet  18 (size= 3392)

  150. Send frame  19

  151. Write packet  17 (size= 2172)

  152. Send frame  20

  153. Write packet  20 (size=12266)

  154. Send frame  21

  155. Write packet  19 (size= 2169)

  156. Send frame  22

  157. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  158. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  159. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  160. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  161. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  162. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  163. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  164. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  165. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  166. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  167. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  168. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  169. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  170. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  171. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  172. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  173. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  174. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  175. Write packet  22 (size= 4000)

  176. Send frame  23

  177. Write packet  21 (size= 2018)

  178. Send frame  24

  179. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  180. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  181. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  182. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  183. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  184. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  185. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  186. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  187. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  188. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  189. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  190. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  191. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  192. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  193. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  194. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  195. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  196. [mpeg1video @ 0x55ac6488f0] warning, clipping 1 dct coefficients to -255..255

  197. Write packet  24 (size= 3072)

  198. Write packet  23 (size= 1935)

  199. root@ok3588:~#

解码测试        

代码

引用: vi decode_video.c

代码如下
  1. #include
  2. #include
  3. #include

  4. #include

  5. #define INBUF_SIZE 4096

  6. static void pgm_save(unsigned char* buf, int wrap, int xsize, int ysize,
  7.     char* filename)
  8. {
  9.     FILE* f;
  10.     int i;

  11.     f = fopen(filename, "wb");
  12.     fprintf(f, "P5n%d %dn%dn", xsize, ysize, 255);
  13.     for (i = 0; i < ysize; i++)
  14.         fwrite(buf + i * wrap, 1, xsize, f);
  15.     fclose(f);
  16. }

  17. static void decode(AVCodecContext* dec_ctx, AVFrame* frame, AVPacket* pkt,
  18.     const char* filename)
  19. {
  20.     char buf[1024];
  21.     int ret;

  22.     ret = avcodec_send_packet(dec_ctx, pkt);
  23.     if (ret < 0) {
  24.         fprintf(stderr, "Error sending a packet for decodingn");
  25.         exit(1);
  26.     }

  27.     while (ret >= 0) {
  28.         ret = avcodec_receive_frame(dec_ctx, frame);
  29.         if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  30.             return;
  31.         else if (ret < 0) {
  32.             fprintf(stderr, "Error during decodingn");
  33.             exit(1);
  34.         }

  35.         printf("saving frame %3"PRId64"n", dec_ctx->frame_num);
  36.         fflush(stdout);

  37.         /* the picture is allocated by the decoder. no need to
  38.            free it */
  39.         snprintf(buf, sizeof(buf), "%s-%"PRId64, filename, dec_ctx->frame_num);
  40.         pgm_save(frame->data[0], frame->linesize[0],
  41.             frame->width, frame->height, buf);
  42.     }
  43. }

  44. int main(int argc, char** argv)
  45. {
  46.     const char* filename, * outfilename;
  47.     const AVCodec* codec;
  48.     AVCodecParserContext* parser;
  49.     AVCodecContext* c = NULL;
  50.     FILE* f;
  51.     AVFrame* frame;
  52.     uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  53.     uint8_t* data;
  54.     size_t   data_size;
  55.     int ret;
  56.     int eof;
  57.     AVPacket* pkt;

  58.     if (argc <= 2) {
  59.         fprintf(stderr, "Usage: %s n"
  60.             "And check your input file is encoded by mpeg1video please.n", argv[0]);
  61.         exit(0);
  62.     }
  63.     filename = argv[1];
  64.     outfilename = argv[2];

  65.     pkt = av_packet_alloc();
  66.     if (!pkt)
  67.         exit(1);

  68.     /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
  69.     memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);

  70.     /* find the MPEG-1 video decoder */
  71.     codec = avcodec_find_decoder(AV_CODEC_ID_MPEG1VIDEO);
  72.     if (!codec) {
  73.         fprintf(stderr, "Codec not foundn");
  74.         exit(1);
  75.     }

  76.     parser = av_parser_init(codec->id);
  77.     if (!parser) {
  78.         fprintf(stderr, "parser not foundn");
  79.         exit(1);
  80.     }

  81.     c = avcodec_alloc_context3(codec);
  82.     if (!c) {
  83.         fprintf(stderr, "Could not allocate video codec contextn");
  84.         exit(1);
  85.     }

  86.     /* For some codecs, such as msmpeg4 and mpeg4, width and height
  87.        MUST be initialized there because this information is not
  88.        available in the bitstream. */

  89.        /* open it */
  90.     if (avcodec_open2(c, codec, NULL) < 0) {
  91.         fprintf(stderr, "Could not open codecn");
  92.         exit(1);
  93.     }

  94.     f = fopen(filename, "rb");
  95.     if (!f) {
  96.         fprintf(stderr, "Could not open %sn", filename);
  97.         exit(1);
  98.     }

  99.     frame = av_frame_alloc();
  100.     if (!frame) {
  101.         fprintf(stderr, "Could not allocate video framen");
  102.         exit(1);
  103.     }

  104.     do {
  105.         /* read raw data from the input file */
  106.         data_size = fread(inbuf, 1, INBUF_SIZE, f);
  107.         if (ferror(f))
  108.             break;
  109.         eof = !data_size;

  110.         /* use the parser to split the data into frames */
  111.         data = inbuf;
  112.         while (data_size > 0 || eof) {
  113.             ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
  114.                 data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
  115.             if (ret < 0) {
  116.                 fprintf(stderr, "Error while parsingn");
  117.                 exit(1);
  118.             }
  119.             data += ret;
  120.             data_size -= ret;

  121.             if (pkt->size)
  122.                 decode(c, frame, pkt, outfilename);
  123.             else if (eof)
  124.                 break;
  125.         }
  126.     } while (!eof);

  127.     /* flush the decoder */
  128.     decode(c, frame, NULL, outfilename);

  129.     fclose(f);

  130.     av_parser_close(parser);
  131.     avcodec_free_context(&c);
  132.     av_frame_free(&frame);
  133.     av_packet_free(&pkt);

  134.     return 0;
  135. }

编译

引用: 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

结果
  1. root@ok3588:~# ./decode encode.bin decode.bin

  2. saving frame   1

  3. saving frame   2

  4. saving frame   3

  5. saving frame   4

  6. saving frame   5

  7. saving frame   6

  8. saving frame   7

  9. saving frame   8

  10. saving frame   9

  11. saving frame  10

  12. saving frame  11

  13. saving frame  12

  14. saving frame  13

  15. saving frame  14

  16. saving frame  15

  17. saving frame  16

  18. saving frame  17

  19. saving frame  18

  20. saving frame  19

  21. saving frame  20

  22. saving frame  21

  23. saving frame  22

  24. saving frame  23

  25. saving frame  24

  26. saving frame  25

  27. root@ok3588:~# ls decode* -al

  28. -rwxr-xr-x 1 root root  14736 Mar 21 23:15 decode

  29. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-1

  30. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-10

  31. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-11

  32. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-12

  33. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-13

  34. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-14

  35. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-15

  36. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-16

  37. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-17

  38. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-18

  39. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-19

  40. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-2

  41. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-20

  42. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-21

  43. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-22

  44. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-23

  45. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-24

  46. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-25

  47. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-3

  48. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-4

  49. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-5

  50. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-6

  51. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-7

  52. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-8

  53. -rw-r--r-- 1 root root 101391 Mar 21 23:20 decode.bin-9

  54. -rw-r--r-- 1 root root   6279 Mar 21 23:05 decode_video.c

  55. root@ok3588:~#
播放可以导出编码的文件到PC进行播放

引用: .ffplay.exe -i encode.bin
图片1.png

总结
  由于开发板运行Ubuntu系统性能非常强,能联网进行apt安装包,所以可以直接在开发板上进行开发,免去了交叉编译的麻烦,开发非常方便。通过ffmpeg编解码测试也可以体验到该开发板进行多媒体开发非常简繁方便,且运行高效。

更多回帖

发帖
×
20
完善资料,
赚取积分