ffmpeg-decode.c 10 KB

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