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.

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