ffmpeg-decode.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /******************************************************************************
  2. Copyright (C) 2014 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "ffmpeg-decode.h"
  15. #include "obs-ffmpeg-compat.h"
  16. #include <obs-avc.h>
  17. #if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(58, 4, 100)
  18. #define USE_NEW_HARDWARE_CODEC_METHOD
  19. #endif
  20. #ifdef USE_NEW_HARDWARE_CODEC_METHOD
  21. enum AVHWDeviceType hw_priority[] = {
  22. AV_HWDEVICE_TYPE_D3D11VA, AV_HWDEVICE_TYPE_DXVA2, AV_HWDEVICE_TYPE_QSV,
  23. AV_HWDEVICE_TYPE_CUDA, AV_HWDEVICE_TYPE_NONE,
  24. };
  25. static bool has_hw_type(AVCodec *c, enum AVHWDeviceType type)
  26. {
  27. for (int i = 0;; i++) {
  28. const AVCodecHWConfig *config = avcodec_get_hw_config(c, i);
  29. if (!config) {
  30. break;
  31. }
  32. if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
  33. config->device_type == type)
  34. return true;
  35. }
  36. return false;
  37. }
  38. static void init_hw_decoder(struct ffmpeg_decode *d)
  39. {
  40. enum AVHWDeviceType *priority = hw_priority;
  41. AVBufferRef *hw_ctx = NULL;
  42. while (*priority != AV_HWDEVICE_TYPE_NONE) {
  43. if (has_hw_type(d->codec, *priority)) {
  44. int ret = av_hwdevice_ctx_create(&hw_ctx, *priority,
  45. NULL, NULL, 0);
  46. if (ret == 0)
  47. break;
  48. }
  49. priority++;
  50. }
  51. if (hw_ctx) {
  52. d->decoder->hw_device_ctx = av_buffer_ref(hw_ctx);
  53. d->hw = true;
  54. }
  55. }
  56. #endif
  57. int ffmpeg_decode_init(struct ffmpeg_decode *decode, enum AVCodecID id,
  58. bool use_hw)
  59. {
  60. int ret;
  61. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  62. avcodec_register_all();
  63. #endif
  64. memset(decode, 0, sizeof(*decode));
  65. decode->codec = avcodec_find_decoder(id);
  66. if (!decode->codec)
  67. return -1;
  68. decode->decoder = avcodec_alloc_context3(decode->codec);
  69. decode->decoder->thread_count = 0;
  70. #ifdef USE_NEW_HARDWARE_CODEC_METHOD
  71. if (use_hw)
  72. init_hw_decoder(decode);
  73. #else
  74. (void)use_hw;
  75. #endif
  76. ret = avcodec_open2(decode->decoder, decode->codec, NULL);
  77. if (ret < 0) {
  78. ffmpeg_decode_free(decode);
  79. return ret;
  80. }
  81. if (decode->codec->capabilities & CODEC_CAP_TRUNC)
  82. decode->decoder->flags |= CODEC_FLAG_TRUNC;
  83. return 0;
  84. }
  85. void ffmpeg_decode_free(struct ffmpeg_decode *decode)
  86. {
  87. if (decode->hw_frame)
  88. av_free(decode->hw_frame);
  89. if (decode->decoder) {
  90. avcodec_close(decode->decoder);
  91. av_free(decode->decoder);
  92. }
  93. if (decode->frame)
  94. av_free(decode->frame);
  95. if (decode->packet_buffer)
  96. bfree(decode->packet_buffer);
  97. memset(decode, 0, sizeof(*decode));
  98. }
  99. static inline enum video_format convert_pixel_format(int f)
  100. {
  101. switch (f) {
  102. case AV_PIX_FMT_NONE:
  103. return VIDEO_FORMAT_NONE;
  104. case AV_PIX_FMT_YUV420P:
  105. case AV_PIX_FMT_YUVJ420P:
  106. return VIDEO_FORMAT_I420;
  107. case AV_PIX_FMT_NV12:
  108. return VIDEO_FORMAT_NV12;
  109. case AV_PIX_FMT_YUYV422:
  110. return VIDEO_FORMAT_YUY2;
  111. case AV_PIX_FMT_UYVY422:
  112. return VIDEO_FORMAT_UYVY;
  113. case AV_PIX_FMT_YUV422P:
  114. case AV_PIX_FMT_YUVJ422P:
  115. return VIDEO_FORMAT_I422;
  116. case AV_PIX_FMT_RGBA:
  117. return VIDEO_FORMAT_RGBA;
  118. case AV_PIX_FMT_BGRA:
  119. return VIDEO_FORMAT_BGRA;
  120. case AV_PIX_FMT_BGR0:
  121. return VIDEO_FORMAT_BGRX;
  122. default:;
  123. }
  124. return VIDEO_FORMAT_NONE;
  125. }
  126. static inline enum audio_format convert_sample_format(int f)
  127. {
  128. switch (f) {
  129. case AV_SAMPLE_FMT_U8:
  130. return AUDIO_FORMAT_U8BIT;
  131. case AV_SAMPLE_FMT_S16:
  132. return AUDIO_FORMAT_16BIT;
  133. case AV_SAMPLE_FMT_S32:
  134. return AUDIO_FORMAT_32BIT;
  135. case AV_SAMPLE_FMT_FLT:
  136. return AUDIO_FORMAT_FLOAT;
  137. case AV_SAMPLE_FMT_U8P:
  138. return AUDIO_FORMAT_U8BIT_PLANAR;
  139. case AV_SAMPLE_FMT_S16P:
  140. return AUDIO_FORMAT_16BIT_PLANAR;
  141. case AV_SAMPLE_FMT_S32P:
  142. return AUDIO_FORMAT_32BIT_PLANAR;
  143. case AV_SAMPLE_FMT_FLTP:
  144. return AUDIO_FORMAT_FLOAT_PLANAR;
  145. default:;
  146. }
  147. return AUDIO_FORMAT_UNKNOWN;
  148. }
  149. static inline enum speaker_layout convert_speaker_layout(uint8_t channels)
  150. {
  151. switch (channels) {
  152. case 0:
  153. return SPEAKERS_UNKNOWN;
  154. case 1:
  155. return SPEAKERS_MONO;
  156. case 2:
  157. return SPEAKERS_STEREO;
  158. case 3:
  159. return SPEAKERS_2POINT1;
  160. case 4:
  161. return SPEAKERS_4POINT0;
  162. case 5:
  163. return SPEAKERS_4POINT1;
  164. case 6:
  165. return SPEAKERS_5POINT1;
  166. case 8:
  167. return SPEAKERS_7POINT1;
  168. default:
  169. return SPEAKERS_UNKNOWN;
  170. }
  171. }
  172. static inline void copy_data(struct ffmpeg_decode *decode, uint8_t *data,
  173. size_t size)
  174. {
  175. size_t new_size = size + INPUT_BUFFER_PADDING_SIZE;
  176. if (decode->packet_size < new_size) {
  177. decode->packet_buffer =
  178. brealloc(decode->packet_buffer, new_size);
  179. decode->packet_size = new_size;
  180. }
  181. memset(decode->packet_buffer + size, 0, INPUT_BUFFER_PADDING_SIZE);
  182. memcpy(decode->packet_buffer, data, size);
  183. }
  184. bool ffmpeg_decode_audio(struct ffmpeg_decode *decode, uint8_t *data,
  185. size_t size, struct obs_source_audio *audio,
  186. bool *got_output)
  187. {
  188. AVPacket packet = {0};
  189. int got_frame = false;
  190. int ret = 0;
  191. *got_output = false;
  192. copy_data(decode, data, size);
  193. av_init_packet(&packet);
  194. packet.data = decode->packet_buffer;
  195. packet.size = (int)size;
  196. if (!decode->frame) {
  197. decode->frame = av_frame_alloc();
  198. if (!decode->frame)
  199. return false;
  200. }
  201. if (data && size)
  202. ret = avcodec_send_packet(decode->decoder, &packet);
  203. if (ret == 0)
  204. ret = avcodec_receive_frame(decode->decoder, decode->frame);
  205. got_frame = (ret == 0);
  206. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  207. ret = 0;
  208. if (ret < 0)
  209. return false;
  210. else if (!got_frame)
  211. return true;
  212. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  213. audio->data[i] = decode->frame->data[i];
  214. audio->samples_per_sec = decode->frame->sample_rate;
  215. audio->format = convert_sample_format(decode->frame->format);
  216. audio->speakers =
  217. convert_speaker_layout((uint8_t)decode->decoder->channels);
  218. audio->frames = decode->frame->nb_samples;
  219. if (audio->format == AUDIO_FORMAT_UNKNOWN)
  220. return false;
  221. *got_output = true;
  222. return true;
  223. }
  224. bool ffmpeg_decode_video(struct ffmpeg_decode *decode, uint8_t *data,
  225. size_t size, long long *ts,
  226. struct obs_source_frame2 *frame, bool *got_output)
  227. {
  228. AVPacket packet = {0};
  229. int got_frame = false;
  230. enum video_format new_format;
  231. AVFrame *out_frame;
  232. int ret;
  233. *got_output = false;
  234. copy_data(decode, data, size);
  235. av_init_packet(&packet);
  236. packet.data = decode->packet_buffer;
  237. packet.size = (int)size;
  238. packet.pts = *ts;
  239. if (decode->codec->id == AV_CODEC_ID_H264 &&
  240. obs_avc_keyframe(data, size))
  241. packet.flags |= AV_PKT_FLAG_KEY;
  242. if (!decode->frame) {
  243. decode->frame = av_frame_alloc();
  244. if (!decode->frame)
  245. return false;
  246. if (decode->hw && !decode->hw_frame) {
  247. decode->hw_frame = av_frame_alloc();
  248. if (!decode->hw_frame)
  249. return false;
  250. }
  251. }
  252. out_frame = decode->hw ? decode->hw_frame : decode->frame;
  253. ret = avcodec_send_packet(decode->decoder, &packet);
  254. if (ret == 0) {
  255. ret = avcodec_receive_frame(decode->decoder, out_frame);
  256. }
  257. got_frame = (ret == 0);
  258. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  259. ret = 0;
  260. if (ret < 0)
  261. return false;
  262. else if (!got_frame)
  263. return true;
  264. #ifdef USE_NEW_HARDWARE_CODEC_METHOD
  265. if (got_frame && decode->hw) {
  266. ret = av_hwframe_transfer_data(decode->frame, out_frame, 0);
  267. if (ret < 0) {
  268. return false;
  269. }
  270. }
  271. #endif
  272. for (size_t i = 0; i < MAX_AV_PLANES; i++) {
  273. frame->data[i] = decode->frame->data[i];
  274. frame->linesize[i] = decode->frame->linesize[i];
  275. }
  276. new_format = convert_pixel_format(decode->frame->format);
  277. if (new_format != frame->format) {
  278. bool success;
  279. frame->format = new_format;
  280. frame->range = decode->frame->color_range == AVCOL_RANGE_JPEG
  281. ? VIDEO_RANGE_FULL
  282. : VIDEO_RANGE_DEFAULT;
  283. success = video_format_get_parameters(
  284. VIDEO_CS_601, frame->range, frame->color_matrix,
  285. frame->color_range_min, frame->color_range_max);
  286. if (!success) {
  287. blog(LOG_ERROR,
  288. "Failed to get video format "
  289. "parameters for video format %u",
  290. VIDEO_CS_601);
  291. return false;
  292. }
  293. }
  294. *ts = decode->frame->pkt_pts;
  295. frame->width = decode->frame->width;
  296. frame->height = decode->frame->height;
  297. frame->flip = false;
  298. if (frame->format == VIDEO_FORMAT_NONE)
  299. return false;
  300. *got_output = true;
  301. return true;
  302. }