ffmpeg-decode.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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,
  23. AV_HWDEVICE_TYPE_DXVA2,
  24. AV_HWDEVICE_TYPE_QSV,
  25. AV_HWDEVICE_TYPE_NONE,
  26. };
  27. static bool has_hw_type(AVCodec *c, enum AVHWDeviceType type)
  28. {
  29. for (int i = 0;; i++) {
  30. const AVCodecHWConfig *config = avcodec_get_hw_config(c, i);
  31. if (!config) {
  32. break;
  33. }
  34. if (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
  35. config->device_type == type)
  36. return true;
  37. }
  38. return false;
  39. }
  40. static void init_hw_decoder(struct ffmpeg_decode *d)
  41. {
  42. enum AVHWDeviceType *priority = hw_priority;
  43. AVBufferRef *hw_ctx = NULL;
  44. while (*priority != AV_HWDEVICE_TYPE_NONE) {
  45. if (has_hw_type(d->codec, *priority)) {
  46. int ret = av_hwdevice_ctx_create(&hw_ctx, *priority,
  47. NULL, NULL, 0);
  48. if (ret == 0)
  49. break;
  50. }
  51. priority++;
  52. }
  53. if (hw_ctx) {
  54. d->hw_device_ctx = hw_ctx;
  55. d->decoder->hw_device_ctx = av_buffer_ref(hw_ctx);
  56. d->hw = true;
  57. }
  58. }
  59. #endif
  60. int ffmpeg_decode_init(struct ffmpeg_decode *decode, enum AVCodecID id,
  61. bool use_hw)
  62. {
  63. int ret;
  64. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  65. avcodec_register_all();
  66. #endif
  67. memset(decode, 0, sizeof(*decode));
  68. decode->codec = avcodec_find_decoder(id);
  69. if (!decode->codec)
  70. return -1;
  71. decode->decoder = avcodec_alloc_context3(decode->codec);
  72. decode->decoder->thread_count = 0;
  73. #ifdef USE_NEW_HARDWARE_CODEC_METHOD
  74. if (use_hw)
  75. init_hw_decoder(decode);
  76. #else
  77. (void)use_hw;
  78. #endif
  79. ret = avcodec_open2(decode->decoder, decode->codec, NULL);
  80. if (ret < 0) {
  81. ffmpeg_decode_free(decode);
  82. return ret;
  83. }
  84. if (decode->codec->capabilities & CODEC_CAP_TRUNC)
  85. decode->decoder->flags |= CODEC_FLAG_TRUNC;
  86. return 0;
  87. }
  88. void ffmpeg_decode_free(struct ffmpeg_decode *decode)
  89. {
  90. if (decode->hw_frame)
  91. av_frame_free(&decode->hw_frame);
  92. if (decode->decoder)
  93. avcodec_free_context(&decode->decoder);
  94. if (decode->frame)
  95. av_frame_free(&decode->frame);
  96. if (decode->hw_device_ctx)
  97. av_buffer_unref(&decode->hw_device_ctx);
  98. if (decode->packet_buffer)
  99. bfree(decode->packet_buffer);
  100. memset(decode, 0, sizeof(*decode));
  101. }
  102. static inline enum video_format convert_pixel_format(int f)
  103. {
  104. switch (f) {
  105. case AV_PIX_FMT_NONE:
  106. return VIDEO_FORMAT_NONE;
  107. case AV_PIX_FMT_YUV420P:
  108. case AV_PIX_FMT_YUVJ420P:
  109. return VIDEO_FORMAT_I420;
  110. case AV_PIX_FMT_NV12:
  111. return VIDEO_FORMAT_NV12;
  112. case AV_PIX_FMT_YUYV422:
  113. return VIDEO_FORMAT_YUY2;
  114. case AV_PIX_FMT_UYVY422:
  115. return VIDEO_FORMAT_UYVY;
  116. case AV_PIX_FMT_YUV422P:
  117. case AV_PIX_FMT_YUVJ422P:
  118. return VIDEO_FORMAT_I422;
  119. case AV_PIX_FMT_RGBA:
  120. return VIDEO_FORMAT_RGBA;
  121. case AV_PIX_FMT_BGRA:
  122. return VIDEO_FORMAT_BGRA;
  123. case AV_PIX_FMT_BGR0:
  124. return VIDEO_FORMAT_BGRX;
  125. default:;
  126. }
  127. return VIDEO_FORMAT_NONE;
  128. }
  129. static inline enum audio_format convert_sample_format(int f)
  130. {
  131. switch (f) {
  132. case AV_SAMPLE_FMT_U8:
  133. return AUDIO_FORMAT_U8BIT;
  134. case AV_SAMPLE_FMT_S16:
  135. return AUDIO_FORMAT_16BIT;
  136. case AV_SAMPLE_FMT_S32:
  137. return AUDIO_FORMAT_32BIT;
  138. case AV_SAMPLE_FMT_FLT:
  139. return AUDIO_FORMAT_FLOAT;
  140. case AV_SAMPLE_FMT_U8P:
  141. return AUDIO_FORMAT_U8BIT_PLANAR;
  142. case AV_SAMPLE_FMT_S16P:
  143. return AUDIO_FORMAT_16BIT_PLANAR;
  144. case AV_SAMPLE_FMT_S32P:
  145. return AUDIO_FORMAT_32BIT_PLANAR;
  146. case AV_SAMPLE_FMT_FLTP:
  147. return AUDIO_FORMAT_FLOAT_PLANAR;
  148. default:;
  149. }
  150. return AUDIO_FORMAT_UNKNOWN;
  151. }
  152. static inline enum speaker_layout convert_speaker_layout(uint8_t channels)
  153. {
  154. switch (channels) {
  155. case 0:
  156. return SPEAKERS_UNKNOWN;
  157. case 1:
  158. return SPEAKERS_MONO;
  159. case 2:
  160. return SPEAKERS_STEREO;
  161. case 3:
  162. return SPEAKERS_2POINT1;
  163. case 4:
  164. return SPEAKERS_4POINT0;
  165. case 5:
  166. return SPEAKERS_4POINT1;
  167. case 6:
  168. return SPEAKERS_5POINT1;
  169. case 8:
  170. return SPEAKERS_7POINT1;
  171. default:
  172. return SPEAKERS_UNKNOWN;
  173. }
  174. }
  175. static inline void copy_data(struct ffmpeg_decode *decode, uint8_t *data,
  176. size_t size)
  177. {
  178. size_t new_size = size + INPUT_BUFFER_PADDING_SIZE;
  179. if (decode->packet_size < new_size) {
  180. decode->packet_buffer =
  181. brealloc(decode->packet_buffer, new_size);
  182. decode->packet_size = new_size;
  183. }
  184. memset(decode->packet_buffer + size, 0, INPUT_BUFFER_PADDING_SIZE);
  185. memcpy(decode->packet_buffer, data, size);
  186. }
  187. bool ffmpeg_decode_audio(struct ffmpeg_decode *decode, uint8_t *data,
  188. size_t size, struct obs_source_audio *audio,
  189. bool *got_output)
  190. {
  191. AVPacket packet = {0};
  192. int got_frame = false;
  193. int ret = 0;
  194. *got_output = false;
  195. copy_data(decode, data, size);
  196. av_init_packet(&packet);
  197. packet.data = decode->packet_buffer;
  198. packet.size = (int)size;
  199. if (!decode->frame) {
  200. decode->frame = av_frame_alloc();
  201. if (!decode->frame)
  202. return false;
  203. }
  204. if (data && size)
  205. ret = avcodec_send_packet(decode->decoder, &packet);
  206. if (ret == 0)
  207. ret = avcodec_receive_frame(decode->decoder, decode->frame);
  208. got_frame = (ret == 0);
  209. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  210. ret = 0;
  211. if (ret < 0)
  212. return false;
  213. else if (!got_frame)
  214. return true;
  215. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  216. audio->data[i] = decode->frame->data[i];
  217. audio->samples_per_sec = decode->frame->sample_rate;
  218. audio->format = convert_sample_format(decode->frame->format);
  219. audio->speakers =
  220. convert_speaker_layout((uint8_t)decode->decoder->channels);
  221. audio->frames = decode->frame->nb_samples;
  222. if (audio->format == AUDIO_FORMAT_UNKNOWN)
  223. return false;
  224. *got_output = true;
  225. return true;
  226. }
  227. static enum video_colorspace
  228. convert_color_space(enum AVColorSpace s, enum AVColorTransferCharacteristic trc)
  229. {
  230. switch (s) {
  231. case AVCOL_SPC_BT709:
  232. return (trc == AVCOL_TRC_IEC61966_2_1) ? VIDEO_CS_SRGB
  233. : VIDEO_CS_709;
  234. case AVCOL_SPC_FCC:
  235. case AVCOL_SPC_BT470BG:
  236. case AVCOL_SPC_SMPTE170M:
  237. case AVCOL_SPC_SMPTE240M:
  238. return VIDEO_CS_601;
  239. default:
  240. return VIDEO_CS_DEFAULT;
  241. }
  242. }
  243. bool ffmpeg_decode_video(struct ffmpeg_decode *decode, uint8_t *data,
  244. size_t size, long long *ts,
  245. enum video_range_type range,
  246. struct obs_source_frame2 *frame, bool *got_output)
  247. {
  248. AVPacket packet = {0};
  249. int got_frame = false;
  250. AVFrame *out_frame;
  251. int ret;
  252. *got_output = false;
  253. copy_data(decode, data, size);
  254. av_init_packet(&packet);
  255. packet.data = decode->packet_buffer;
  256. packet.size = (int)size;
  257. packet.pts = *ts;
  258. if (decode->codec->id == AV_CODEC_ID_H264 &&
  259. obs_avc_keyframe(data, size))
  260. packet.flags |= AV_PKT_FLAG_KEY;
  261. if (!decode->frame) {
  262. decode->frame = av_frame_alloc();
  263. if (!decode->frame)
  264. return false;
  265. if (decode->hw && !decode->hw_frame) {
  266. decode->hw_frame = av_frame_alloc();
  267. if (!decode->hw_frame)
  268. return false;
  269. }
  270. }
  271. out_frame = decode->hw ? decode->hw_frame : decode->frame;
  272. ret = avcodec_send_packet(decode->decoder, &packet);
  273. if (ret == 0) {
  274. ret = avcodec_receive_frame(decode->decoder, out_frame);
  275. }
  276. got_frame = (ret == 0);
  277. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  278. ret = 0;
  279. if (ret < 0)
  280. return false;
  281. else if (!got_frame)
  282. return true;
  283. #ifdef USE_NEW_HARDWARE_CODEC_METHOD
  284. if (got_frame && decode->hw) {
  285. ret = av_hwframe_transfer_data(decode->frame, out_frame, 0);
  286. if (ret < 0) {
  287. return false;
  288. }
  289. }
  290. #endif
  291. for (size_t i = 0; i < MAX_AV_PLANES; i++) {
  292. frame->data[i] = decode->frame->data[i];
  293. frame->linesize[i] = decode->frame->linesize[i];
  294. }
  295. frame->format = convert_pixel_format(decode->frame->format);
  296. if (range == VIDEO_RANGE_DEFAULT) {
  297. range = (decode->frame->color_range == AVCOL_RANGE_JPEG)
  298. ? VIDEO_RANGE_FULL
  299. : VIDEO_RANGE_PARTIAL;
  300. }
  301. const enum video_colorspace cs = convert_color_space(
  302. decode->frame->colorspace, decode->frame->color_trc);
  303. const bool success = video_format_get_parameters(
  304. cs, range, frame->color_matrix, frame->color_range_min,
  305. frame->color_range_max);
  306. if (!success) {
  307. blog(LOG_ERROR,
  308. "Failed to get video format "
  309. "parameters for video format %u",
  310. cs);
  311. return false;
  312. }
  313. frame->range = range;
  314. *ts = decode->frame->pts;
  315. frame->width = decode->frame->width;
  316. frame->height = decode->frame->height;
  317. frame->flip = false;
  318. if (frame->format == VIDEO_FORMAT_NONE)
  319. return false;
  320. *got_output = true;
  321. return true;
  322. }