You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
7.5KB

  1. /*
  2. * Copyright (c) 2017 Jun Zhao
  3. * Copyright (c) 2017 Kaixuan Liu
  4. *
  5. * HW Acceleration API (video decoding) decode sample
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * HW-Accelerated decoding example.
  26. *
  27. * @example hw_decode.c
  28. * This example shows how to do HW-accelerated decoding with output
  29. * frames from the HW video surfaces.
  30. */
  31. #include <stdio.h>
  32. #include <libavcodec/avcodec.h>
  33. #include <libavformat/avformat.h>
  34. #include <libavutil/pixdesc.h>
  35. #include <libavutil/hwcontext.h>
  36. #include <libavutil/opt.h>
  37. #include <libavutil/avassert.h>
  38. #include <libavutil/imgutils.h>
  39. static AVBufferRef *hw_device_ctx = NULL;
  40. static enum AVPixelFormat hw_pix_fmt;
  41. static FILE *output_file = NULL;
  42. static enum AVPixelFormat find_fmt_by_hw_type(const enum AVHWDeviceType type)
  43. {
  44. enum AVPixelFormat fmt;
  45. switch (type) {
  46. case AV_HWDEVICE_TYPE_VAAPI:
  47. fmt = AV_PIX_FMT_VAAPI;
  48. break;
  49. case AV_HWDEVICE_TYPE_DXVA2:
  50. fmt = AV_PIX_FMT_DXVA2_VLD;
  51. break;
  52. case AV_HWDEVICE_TYPE_D3D11VA:
  53. fmt = AV_PIX_FMT_D3D11;
  54. break;
  55. case AV_HWDEVICE_TYPE_VDPAU:
  56. fmt = AV_PIX_FMT_VDPAU;
  57. break;
  58. case AV_HWDEVICE_TYPE_VIDEOTOOLBOX:
  59. fmt = AV_PIX_FMT_VIDEOTOOLBOX;
  60. break;
  61. default:
  62. fmt = AV_PIX_FMT_NONE;
  63. break;
  64. }
  65. return fmt;
  66. }
  67. static int hw_decoder_init(AVCodecContext *ctx, const enum AVHWDeviceType type)
  68. {
  69. int err = 0;
  70. if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type,
  71. NULL, NULL, 0)) < 0) {
  72. fprintf(stderr, "Failed to create specified HW device.\n");
  73. return err;
  74. }
  75. ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
  76. return err;
  77. }
  78. static enum AVPixelFormat get_hw_format(AVCodecContext *ctx,
  79. const enum AVPixelFormat *pix_fmts)
  80. {
  81. const enum AVPixelFormat *p;
  82. for (p = pix_fmts; *p != -1; p++) {
  83. if (*p == hw_pix_fmt)
  84. return *p;
  85. }
  86. fprintf(stderr, "Failed to get HW surface format.\n");
  87. return AV_PIX_FMT_NONE;
  88. }
  89. static int decode_write(AVCodecContext *avctx, AVPacket *packet)
  90. {
  91. AVFrame *frame = NULL, *sw_frame = NULL;
  92. AVFrame *tmp_frame = NULL;
  93. uint8_t *buffer = NULL;
  94. int size;
  95. int ret = 0;
  96. ret = avcodec_send_packet(avctx, packet);
  97. if (ret < 0) {
  98. fprintf(stderr, "Error during decoding\n");
  99. return ret;
  100. }
  101. while (ret >= 0) {
  102. if (!(frame = av_frame_alloc()) || !(sw_frame = av_frame_alloc())) {
  103. fprintf(stderr, "Can not alloc frame\n");
  104. ret = AVERROR(ENOMEM);
  105. goto fail;
  106. }
  107. ret = avcodec_receive_frame(avctx, frame);
  108. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  109. av_frame_free(&frame);
  110. av_frame_free(&sw_frame);
  111. return 0;
  112. } else if (ret < 0) {
  113. fprintf(stderr, "Error while decoding\n");
  114. goto fail;
  115. }
  116. if (frame->format == hw_pix_fmt) {
  117. /* retrieve data from GPU to CPU */
  118. if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) < 0) {
  119. fprintf(stderr, "Error transferring the data to system memory\n");
  120. goto fail;
  121. }
  122. tmp_frame = sw_frame;
  123. } else
  124. tmp_frame = frame;
  125. size = av_image_get_buffer_size(tmp_frame->format, tmp_frame->width,
  126. tmp_frame->height, 1);
  127. buffer = av_malloc(size);
  128. if (!buffer) {
  129. fprintf(stderr, "Can not alloc buffer\n");
  130. ret = AVERROR(ENOMEM);
  131. goto fail;
  132. }
  133. ret = av_image_copy_to_buffer(buffer, size,
  134. (const uint8_t * const *)tmp_frame->data,
  135. (const int *)tmp_frame->linesize, tmp_frame->format,
  136. tmp_frame->width, tmp_frame->height, 1);
  137. if (ret < 0) {
  138. fprintf(stderr, "Can not copy image to buffer\n");
  139. goto fail;
  140. }
  141. if ((ret = fwrite(buffer, 1, size, output_file)) < 0) {
  142. fprintf(stderr, "Failed to dump raw data.\n");
  143. goto fail;
  144. }
  145. fail:
  146. av_frame_free(&frame);
  147. av_frame_free(&sw_frame);
  148. if (buffer)
  149. av_freep(&buffer);
  150. if (ret < 0)
  151. return ret;
  152. }
  153. return 0;
  154. }
  155. int main(int argc, char *argv[])
  156. {
  157. AVFormatContext *input_ctx = NULL;
  158. int video_stream, ret;
  159. AVStream *video = NULL;
  160. AVCodecContext *decoder_ctx = NULL;
  161. AVCodec *decoder = NULL;
  162. AVPacket packet;
  163. enum AVHWDeviceType type;
  164. if (argc < 4) {
  165. fprintf(stderr, "Usage: %s <vaapi|vdpau|dxva2|d3d11va> <input file> <output file>\n", argv[0]);
  166. return -1;
  167. }
  168. av_register_all();
  169. type = av_hwdevice_find_type_by_name(argv[1]);
  170. hw_pix_fmt = find_fmt_by_hw_type(type);
  171. if (hw_pix_fmt == -1) {
  172. fprintf(stderr, "Cannot support '%s' in this example.\n", argv[1]);
  173. return -1;
  174. }
  175. /* open the input file */
  176. if (avformat_open_input(&input_ctx, argv[2], NULL, NULL) != 0) {
  177. fprintf(stderr, "Cannot open input file '%s'\n", argv[2]);
  178. return -1;
  179. }
  180. if (avformat_find_stream_info(input_ctx, NULL) < 0) {
  181. fprintf(stderr, "Cannot find input stream information.\n");
  182. return -1;
  183. }
  184. /* find the video stream information */
  185. ret = av_find_best_stream(input_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0);
  186. if (ret < 0) {
  187. fprintf(stderr, "Cannot find a video stream in the input file\n");
  188. return -1;
  189. }
  190. video_stream = ret;
  191. if (!(decoder_ctx = avcodec_alloc_context3(decoder)))
  192. return AVERROR(ENOMEM);
  193. video = input_ctx->streams[video_stream];
  194. if (avcodec_parameters_to_context(decoder_ctx, video->codecpar) < 0)
  195. return -1;
  196. decoder_ctx->get_format = get_hw_format;
  197. av_opt_set_int(decoder_ctx, "refcounted_frames", 1, 0);
  198. if (hw_decoder_init(decoder_ctx, type) < 0)
  199. return -1;
  200. if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0) {
  201. fprintf(stderr, "Failed to open codec for stream #%u\n", video_stream);
  202. return -1;
  203. }
  204. /* open the file to dump raw data */
  205. output_file = fopen(argv[3], "w+");
  206. /* actual decoding and dump the raw data */
  207. while (ret >= 0) {
  208. if ((ret = av_read_frame(input_ctx, &packet)) < 0)
  209. break;
  210. if (video_stream == packet.stream_index)
  211. ret = decode_write(decoder_ctx, &packet);
  212. av_packet_unref(&packet);
  213. }
  214. /* flush the decoder */
  215. packet.data = NULL;
  216. packet.size = 0;
  217. ret = decode_write(decoder_ctx, &packet);
  218. av_packet_unref(&packet);
  219. if (output_file)
  220. fclose(output_file);
  221. avcodec_free_context(&decoder_ctx);
  222. avformat_close_input(&input_ctx);
  223. av_buffer_unref(&hw_device_ctx);
  224. return 0;
  225. }