ffmpeg-decode.c 6.8 KB

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