ffmpeg-decode.c 8.8 KB

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