ffmpeg-decode.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 <util/util_uint128.h>
  16. #include <util/base.h>
  17. #include <obs-avc.h>
  18. int ffmpeg_decode_init(struct ffmpeg_decode *decode, enum AVCodecID id)
  19. {
  20. int ret;
  21. avcodec_register_all();
  22. memset(decode, 0, sizeof(*decode));
  23. decode->codec = avcodec_find_decoder(id);
  24. if (!decode->codec)
  25. return -1;
  26. decode->decoder = avcodec_alloc_context3(decode->codec);
  27. ret = avcodec_open2(decode->decoder, decode->codec, NULL);
  28. if (ret < 0) {
  29. ffmpeg_decode_free(decode);
  30. return ret;
  31. }
  32. if (decode->codec->capabilities & CODEC_CAP_TRUNCATED)
  33. decode->decoder->flags |= CODEC_FLAG_TRUNCATED;
  34. return 0;
  35. }
  36. void ffmpeg_decode_free(struct ffmpeg_decode *decode)
  37. {
  38. if (decode->decoder) {
  39. avcodec_close(decode->decoder);
  40. av_free(decode->decoder);
  41. }
  42. if (decode->frame)
  43. av_free(decode->frame);
  44. if (decode->packet_buffer)
  45. bfree(decode->packet_buffer);
  46. memset(decode, 0, sizeof(*decode));
  47. }
  48. static inline enum video_format convert_pixel_format(int f)
  49. {
  50. switch (f) {
  51. case AV_PIX_FMT_NONE: return VIDEO_FORMAT_NONE;
  52. case AV_PIX_FMT_YUV420P: return VIDEO_FORMAT_I420;
  53. case AV_PIX_FMT_NV12: return VIDEO_FORMAT_NV12;
  54. case AV_PIX_FMT_YUYV422: return VIDEO_FORMAT_YUY2;
  55. case AV_PIX_FMT_UYVY422: return VIDEO_FORMAT_UYVY;
  56. case AV_PIX_FMT_RGBA: return VIDEO_FORMAT_RGBA;
  57. case AV_PIX_FMT_BGRA: return VIDEO_FORMAT_BGRA;
  58. case AV_PIX_FMT_BGR0: return VIDEO_FORMAT_BGRX;
  59. default:;
  60. }
  61. return VIDEO_FORMAT_NONE;
  62. }
  63. static inline enum audio_format convert_sample_format(int f)
  64. {
  65. switch (f) {
  66. case AV_SAMPLE_FMT_U8: return AUDIO_FORMAT_U8BIT;
  67. case AV_SAMPLE_FMT_S16: return AUDIO_FORMAT_16BIT;
  68. case AV_SAMPLE_FMT_S32: return AUDIO_FORMAT_32BIT;
  69. case AV_SAMPLE_FMT_FLT: return AUDIO_FORMAT_FLOAT;
  70. case AV_SAMPLE_FMT_U8P: return AUDIO_FORMAT_U8BIT_PLANAR;
  71. case AV_SAMPLE_FMT_S16P: return AUDIO_FORMAT_16BIT_PLANAR;
  72. case AV_SAMPLE_FMT_S32P: return AUDIO_FORMAT_32BIT_PLANAR;
  73. case AV_SAMPLE_FMT_FLTP: return AUDIO_FORMAT_FLOAT_PLANAR;
  74. default:;
  75. }
  76. return AUDIO_FORMAT_UNKNOWN;
  77. }
  78. static inline void copy_data(struct ffmpeg_decode *decode, uint8_t *data,
  79. size_t size)
  80. {
  81. size_t new_size = size + FF_INPUT_BUFFER_PADDING_SIZE;
  82. if (decode->packet_size < new_size) {
  83. decode->packet_buffer = brealloc(decode->packet_buffer,
  84. new_size);
  85. }
  86. memset(decode->packet_buffer + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  87. memcpy(decode->packet_buffer, data, size);
  88. }
  89. static void do_idiotic_lgp_audio_packet_realignment(
  90. struct ffmpeg_decode *decode, long long *ts)
  91. {
  92. uint64_t new_ts = (uint64_t)*ts;
  93. util_uint128_t u128;
  94. if (!decode->lgp_started) {
  95. decode->lgp_start_ts = new_ts;
  96. decode->lgp_next_expected_ts = new_ts;
  97. decode->lgp_started = true;
  98. }
  99. if (llabs(decode->lgp_next_expected_ts - new_ts) < 3000000ULL) {
  100. *ts = (long long)decode->lgp_next_expected_ts;
  101. } else {
  102. decode->lgp_start_ts = new_ts;
  103. decode->lgp_frames_since_start = 0;
  104. }
  105. decode->lgp_frames_since_start += (uint64_t)decode->frame->nb_samples;
  106. u128 = util_mul64_64(decode->lgp_frames_since_start, 10000000ULL);
  107. decode->lgp_next_expected_ts = decode->lgp_start_ts +
  108. util_div128_32(u128, (uint32_t)decode->frame->sample_rate).low;
  109. }
  110. int ffmpeg_decode_audio(struct ffmpeg_decode *decode,
  111. uint8_t *data, size_t size, long long *ts,
  112. struct obs_source_audio *audio,
  113. bool *got_output)
  114. {
  115. AVPacket packet = {0};
  116. int got_frame = false;
  117. int len;
  118. *got_output = false;
  119. copy_data(decode, data, size);
  120. av_init_packet(&packet);
  121. packet.data = decode->packet_buffer;
  122. packet.size = (int)size;
  123. if (!decode->frame) {
  124. decode->frame = av_frame_alloc();
  125. if (!decode->frame)
  126. return -1;
  127. }
  128. len = avcodec_decode_audio4(decode->decoder, decode->frame, &got_frame,
  129. &packet);
  130. if (len <= 0 || !got_frame)
  131. return len;
  132. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  133. audio->data[i] = decode->frame->data[i];
  134. audio->samples_per_sec = decode->frame->sample_rate;
  135. audio->speakers = (enum speaker_layout)decode->decoder->channels;
  136. audio->format = convert_sample_format(decode->frame->format);
  137. audio->frames = decode->frame->nb_samples;
  138. if (audio->format == AUDIO_FORMAT_UNKNOWN)
  139. return 0;
  140. if (decode->fix_braindead_lgp_audio_packet_stupidity)
  141. do_idiotic_lgp_audio_packet_realignment(decode, ts);
  142. *got_output = true;
  143. return len;
  144. }
  145. int ffmpeg_decode_video(struct ffmpeg_decode *decode,
  146. uint8_t *data, size_t size, long long *ts,
  147. struct obs_source_frame *frame,
  148. bool *got_output)
  149. {
  150. AVPacket packet = {0};
  151. int got_frame = false;
  152. enum video_format new_format;
  153. int len;
  154. *got_output = false;
  155. copy_data(decode, data, size);
  156. av_init_packet(&packet);
  157. packet.data = decode->packet_buffer;
  158. packet.size = (int)size;
  159. packet.pts = *ts;
  160. if (decode->codec->id == AV_CODEC_ID_H264 &&
  161. obs_avc_keyframe(data, size))
  162. packet.flags |= AV_PKT_FLAG_KEY;
  163. if (!decode->frame) {
  164. decode->frame = av_frame_alloc();
  165. if (!decode->frame)
  166. return -1;
  167. }
  168. len = avcodec_decode_video2(decode->decoder, decode->frame, &got_frame,
  169. &packet);
  170. if (len <= 0 || !got_frame)
  171. return len;
  172. for (size_t i = 0; i < MAX_AV_PLANES; i++) {
  173. frame->data[i] = decode->frame->data[i];
  174. frame->linesize[i] = decode->frame->linesize[i];
  175. }
  176. new_format = convert_pixel_format(decode->frame->format);
  177. if (new_format != frame->format) {
  178. bool success;
  179. enum video_range_type range;
  180. frame->format = new_format;
  181. frame->full_range =
  182. decode->frame->color_range == AVCOL_RANGE_JPEG;
  183. range = frame->full_range ?
  184. VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
  185. success = video_format_get_parameters(VIDEO_CS_601,
  186. range, frame->color_matrix,
  187. frame->color_range_min, frame->color_range_max);
  188. if (!success) {
  189. blog(LOG_ERROR, "Failed to get video format "
  190. "parameters for video format %u",
  191. VIDEO_CS_601);
  192. return 0;
  193. }
  194. }
  195. *ts = decode->frame->pkt_pts;
  196. frame->width = decode->frame->width;
  197. frame->height = decode->frame->height;
  198. frame->flip = false;
  199. if (frame->format == VIDEO_FORMAT_NONE)
  200. return 0;
  201. *got_output = true;
  202. return len;
  203. }