ffmpeg-decode.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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_YUV420P10LE:
  124. return VIDEO_FORMAT_I010;
  125. case AV_PIX_FMT_BGR0:
  126. return VIDEO_FORMAT_BGRX;
  127. case AV_PIX_FMT_P010LE:
  128. return VIDEO_FORMAT_P010;
  129. default:;
  130. }
  131. return VIDEO_FORMAT_NONE;
  132. }
  133. static inline enum audio_format convert_sample_format(int f)
  134. {
  135. switch (f) {
  136. case AV_SAMPLE_FMT_U8:
  137. return AUDIO_FORMAT_U8BIT;
  138. case AV_SAMPLE_FMT_S16:
  139. return AUDIO_FORMAT_16BIT;
  140. case AV_SAMPLE_FMT_S32:
  141. return AUDIO_FORMAT_32BIT;
  142. case AV_SAMPLE_FMT_FLT:
  143. return AUDIO_FORMAT_FLOAT;
  144. case AV_SAMPLE_FMT_U8P:
  145. return AUDIO_FORMAT_U8BIT_PLANAR;
  146. case AV_SAMPLE_FMT_S16P:
  147. return AUDIO_FORMAT_16BIT_PLANAR;
  148. case AV_SAMPLE_FMT_S32P:
  149. return AUDIO_FORMAT_32BIT_PLANAR;
  150. case AV_SAMPLE_FMT_FLTP:
  151. return AUDIO_FORMAT_FLOAT_PLANAR;
  152. default:;
  153. }
  154. return AUDIO_FORMAT_UNKNOWN;
  155. }
  156. static inline enum speaker_layout convert_speaker_layout(uint8_t channels)
  157. {
  158. switch (channels) {
  159. case 0:
  160. return SPEAKERS_UNKNOWN;
  161. case 1:
  162. return SPEAKERS_MONO;
  163. case 2:
  164. return SPEAKERS_STEREO;
  165. case 3:
  166. return SPEAKERS_2POINT1;
  167. case 4:
  168. return SPEAKERS_4POINT0;
  169. case 5:
  170. return SPEAKERS_4POINT1;
  171. case 6:
  172. return SPEAKERS_5POINT1;
  173. case 8:
  174. return SPEAKERS_7POINT1;
  175. default:
  176. return SPEAKERS_UNKNOWN;
  177. }
  178. }
  179. static inline void copy_data(struct ffmpeg_decode *decode, uint8_t *data,
  180. size_t size)
  181. {
  182. size_t new_size = size + INPUT_BUFFER_PADDING_SIZE;
  183. if (decode->packet_size < new_size) {
  184. decode->packet_buffer =
  185. brealloc(decode->packet_buffer, new_size);
  186. decode->packet_size = new_size;
  187. }
  188. memset(decode->packet_buffer + size, 0, INPUT_BUFFER_PADDING_SIZE);
  189. memcpy(decode->packet_buffer, data, size);
  190. }
  191. bool ffmpeg_decode_audio(struct ffmpeg_decode *decode, uint8_t *data,
  192. size_t size, struct obs_source_audio *audio,
  193. bool *got_output)
  194. {
  195. int got_frame = false;
  196. int ret = 0;
  197. *got_output = false;
  198. copy_data(decode, data, size);
  199. if (!decode->frame) {
  200. decode->frame = av_frame_alloc();
  201. if (!decode->frame)
  202. return false;
  203. }
  204. if (data && size) {
  205. AVPacket *packet = av_packet_alloc();
  206. packet->data = decode->packet_buffer;
  207. packet->size = (int)size;
  208. ret = avcodec_send_packet(decode->decoder, packet);
  209. av_packet_free(&packet);
  210. }
  211. if (ret == 0)
  212. ret = avcodec_receive_frame(decode->decoder, decode->frame);
  213. got_frame = (ret == 0);
  214. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  215. ret = 0;
  216. if (ret < 0)
  217. return false;
  218. else if (!got_frame)
  219. return true;
  220. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  221. audio->data[i] = decode->frame->data[i];
  222. audio->samples_per_sec = decode->frame->sample_rate;
  223. audio->format = convert_sample_format(decode->frame->format);
  224. audio->speakers =
  225. convert_speaker_layout((uint8_t)decode->decoder->channels);
  226. audio->frames = decode->frame->nb_samples;
  227. if (audio->format == AUDIO_FORMAT_UNKNOWN)
  228. return false;
  229. *got_output = true;
  230. return true;
  231. }
  232. static enum video_colorspace
  233. convert_color_space(enum AVColorSpace s, enum AVColorTransferCharacteristic trc)
  234. {
  235. switch (s) {
  236. case AVCOL_SPC_BT709:
  237. return (trc == AVCOL_TRC_IEC61966_2_1) ? VIDEO_CS_SRGB
  238. : VIDEO_CS_709;
  239. case AVCOL_SPC_FCC:
  240. case AVCOL_SPC_BT470BG:
  241. case AVCOL_SPC_SMPTE170M:
  242. case AVCOL_SPC_SMPTE240M:
  243. return VIDEO_CS_601;
  244. case AVCOL_SPC_BT2020_NCL:
  245. return (trc == AVCOL_TRC_ARIB_STD_B67) ? VIDEO_CS_2100_HLG
  246. : VIDEO_CS_2100_PQ;
  247. default:
  248. return VIDEO_CS_DEFAULT;
  249. }
  250. }
  251. bool ffmpeg_decode_video(struct ffmpeg_decode *decode, uint8_t *data,
  252. size_t size, long long *ts,
  253. enum video_range_type range,
  254. struct obs_source_frame2 *frame, bool *got_output)
  255. {
  256. int got_frame = false;
  257. AVFrame *out_frame;
  258. int ret;
  259. *got_output = false;
  260. copy_data(decode, data, size);
  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. AVPacket *packet = av_packet_alloc();
  273. packet->data = decode->packet_buffer;
  274. packet->size = (int)size;
  275. packet->pts = *ts;
  276. if (decode->codec->id == AV_CODEC_ID_H264 &&
  277. obs_avc_keyframe(data, size))
  278. packet->flags |= AV_PKT_FLAG_KEY;
  279. ret = avcodec_send_packet(decode->decoder, packet);
  280. if (ret == 0) {
  281. ret = avcodec_receive_frame(decode->decoder, out_frame);
  282. }
  283. av_packet_free(&packet);
  284. got_frame = (ret == 0);
  285. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  286. ret = 0;
  287. if (ret < 0)
  288. return false;
  289. else if (!got_frame)
  290. return true;
  291. #ifdef USE_NEW_HARDWARE_CODEC_METHOD
  292. if (got_frame && decode->hw) {
  293. ret = av_hwframe_transfer_data(decode->frame, out_frame, 0);
  294. if (ret < 0) {
  295. return false;
  296. }
  297. }
  298. #endif
  299. for (size_t i = 0; i < MAX_AV_PLANES; i++) {
  300. frame->data[i] = decode->frame->data[i];
  301. frame->linesize[i] = decode->frame->linesize[i];
  302. }
  303. const enum video_format format =
  304. convert_pixel_format(decode->frame->format);
  305. frame->format = format;
  306. if (range == VIDEO_RANGE_DEFAULT) {
  307. range = (decode->frame->color_range == AVCOL_RANGE_JPEG)
  308. ? VIDEO_RANGE_FULL
  309. : VIDEO_RANGE_PARTIAL;
  310. }
  311. const enum video_colorspace cs = convert_color_space(
  312. decode->frame->colorspace, decode->frame->color_trc);
  313. const bool success = video_format_get_parameters_for_format(
  314. cs, range, format, frame->color_matrix, frame->color_range_min,
  315. frame->color_range_max);
  316. if (!success) {
  317. blog(LOG_ERROR,
  318. "Failed to get video format "
  319. "parameters for video format %u",
  320. cs);
  321. return false;
  322. }
  323. frame->range = range;
  324. *ts = decode->frame->pts;
  325. frame->width = decode->frame->width;
  326. frame->height = decode->frame->height;
  327. frame->flip = false;
  328. if (frame->format == VIDEO_FORMAT_NONE)
  329. return false;
  330. *got_output = true;
  331. return true;
  332. }