ffmpeg-decode.c 5.9 KB

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