当前位置:   article > 正文

07 - FFmpeg 更改视频分辨率 - 保存 yuv420p_ffmpeg无损修改视频分辨率

ffmpeg无损修改视频分辨率

---------------------------------------------------------------- 更改视频格式流程 ----------------------------------------------------------------

解码后的数据存储在data[0]、data[1]、data[2]中,
data[0]存储了linesize[0] * height个数据,
为对齐解码器的CPU及其他优化等原因,导致linesize[0],实际上并不等于宽度width, 而是比宽度大。

linesize[0] - Y分量对应的一个宽度

1、av_parse_video_size -- 解析分辨率
2、sws_getContext -- 获得分辨率切换的上下文
3、av_frame_alloc -- 图像转换后的数据需要存储起来
4、av_image_get_buffer_size -- 共创建多少字节的空间
5、av_malloc -- 创建【申请空间】
6、av_image_fill_arrays -- 格式化分配空间
7、sws_scale -- 视频处理

sws_scale() 函数 -- 清空无效数据
1、图像色彩空间转换;
2、分辨率缩放
3、前后图像滤波处理

  1. int ChangeResolutionInterface(AVCodecContext *codecCtx, AVPacket *packet, struct SwsContext *swsCtx, int destWidth, int destHeight, AVFrame *destFrame, FILE *dest_fp, int *frameCount)
  2. {
  3. AVFrame *frame = av_frame_alloc();
  4. if (frame == NULL)
  5. {
  6. av_log(NULL, AV_LOG_ERROR, "frame alloc failed!\n");
  7. }
  8. int ret = avcodec_send_packet(codecCtx, packet);
  9. if (ret != 0)
  10. {
  11. av_log(NULL, AV_LOG_ERROR, "send packet failed:%s\n", av_err2str(ret));
  12. av_packet_unref(packet);
  13. return -1;
  14. }
  15. while (avcodec_receive_frame(codecCtx, frame) == 0)
  16. {
  17. sws_scale(swsCtx, (const uint8_t *const *)frame->data, frame->linesize, 0, codecCtx->height, destFrame->data, destFrame->linesize); // 视频处理
  18. // 保存 sws_scale 处理后的 帧
  19. fwrite(destFrame->data[0], 1, destWidth * destHeight, dest_fp);
  20. fwrite(destFrame->data[1], 1, destWidth * destHeight / 4, dest_fp);
  21. fwrite(destFrame->data[2], 1, destWidth * destHeight / 4, dest_fp);
  22. (*frameCount)++;
  23. av_log(NULL, AV_LOG_DEBUG, "frameCount = %d, linesize[0] = %d, linesize[1] = %d, linesize[2] = %d, width = %d, heigth = %d\n",
  24. *frameCount, destFrame->linesize[0], destFrame->linesize[1], destFrame->linesize[2], destWidth, destHeight);
  25. }
  26. if (frame)
  27. {
  28. av_frame_free(&frame);
  29. }
  30. }
  1. int ChangeResolutionDecodeVideoYUV(const char *inFileName, const char *outFileName, char *destVideoSizeString)
  2. {
  3. int frameCount = 0;
  4. /**********************************************************************************/
  5. FILE *dest_fp = fopen(outFileName, "wb+");
  6. if (dest_fp == NULL)
  7. {
  8. av_log(NULL, AV_LOG_ERROR, "open outfile:%s failed!\n", outFileName);
  9. return -1;
  10. }
  11. /**********************************************************************************/
  12. // 分析分辨率
  13. int destWidth ,destHeight;
  14. int ret = av_parse_video_size(&destWidth, &destHeight, destVideoSizeString); // 解析分辨率
  15. if (ret < 0)
  16. {
  17. av_log(NULL, AV_LOG_ERROR, "invalid video size: %s\n", destVideoSizeString);
  18. return -1;
  19. }
  20. av_log(NULL, AV_LOG_INFO, "destWidth:%d, destHeight:%d\n", destWidth, destHeight);
  21. AVFormatContext *inFmtCtx = NULL;
  22. ret = avformat_open_input(&inFmtCtx, inFileName, NULL, NULL);
  23. if (ret != 0)
  24. {
  25. av_log(NULL, AV_LOG_ERROR, "open input file error\n");
  26. return -1;
  27. }
  28. ret = avformat_find_stream_info(inFmtCtx, NULL);
  29. if (ret < 0)
  30. {
  31. av_log(NULL, AV_LOG_ERROR, "find stream info error\n");
  32. goto fail;
  33. }
  34. int videoStreamIndex = av_find_best_stream(inFmtCtx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  35. if (videoStreamIndex < 0)
  36. {
  37. av_log(NULL, AV_LOG_ERROR, "alloc avcodec context failed!\n");
  38. goto fail;
  39. }
  40. AVCodecContext *codecCtx = avcodec_alloc_context3(NULL);
  41. if (codecCtx == NULL)
  42. {
  43. av_log(NULL, AV_LOG_ERROR, "alloc avcodec context failed!\n");
  44. goto fail;
  45. }
  46. avcodec_parameters_to_context(codecCtx, inFmtCtx->streams[videoStreamIndex]->codecpar);
  47. AVCodec *decoder = avcodec_find_decoder(codecCtx->codec_id);
  48. if (decoder == NULL)
  49. {
  50. av_log(NULL, AV_LOG_ERROR, "find decoder failed,codec_id:%d\n", codecCtx->codec_id);
  51. ret = -1;
  52. goto fail;
  53. }
  54. ret = avcodec_open2(codecCtx, decoder, NULL);
  55. if (ret != 0)
  56. {
  57. av_log(NULL, AV_LOG_ERROR, "open decoder failed:%s \n", av_err2str(ret));
  58. ret = -1;
  59. goto fail;
  60. }
  61. // 获得分辨率切换的上下文
  62. enum AVPixelFormat destPixFormat = codecCtx->pix_fmt;
  63. struct SwsContext *swsCtx = sws_getContext(codecCtx->width, codecCtx->height, codecCtx->pix_fmt,
  64. destWidth, destHeight, destPixFormat,
  65. SWS_FAST_BILINEAR, NULL, NULL, NULL);
  66. if (swsCtx == NULL)
  67. {
  68. av_log(NULL, AV_LOG_ERROR, "get sws context failed!\n");
  69. ret = -1;
  70. goto fail;
  71. }
  72. AVFrame *destFrame = av_frame_alloc(); // 图像转换后的数据需要存储起来
  73. uint8_t *outBuffer = av_malloc(av_image_get_buffer_size(destPixFormat, destWidth, destHeight, 1) /*共创建多少字节的空间*/);
  74. av_image_fill_arrays(destFrame->data, destFrame->linesize, outBuffer, destPixFormat, destWidth, destHeight, 1); // 格式化分配空间
  75. AVPacket packet;
  76. av_init_packet(&packet);
  77. while (av_read_frame(inFmtCtx, &packet) >= 0)
  78. {
  79. if (packet.stream_index == videoStreamIndex)
  80. {
  81. if (ChangeResolutionInterface(codecCtx, &packet, swsCtx, destWidth, destHeight, destFrame, dest_fp, &frameCount) == -1)
  82. {
  83. ret = -1;
  84. av_packet_unref(&packet);
  85. goto fail;
  86. }
  87. }
  88. av_packet_unref(&packet);
  89. }
  90. // flush decoder
  91. ChangeResolutionInterface(codecCtx, NULL, swsCtx, destWidth, destHeight, destFrame, dest_fp, &frameCount);
  92. fail:
  93. if (inFmtCtx)
  94. {
  95. av_log(NULL, AV_LOG_DEBUG, "inFmtCtx format close input\n");
  96. avformat_close_input(&inFmtCtx);
  97. }
  98. if (codecCtx)
  99. {
  100. av_log(NULL, AV_LOG_DEBUG, "codecCtx codec free context\n");
  101. avcodec_free_context(&codecCtx);
  102. }
  103. if (dest_fp)
  104. {
  105. av_log(NULL, AV_LOG_DEBUG, "dest_fp fclose\n");
  106. fclose(dest_fp);
  107. }
  108. if (destFrame)
  109. {
  110. av_log(NULL, AV_LOG_DEBUG, "destFrame av frame free\n");
  111. av_frame_free(&destFrame);
  112. }
  113. if (outBuffer)
  114. {
  115. av_log(NULL, AV_LOG_DEBUG, "outBuffer free\n");
  116. av_free(outBuffer);
  117. }
  118. return ret;
  119. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/837792
推荐阅读
相关标签
  

闽ICP备14008679号