ffmpeg-decode.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. decode->packet_size = new_size;
  84. }
  85. memset(decode->packet_buffer + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  86. memcpy(decode->packet_buffer, data, size);
  87. }
  88. int ffmpeg_decode_audio(struct ffmpeg_decode *decode,
  89. uint8_t *data, size_t size,
  90. struct obs_source_audio *audio,
  91. bool *got_output)
  92. {
  93. AVPacket packet = {0};
  94. int got_frame = false;
  95. int len;
  96. *got_output = false;
  97. copy_data(decode, data, size);
  98. av_init_packet(&packet);
  99. packet.data = decode->packet_buffer;
  100. packet.size = (int)size;
  101. if (!decode->frame) {
  102. decode->frame = av_frame_alloc();
  103. if (!decode->frame)
  104. return -1;
  105. }
  106. len = avcodec_decode_audio4(decode->decoder, decode->frame, &got_frame,
  107. &packet);
  108. if (len <= 0 || !got_frame)
  109. return len;
  110. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  111. audio->data[i] = decode->frame->data[i];
  112. audio->samples_per_sec = decode->frame->sample_rate;
  113. audio->speakers = (enum speaker_layout)decode->decoder->channels;
  114. audio->format = convert_sample_format(decode->frame->format);
  115. audio->frames = decode->frame->nb_samples;
  116. if (audio->format == AUDIO_FORMAT_UNKNOWN)
  117. return 0;
  118. *got_output = true;
  119. return len;
  120. }
  121. int ffmpeg_decode_video(struct ffmpeg_decode *decode,
  122. uint8_t *data, size_t size, long long *ts,
  123. struct obs_source_frame *frame,
  124. bool *got_output)
  125. {
  126. AVPacket packet = {0};
  127. int got_frame = false;
  128. enum video_format new_format;
  129. int len;
  130. *got_output = false;
  131. copy_data(decode, data, size);
  132. av_init_packet(&packet);
  133. packet.data = decode->packet_buffer;
  134. packet.size = (int)size;
  135. packet.pts = *ts;
  136. if (decode->codec->id == AV_CODEC_ID_H264 &&
  137. obs_avc_keyframe(data, size))
  138. packet.flags |= AV_PKT_FLAG_KEY;
  139. if (!decode->frame) {
  140. decode->frame = av_frame_alloc();
  141. if (!decode->frame)
  142. return -1;
  143. }
  144. len = avcodec_decode_video2(decode->decoder, decode->frame, &got_frame,
  145. &packet);
  146. if (len <= 0 || !got_frame)
  147. return len;
  148. for (size_t i = 0; i < MAX_AV_PLANES; i++) {
  149. frame->data[i] = decode->frame->data[i];
  150. frame->linesize[i] = decode->frame->linesize[i];
  151. }
  152. new_format = convert_pixel_format(decode->frame->format);
  153. if (new_format != frame->format) {
  154. bool success;
  155. enum video_range_type range;
  156. frame->format = new_format;
  157. frame->full_range =
  158. decode->frame->color_range == AVCOL_RANGE_JPEG;
  159. range = frame->full_range ?
  160. VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
  161. success = video_format_get_parameters(VIDEO_CS_601,
  162. range, frame->color_matrix,
  163. frame->color_range_min, frame->color_range_max);
  164. if (!success) {
  165. blog(LOG_ERROR, "Failed to get video format "
  166. "parameters for video format %u",
  167. VIDEO_CS_601);
  168. return 0;
  169. }
  170. }
  171. *ts = decode->frame->pkt_pts;
  172. frame->width = decode->frame->width;
  173. frame->height = decode->frame->height;
  174. frame->flip = false;
  175. if (frame->format == VIDEO_FORMAT_NONE)
  176. return 0;
  177. *got_output = true;
  178. return len;
  179. }