ffmpeg4 解码264 输出rgb

举报
风吹稻花香 发表于 2021/06/04 22:47:07 2021/06/04
【摘要】     int play_file(char* file_name, FrameFunc tcallback(char* a, int size, int num, int height, int width)){ av_log_set_level(AV_LOG_WARNING); unsigned version = avcodec_version...

 

 


  
  1. int play_file(char* file_name, FrameFunc tcallback(char* a, int size, int num, int height, int width))
  2. {
  3. av_log_set_level(AV_LOG_WARNING);
  4. unsigned version = avcodec_version();
  5. printf("FFmpeg version: %d\n", version);
  6. AVFormatContext *avFormatCtx = NULL;
  7. int i, videoindex;
  8. AVCodecContext *avCodecCtx=NULL;
  9. AVCodec *avCodec;
  10. avformat_network_init();
  11. printf("1111\n");
  12. FILE *fp_open = fopen(file_name, "rb+");
  13. unsigned char *aviobuffer = (unsigned char *)av_malloc(32768);
  14. AVIOContext *avio = avio_alloc_context(aviobuffer, 32768, 0, (void*)fp_open, file_buffer, NULL, NULL);
  15. printf("222\n");
  16. avFormatCtx = avformat_alloc_context();
  17. avFormatCtx->pb = avio;
  18. //if (avformat_open_input(&pFormatCtx, patha.c_str(), NULL, NULL) != 0) {
  19. if (avformat_open_input(&avFormatCtx, NULL, NULL, NULL) != 0) {
  20. printf("Couldn't open input stream.\n");
  21. return -1;
  22. }
  23. avCodec = NULL;
  24. while (avCodec == NULL) {
  25. printf("start find stream info \n");
  26. if (avformat_find_stream_info(avFormatCtx, NULL) < 0) {
  27. printf("Couldn't find stream info\n");
  28. goto restart_stream;
  29. continue;
  30. }
  31. videoindex = -1;
  32. for (i = 0; i < avFormatCtx->nb_streams; i++)
  33. if (avFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  34. if (videoindex == -1) {
  35. videoindex = i;
  36. }
  37. //break;
  38. }
  39. if (videoindex == -1) {
  40. printf("Didn't find a video stream.\n");
  41. goto restart_stream;
  42. }
  43. avCodecCtx = avcodec_alloc_context3(NULL);
  44. avcodec_parameters_to_context(avCodecCtx, avFormatCtx->streams[videoindex]->codecpar);
  45. avCodec = avcodec_find_decoder(avCodecCtx->codec_id);
  46. if (avCodec == NULL) {
  47. printf("Codec not found \n");
  48. goto restart_stream;
  49. //return -1;
  50. }
  51. if (avcodec_open2(avCodecCtx, avCodec, NULL) < 0) {
  52. printf("Could not open codec.\n");
  53. goto restart_stream;
  54. continue;
  55. //return -1;
  56. }
  57. goto ok;
  58. restart_stream:
  59. printf("restart 1 ");
  60. avformat_free_context(avFormatCtx);
  61. printf("restart 2 ");
  62. //avformat_close_input(&pFormatCtx);
  63. avFormatCtx = NULL;
  64. avFormatCtx = avformat_alloc_context();
  65. printf("restart 3 ");
  66. av_freep(aviobuffer);
  67. //printf("restart 4");
  68. aviobuffer = (unsigned char *)av_malloc(32768);
  69. printf("restart 4 ");
  70. AVIOContext *avio2 = avio_alloc_context(aviobuffer, 32768, 0, &fp_open, file_buffer, NULL, NULL);
  71. avFormatCtx->pb = avio2;
  72. int open_ret = avformat_open_input(&avFormatCtx, NULL, NULL, NULL);
  73. if (open_ret != 0) {
  74. printf("2Couldn't open input stream %d\n", open_ret);
  75. return -1;
  76. }
  77. avFormatCtx->probesize = 1000 * 1024;
  78. avFormatCtx->max_analyze_duration = 10 * AV_TIME_BASE;
  79. printf("restart 5\n");
  80. avCodec = NULL;
  81. continue;
  82. ok:
  83. break;
  84. }
  85. AVFrame *pFrameYUV;
  86. AVFrame *pFrameRGB;
  87. pFrameYUV = av_frame_alloc();
  88. pFrameRGB = av_frame_alloc();
  89. uint8_t *out_buffer;
  90. struct SwsContext *img_convert_ctx;
  91. img_convert_ctx = sws_getContext(avCodecCtx->width, avCodecCtx->height, avCodecCtx->pix_fmt, avCodecCtx->width, avCodecCtx->height, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);
  92. out_buffer = new uint8_t[avpicture_get_size(AV_PIX_FMT_RGB24, avCodecCtx->width, avCodecCtx->height)];
  93. //avpicture_fill((AVPicture *)pFrameRGB, out_buffer, AV_PIX_FMT_RGB24, avCodecCtx->width, avCodecCtx->height);
  94. av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, out_buffer, AV_PIX_FMT_RGB24, avCodecCtx->width, avCodecCtx->height, 1);
  95. int ret, got_picture;
  96. AVPacket packet;// = (AVPacket *)av_malloc(sizeof(AVPacket));
  97. int need_decode = 1;
  98. int is_key_frame = 0;
  99. while (av_read_frame(avFormatCtx, &packet) >= 0) {
  100. if (packet.stream_index == videoindex) {
  101. DWORD start_time = GetTickCount();
  102. if (avcodec_send_packet(avCodecCtx, &packet) != 0) {
  103. printf("avcodec_send_packet错误\n");
  104. break;
  105. }
  106. /* if (packet.flags &AV_PKT_FLAG_KEY) {
  107. is_key_frame = 1;
  108. need_decode = 1;
  109. printf("key frame %d\n", need_decode);
  110. }
  111. else {
  112. is_key_frame = 0;
  113. }
  114. if (!need_decode) {
  115. av_packet_unref(&packet);
  116. printf("not key frame \n");
  117. continue;
  118. }*/
  119. while (avcodec_receive_frame(avCodecCtx, pFrameYUV) == 0) {
  120. int ret = avcodec_send_frame(avCodecCtx, pFrameYUV);
  121. pFrameYUV->pts++;
  122. printf("decode ok %d %u\n", avCodecCtx->flags, GetTickCount()-start_time);
  123. /*fwrite(pFrameYUV->data[0], (pCodecCtx->width)*(pCodecCtx->height) * 3, 1, output);*/
  124. sws_scale(img_convert_ctx, pFrameYUV->data, pFrameYUV->linesize, 0, avCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
  125. tcallback((char*)pFrameRGB->data[0], avCodecCtx->height * avCodecCtx->width * 3, 1, avCodecCtx->height, avCodecCtx->width);
  126. avcodec_flush_buffers(avCodecCtx);
  127. //av_frame_free(&pFrameYUV);
  128. }
  129. }
  130. av_packet_unref(&packet);
  131. }
  132. avcodec_close(avCodecCtx);
  133. avformat_close_input(&avFormatCtx);
  134. return 0;
  135. }

 

 

 

 

 

文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/87456509

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。