ffmpeg-decode.c 6.5 KB

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