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.

255 lines
7.6KB

  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 (ret >= 0) {
  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. if (buffer)
  124. av_freep(&buffer);
  125. if (ret < 0)
  126. return ret;
  127. }
  128. return 0;
  129. }
  130. int main(int argc, char *argv[])
  131. {
  132. AVFormatContext *input_ctx = NULL;
  133. int video_stream, ret;
  134. AVStream *video = NULL;
  135. AVCodecContext *decoder_ctx = NULL;
  136. AVCodec *decoder = NULL;
  137. AVPacket packet;
  138. enum AVHWDeviceType type;
  139. int i;
  140. if (argc < 4) {
  141. fprintf(stderr, "Usage: %s <device type> <input file> <output file>\n", argv[0]);
  142. return -1;
  143. }
  144. type = av_hwdevice_find_type_by_name(argv[1]);
  145. if (type == AV_HWDEVICE_TYPE_NONE) {
  146. fprintf(stderr, "Device type %s is not supported.\n", argv[1]);
  147. fprintf(stderr, "Available device types:");
  148. while((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
  149. fprintf(stderr, " %s", av_hwdevice_get_type_name(type));
  150. fprintf(stderr, "\n");
  151. return -1;
  152. }
  153. /* open the input file */
  154. if (avformat_open_input(&input_ctx, argv[2], NULL, NULL) != 0) {
  155. fprintf(stderr, "Cannot open input file '%s'\n", argv[2]);
  156. return -1;
  157. }
  158. if (avformat_find_stream_info(input_ctx, NULL) < 0) {
  159. fprintf(stderr, "Cannot find input stream information.\n");
  160. return -1;
  161. }
  162. /* find the video stream information */
  163. ret = av_find_best_stream(input_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, &decoder, 0);
  164. if (ret < 0) {
  165. fprintf(stderr, "Cannot find a video stream in the input file\n");
  166. return -1;
  167. }
  168. video_stream = ret;
  169. for (i = 0;; i++) {
  170. const AVCodecHWConfig *config = avcodec_get_hw_config(decoder, i);
  171. if (!config) {
  172. fprintf(stderr, "Decoder %s does not support device type %s.\n",
  173. decoder->name, av_hwdevice_get_type_name(type));
  174. return -1;
  175. }
  176. if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
  177. config->device_type == type) {
  178. hw_pix_fmt = config->pix_fmt;
  179. break;
  180. }
  181. }
  182. if (!(decoder_ctx = avcodec_alloc_context3(decoder)))
  183. return AVERROR(ENOMEM);
  184. video = input_ctx->streams[video_stream];
  185. if (avcodec_parameters_to_context(decoder_ctx, video->codecpar) < 0)
  186. return -1;
  187. decoder_ctx->get_format = get_hw_format;
  188. av_opt_set_int(decoder_ctx, "refcounted_frames", 1, 0);
  189. if (hw_decoder_init(decoder_ctx, type) < 0)
  190. return -1;
  191. if ((ret = avcodec_open2(decoder_ctx, decoder, NULL)) < 0) {
  192. fprintf(stderr, "Failed to open codec for stream #%u\n", video_stream);
  193. return -1;
  194. }
  195. /* open the file to dump raw data */
  196. output_file = fopen(argv[3], "w+");
  197. /* actual decoding and dump the raw data */
  198. while (ret >= 0) {
  199. if ((ret = av_read_frame(input_ctx, &packet)) < 0)
  200. break;
  201. if (video_stream == packet.stream_index)
  202. ret = decode_write(decoder_ctx, &packet);
  203. av_packet_unref(&packet);
  204. }
  205. /* flush the decoder */
  206. packet.data = NULL;
  207. packet.size = 0;
  208. ret = decode_write(decoder_ctx, &packet);
  209. av_packet_unref(&packet);
  210. if (output_file)
  211. fclose(output_file);
  212. avcodec_free_context(&decoder_ctx);
  213. avformat_close_input(&input_ctx);
  214. av_buffer_unref(&hw_device_ctx);
  215. return 0;
  216. }