ffmpeg-decode.c 9.8 KB

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