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.

252 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 int hw_decoder_init(AVCodecContext *ctx, const enum AVHWDeviceType type)
  43. {
  44. int err = 0;
  45. if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type,
  46. NULL, NULL, 0)) < 0) {
  47. fprintf(stderr, "Failed to create specified HW device.\n");
  48. return err;
  49. }
  50. ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
  51. return err;
  52. }
  53. static enum AVPixelFormat get_hw_format(AVCodecContext *ctx,
  54. const enum AVPixelFormat *pix_fmts)
  55. {
  56. const enum AVPixelFormat *p;
  57. for (p = pix_fmts; *p != -1; p++) {
  58. if (*p == hw_pix_fmt)
  59. return *p;
  60. }
  61. fprintf(stderr, "Failed to get HW surface format.\n");
  62. return AV_PIX_FMT_NONE;
  63. }
  64. static int decode_write(AVCodecContext *avctx, AVPacket *packet)
  65. {
  66. AVFrame *frame = NULL, *sw_frame = NULL;
  67. AVFrame *tmp_frame = NULL;
  68. uint8_t *buffer = NULL;
  69. int size;
  70. int ret = 0;
  71. ret = avcodec_send_packet(avctx, packet);
  72. if (ret < 0) {
  73. fprintf(stderr, "Error during decoding\n");
  74. return ret;
  75. }
  76. while (1) {
  77. if (!(frame = av_frame_alloc()) || !(sw_frame = av_frame_alloc())) {
  78. fprintf(stderr, "Can not alloc frame\n");
  79. ret = AVERROR(ENOMEM);
  80. goto fail;
  81. }
  82. ret = avcodec_receive_frame(avctx, frame);
  83. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  84. av_frame_free(&frame);
  85. av_frame_free(&sw_frame);
  86. return 0;
  87. } else if (ret < 0) {
  88. fprintf(stderr, "Error while decoding\n");
  89. goto fail;
  90. }
  91. if (frame->format == hw_pix_fmt) {
  92. /* retrieve data from GPU to CPU */
  93. if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) < 0) {
  94. fprintf(stderr, "Error transferring the data to system memory\n");
  95. goto fail;
  96. }
  97. tmp_frame = sw_frame;
  98. } else
  99. tmp_frame = frame;
  100. size = av_image_get_buffer_size(tmp_frame->format, tmp_frame->width,
  101. tmp_frame->height, 1);
  102. buffer = av_malloc(size);
  103. if (!buffer) {
  104. fprintf(stderr, "Can not alloc buffer\n");
  105. ret = AVERROR(ENOMEM);
  106. goto fail;
  107. }
  108. ret = av_image_copy_to_buffer(buffer, size,
  109. (const uint8_t * const *)tmp_frame->data,
  110. (const int *)tmp_frame->linesize, tmp_frame->format,
  111. tmp_frame->width, tmp_frame->height, 1);
  112. if (ret < 0) {
  113. fprintf(stderr, "Can not copy image to buffer\n");
  114. goto fail;
  115. }
  116. if ((ret = fwrite(buffer, 1, size, output_file)) < 0) {
  117. fprintf(stderr, "Failed to dump raw data.\n");
  118. goto fail;
  119. }
  120. fail:
  121. av_frame_free(&frame);
  122. av_frame_free(&sw_frame);
  123. av_freep(&buffer);
  124. if (ret < 0)
  125. return ret;
  126. }
  127. }
  128. int main(int argc, char *argv[])
  129. {
  130. AVFormatContext *input_ctx = NULL;
  131. int video_stream, ret;
  132. AVStream *video = NULL;
  133. AVCodecContext *decoder_ctx = NULL;
  134. AVCodec *decoder = NULL;
  135. AVPacket packet;
  136. enum AVHWDeviceType type;
  137. int i;
  138. if (argc < 4) {
  139. fprintf(stderr, "Usage: %s <device type> <input file> <output file>\n", argv[0]);
  140. return -1;
  141. }
  142. type = av_hwdevice_find_type_by_name(argv[1]);
  143. if (type == AV_HWDEVICE_TYPE_NONE) {
  144. fprintf(stderr, "Device type %s is not supported.\n", argv[1]);
  145. fprintf(stderr, "Available device types:");
  146. while((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
  147. fprintf(stderr, " %s", av_hwdevice_get_type_name(type));
  148. fprintf(stderr, "\n");
  149. return -1;
  150. }
  151. /* open the input file */
  152. if (avformat_open_input(&input_ctx, argv[2], NULL, NULL) != 0) {
  153. fprintf(stderr, "Cannot open input file '%s'\n", argv[2]);
  154. return -1;
  155. }
  156. if (avformat_find_stream_info(input_ctx, NULL) < 0) {
  157. fprintf(stderr, "Cannot find input stream information.\n");
  158. return -1;
  159. }
  160. /* find the video stream information */
  161. ret = av_find_best_stream(input_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0);
  162. if (ret < 0) {
  163. fprintf(stderr, "Cannot find a video stream in the input file\n");
  164. return -1;
  165. }
  166. video_stream = ret;
  167. for (i = 0;; i++) {
  168. const AVCodecHWConfig *config = avcodec_get_hw_config(decoder, i);
  169. if (!config) {
  170. fprintf(stderr, "Decoder %s does not support device type %s.\n",
  171. decoder->name, av_hwdevice_get_type_name(type));
  172. return -1;
  173. }
  174. if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
  175. config->device_type == type) {
  176. hw_pix_fmt = config->pix_fmt;
  177. break;
  178. }
  179. }
  180. if (!(decoder_ctx = avcodec_alloc_context3(decoder)))
  181. return AVERROR(ENOMEM);
  182. video = input_ctx->streams[video_stream];
  183. if (avcodec_parameters_to_context(decoder_ctx, video->codecpar) < 0)
  184. return -1;
  185. decoder_ctx->get_format = get_hw_format;
  186. av_opt_set_int(decoder_ctx, "refcounted_frames", 1, 0);
  187. if (hw_decoder_init(decoder_ctx, type) < 0)
  188. return -1;
  189. if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0) {
  190. fprintf(stderr, "Failed to open codec for stream #%u\n", video_stream);
  191. return -1;
  192. }
  193. /* open the file to dump raw data */
  194. output_file = fopen(argv[3], "w+");
  195. /* actual decoding and dump the raw data */
  196. while (ret >= 0) {
  197. if ((ret = av_read_frame(input_ctx, &packet)) < 0)
  198. break;
  199. if (video_stream == packet.stream_index)
  200. ret = decode_write(decoder_ctx, &packet);
  201. av_packet_unref(&packet);
  202. }
  203. /* flush the decoder */
  204. packet.data = NULL;
  205. packet.size = 0;
  206. ret = decode_write(decoder_ctx, &packet);
  207. av_packet_unref(&packet);
  208. if (output_file)
  209. fclose(output_file);
  210. avcodec_free_context(&decoder_ctx);
  211. avformat_close_input(&input_ctx);
  212. av_buffer_unref(&hw_device_ctx);
  213. return 0;
  214. }