ffmpeg-decode.c 10.0 KB

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