ffmpeg_dec_test.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2021 StarFive Technology Co., Ltd.
  4. */
  5. #include <getopt.h>
  6. #include <signal.h>
  7. #include <inttypes.h>
  8. #include <stdbool.h>
  9. #include <libv4l2.h>
  10. #include <poll.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <libavcodec/avcodec.h>
  15. #define INBUF_SIZE 4096
  16. #define DRM_DEVICE_NAME "/dev/dri/card0"
  17. static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
  18. char *filename)
  19. {
  20. FILE *f;
  21. int i;
  22. printf("[%s,%d]: wrap=%d, xsize=%d, ysize=%d, filename=%s\n",__FUNCTION__,__LINE__, wrap, xsize, ysize, filename);
  23. f = fopen(filename,"w");
  24. for (i = 0; i < ysize; i++)
  25. fwrite(buf + i * wrap, 1, xsize, f);
  26. fclose(f);
  27. }
  28. static void yuv_save(unsigned char **buf, int *wrap, int xsize, int ysize,
  29. char *filename)
  30. {
  31. FILE *f;
  32. int i, j, plane_idx = 0;
  33. printf("[%s,%d]: wrap=%d, xsize=%d, ysize=%d, filename=%s\n",__FUNCTION__,__LINE__, wrap, xsize, ysize, filename);
  34. f = fopen(filename,"w");
  35. // fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
  36. // while (buf[plane_idx] && wrap[plane_idx]) {
  37. // for (i = 0; i < ysize; i++) {
  38. // fwrite(buf[plane_idx] + i * wrap[plane_idx], 1, wrap[plane_idx], f);
  39. // }
  40. // plane_idx ++;
  41. // }
  42. unsigned char *y = buf[0];
  43. unsigned char *u = buf[1];
  44. unsigned char *v = buf[2];
  45. int y_linesize = wrap[0];
  46. int u_linesize = wrap[1];
  47. int v_linesize = wrap[2];
  48. fwrite(y, 1, y_linesize*ysize, f);
  49. for (i = 0; i < ysize * u_linesize; i++) {
  50. fwrite(u+i, 1, 1, f);
  51. fwrite(v+i, 1, 1, f);
  52. }
  53. // for (i = 0; i < ysize; i++) {
  54. // for (j = 0; j < u_linesize; j++) {
  55. // fwrite(u+i*, 1, y_linesize*ysize, f);
  56. // }
  57. // }
  58. fclose(f);
  59. }
  60. static void decode(AVCodecContext *dec_ctx, AVFrame *frame, AVPacket *pkt,
  61. const char *filename)
  62. {
  63. char buf[1024];
  64. int ret;
  65. ret = avcodec_send_packet(dec_ctx, pkt);
  66. if (ret < 0) {
  67. fprintf(stderr, "Error sending a packet for decoding\n");
  68. exit(1);
  69. }
  70. while (ret >= 0) {
  71. ret = avcodec_receive_frame(dec_ctx, frame);
  72. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
  73. return;
  74. else if (ret < 0) {
  75. fprintf(stderr, "Error during decoding\n");
  76. exit(1);
  77. }
  78. printf("saving frame %3d\n", dec_ctx->frame_number);
  79. fflush(stdout);
  80. /* the picture is allocated by the decoder. no need to
  81. free it */
  82. snprintf(buf, sizeof(buf), "%s-%d", filename, dec_ctx->frame_number);
  83. // pgm_save(frame->data[0], frame->linesize[0],
  84. // frame->width, frame->height, buf);
  85. yuv_save(frame->data, frame->linesize,
  86. frame->width, frame->height, buf);
  87. printf("[%s,%d]: line=%d, %d, %d, %d. frame->data=%p, %p,%p,%p\n",
  88. __FUNCTION__,__LINE__, frame->linesize[0], frame->linesize[1], frame->linesize[2], frame->linesize[3],
  89. frame->data[0], frame->data[1], frame->data[2],frame->data[3]);
  90. }
  91. }
  92. int main(int argc, char **argv)
  93. {
  94. const char *filename, *outfilename;
  95. const AVCodec *codec;
  96. AVCodecParserContext *parser;
  97. AVCodecContext *c= NULL;
  98. FILE *f;
  99. AVFrame *frame;
  100. uint8_t inbuf[INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
  101. uint8_t *data;
  102. size_t data_size;
  103. int ret;
  104. AVPacket *pkt;
  105. av_log_set_level(AV_LOG_TRACE);
  106. if (argc <= 2) {
  107. fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
  108. exit(0);
  109. }
  110. filename = argv[1];
  111. outfilename = argv[2];
  112. pkt = av_packet_alloc();
  113. if (!pkt)
  114. exit(1);
  115. /* set end of buffer to 0 (this ensures that no overreading happens for damaged MPEG streams) */
  116. memset(inbuf + INBUF_SIZE, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  117. /* find the MPEG-1 video decoder */
  118. // codec = avcodec_find_decoder(AV_CODEC_ID_H264); // AV_CODEC_ID_MPEG1VIDEO
  119. // if (!codec) {
  120. // fprintf(stderr, "Codec not found\n");
  121. // exit(1);
  122. // }
  123. /* find the video decoder: ie: h264_v4l2m2m */
  124. codec = avcodec_find_decoder_by_name("h264"); // h264_omx
  125. if (!codec) {
  126. err("Codec not found\n");
  127. exit(1);
  128. }
  129. parser = av_parser_init(codec->id);
  130. if (!parser) {
  131. fprintf(stderr, "parser not found\n");
  132. exit(1);
  133. }
  134. c = avcodec_alloc_context3(codec);
  135. if (!c) {
  136. fprintf(stderr, "Could not allocate video codec context\n");
  137. exit(1);
  138. }
  139. printf("[%s,%d]: \n",__FUNCTION__,__LINE__);
  140. /* For some codecs, such as msmpeg4 and mpeg4, width and height
  141. MUST be initialized there because this information is not
  142. available in the bitstream. */
  143. /* open it */
  144. if (avcodec_open2(c, codec, NULL) < 0) {
  145. printf("[%s,%d]: \n",__FUNCTION__,__LINE__);
  146. fprintf(stderr, "Could not open codec\n");
  147. exit(1);
  148. }
  149. printf("[%s,%d]: \n",__FUNCTION__,__LINE__);
  150. f = fopen(filename, "rb");
  151. if (!f) {
  152. fprintf(stderr, "Could not open %s\n", filename);
  153. exit(1);
  154. }
  155. frame = av_frame_alloc();
  156. if (!frame) {
  157. fprintf(stderr, "Could not allocate video frame\n");
  158. exit(1);
  159. }
  160. while (!feof(f)) {
  161. /* read raw data from the input file */
  162. data_size = fread(inbuf, 1, INBUF_SIZE, f);
  163. if (!data_size)
  164. break;
  165. /* use the parser to split the data into frames */
  166. data = inbuf;
  167. while (data_size > 0) {
  168. ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
  169. data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
  170. if (ret < 0) {
  171. fprintf(stderr, "Error while parsing\n");
  172. exit(1);
  173. }
  174. data += ret;
  175. data_size -= ret;
  176. if (pkt->size)
  177. decode(c, frame, pkt, outfilename);
  178. }
  179. }
  180. /* flush the decoder */
  181. decode(c, frame, NULL, outfilename);
  182. fclose(f);
  183. av_parser_close(parser);
  184. avcodec_free_context(&c);
  185. av_frame_free(&frame);
  186. av_packet_free(&pkt);
  187. return 0;
  188. }