hw_decode.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2017 Jun Zhao
  3. * Copyright (c) 2017 Kaixuan Liu
  4. *
  5. * HW Acceleration API (video decoding) decode sample
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. /**
  26. * @file
  27. * HW-Accelerated decoding example.
  28. *
  29. * @example hw_decode.c
  30. * This example shows how to do HW-accelerated decoding with output
  31. * frames from the HW video surfaces.
  32. */
  33. #include <stdio.h>
  34. #include <libavcodec/avcodec.h>
  35. #include <libavformat/avformat.h>
  36. #include <libavutil/pixdesc.h>
  37. #include <libavutil/hwcontext.h>
  38. #include <libavutil/opt.h>
  39. #include <libavutil/avassert.h>
  40. #include <libavutil/imgutils.h>
  41. static AVBufferRef *hw_device_ctx = NULL;
  42. static enum AVPixelFormat hw_pix_fmt;
  43. static FILE *output_file = NULL;
  44. static int hw_decoder_init(AVCodecContext *ctx, const enum AVHWDeviceType type)
  45. {
  46. int err = 0;
  47. if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type,
  48. NULL, NULL, 0)) < 0) {
  49. fprintf(stderr, "Failed to create specified HW device.\n");
  50. return err;
  51. }
  52. ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
  53. return err;
  54. }
  55. static enum AVPixelFormat get_hw_format(AVCodecContext *ctx,
  56. const enum AVPixelFormat *pix_fmts)
  57. {
  58. const enum AVPixelFormat *p;
  59. for (p = pix_fmts; *p != -1; p++) {
  60. if (*p == hw_pix_fmt)
  61. return *p;
  62. }
  63. fprintf(stderr, "Failed to get HW surface format.\n");
  64. return AV_PIX_FMT_NONE;
  65. }
  66. static int decode_write(AVCodecContext *avctx, AVPacket *packet)
  67. {
  68. AVFrame *frame = NULL, *sw_frame = NULL;
  69. AVFrame *tmp_frame = NULL;
  70. uint8_t *buffer = NULL;
  71. int size;
  72. int ret = 0;
  73. ret = avcodec_send_packet(avctx, packet);
  74. if (ret < 0) {
  75. fprintf(stderr, "Error during decoding\n");
  76. return ret;
  77. }
  78. while (1) {
  79. if (!(frame = av_frame_alloc()) || !(sw_frame = av_frame_alloc())) {
  80. fprintf(stderr, "Can not alloc frame\n");
  81. ret = AVERROR(ENOMEM);
  82. goto fail;
  83. }
  84. ret = avcodec_receive_frame(avctx, frame);
  85. if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
  86. av_frame_free(&frame);
  87. av_frame_free(&sw_frame);
  88. return 0;
  89. } else if (ret < 0) {
  90. fprintf(stderr, "Error while decoding\n");
  91. goto fail;
  92. }
  93. if (frame->format == hw_pix_fmt) {
  94. /* retrieve data from GPU to CPU */
  95. if ((ret = av_hwframe_transfer_data(sw_frame, frame, 0)) < 0) {
  96. fprintf(stderr, "Error transferring the data to system memory\n");
  97. goto fail;
  98. }
  99. tmp_frame = sw_frame;
  100. } else
  101. tmp_frame = frame;
  102. size = av_image_get_buffer_size(tmp_frame->format, tmp_frame->width,
  103. tmp_frame->height, 1);
  104. buffer = av_malloc(size);
  105. if (!buffer) {
  106. fprintf(stderr, "Can not alloc buffer\n");
  107. ret = AVERROR(ENOMEM);
  108. goto fail;
  109. }
  110. ret = av_image_copy_to_buffer(buffer, size,
  111. (const uint8_t * const *)tmp_frame->data,
  112. (const int *)tmp_frame->linesize, tmp_frame->format,
  113. tmp_frame->width, tmp_frame->height, 1);
  114. if (ret < 0) {
  115. fprintf(stderr, "Can not copy image to buffer\n");
  116. goto fail;
  117. }
  118. if ((ret = fwrite(buffer, 1, size, output_file)) < 0) {
  119. fprintf(stderr, "Failed to dump raw data.\n");
  120. goto fail;
  121. }
  122. fail:
  123. av_frame_free(&frame);
  124. av_frame_free(&sw_frame);
  125. av_freep(&buffer);
  126. if (ret < 0)
  127. return ret;
  128. }
  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. 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. }