| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "services/media/framework_ffmpeg/av_codec_context.h" | 6 #include "services/media/framework_ffmpeg/av_codec_context.h" |
| 7 #include "services/media/framework_ffmpeg/ffmpeg_init.h" | 7 #include "services/media/framework_ffmpeg/ffmpeg_init.h" |
| 8 extern "C" { | 8 extern "C" { |
| 9 #include "third_party/ffmpeg/libavformat/avformat.h" | 9 #include "third_party/ffmpeg/libavformat/avformat.h" |
| 10 } | 10 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 size_t byte_count = bytes.size(); | 48 size_t byte_count = bytes.size(); |
| 49 uint8_t* copy = reinterpret_cast<uint8_t*>(malloc(byte_count)); | 49 uint8_t* copy = reinterpret_cast<uint8_t*>(malloc(byte_count)); |
| 50 std::memcpy(copy, bytes.data(), byte_count); | 50 std::memcpy(copy, bytes.data(), byte_count); |
| 51 context->extradata = copy; | 51 context->extradata = copy; |
| 52 context->extradata_size = byte_count; | 52 context->extradata_size = byte_count; |
| 53 } | 53 } |
| 54 | 54 |
| 55 // Creates a StreamType from an AVCodecContext describing an LPCM type. | 55 // Creates a StreamType from an AVCodecContext describing an LPCM type. |
| 56 std::unique_ptr<StreamType> StreamTypeFromLpcmCodecContext( | 56 std::unique_ptr<StreamType> StreamTypeFromLpcmCodecContext( |
| 57 const AVCodecContext& from) { | 57 const AVCodecContext& from) { |
| 58 return LpcmStreamType::Create( | 58 return LpcmStreamType::Create(Convert(from.sample_fmt), from.channels, |
| 59 Convert(from.sample_fmt), | 59 from.sample_rate); |
| 60 from.channels, | |
| 61 from.sample_rate); | |
| 62 } | 60 } |
| 63 | 61 |
| 64 // Creates a StreamType from an AVCodecContext describing a compressed audio | 62 // Creates a StreamType from an AVCodecContext describing a compressed audio |
| 65 // type. | 63 // type. |
| 66 std::unique_ptr<StreamType> | 64 std::unique_ptr<StreamType> StreamTypeFromCompressedAudioCodecContext( |
| 67 StreamTypeFromCompressedAudioCodecContext(const AVCodecContext& from) { | 65 const AVCodecContext& from) { |
| 68 CompressedAudioStreamType::AudioEncoding encoding; | 66 CompressedAudioStreamType::AudioEncoding encoding; |
| 69 switch (from.codec_id) { | 67 switch (from.codec_id) { |
| 70 case CODEC_ID_VORBIS: | 68 case CODEC_ID_VORBIS: |
| 71 encoding = CompressedAudioStreamType::AudioEncoding::kVorbis; | 69 encoding = CompressedAudioStreamType::AudioEncoding::kVorbis; |
| 72 break; | 70 break; |
| 73 default: | 71 default: |
| 74 encoding = CompressedAudioStreamType::AudioEncoding::kUnknown; | 72 encoding = CompressedAudioStreamType::AudioEncoding::kUnknown; |
| 75 break; | 73 break; |
| 76 } | 74 } |
| 77 | 75 |
| 78 return CompressedAudioStreamType::Create( | 76 return CompressedAudioStreamType::Create( |
| 79 encoding, | 77 encoding, Convert(from.sample_fmt), from.channels, from.sample_rate, |
| 80 Convert(from.sample_fmt), | 78 from.extradata_size == 0 ? nullptr : Bytes::Create(from.extradata, |
| 81 from.channels, | 79 from.extradata_size)); |
| 82 from.sample_rate, | |
| 83 from.extradata_size == 0 ? | |
| 84 nullptr : | |
| 85 Bytes::Create(from.extradata, from.extradata_size)); | |
| 86 } | 80 } |
| 87 | 81 |
| 88 // Converts AVColorSpace and AVColorRange to ColorSpace. | 82 // Converts AVColorSpace and AVColorRange to ColorSpace. |
| 89 VideoStreamType::ColorSpace ColorSpaceFromAVColorSpaceAndRange( | 83 VideoStreamType::ColorSpace ColorSpaceFromAVColorSpaceAndRange( |
| 90 AVColorSpace color_space, | 84 AVColorSpace color_space, |
| 91 AVColorRange color_range) { | 85 AVColorRange color_range) { |
| 92 // TODO(dalesat): Blindly copied from Chromium. | 86 // TODO(dalesat): Blindly copied from Chromium. |
| 93 if (color_range == AVCOL_RANGE_JPEG) { | 87 if (color_range == AVCOL_RANGE_JPEG) { |
| 94 return VideoStreamType::ColorSpace::kJpeg; | 88 return VideoStreamType::ColorSpace::kJpeg; |
| 95 } | 89 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 126 case VideoStreamType::VideoProfile::kH264High444Predictive: | 120 case VideoStreamType::VideoProfile::kH264High444Predictive: |
| 127 return FF_PROFILE_H264_HIGH_444_PREDICTIVE; | 121 return FF_PROFILE_H264_HIGH_444_PREDICTIVE; |
| 128 case VideoStreamType::VideoProfile::kUnknown: | 122 case VideoStreamType::VideoProfile::kUnknown: |
| 129 case VideoStreamType::VideoProfile::kNotApplicable: | 123 case VideoStreamType::VideoProfile::kNotApplicable: |
| 130 case VideoStreamType::VideoProfile::kH264ScalableBaseline: | 124 case VideoStreamType::VideoProfile::kH264ScalableBaseline: |
| 131 case VideoStreamType::VideoProfile::kH264ScalableHigh: | 125 case VideoStreamType::VideoProfile::kH264ScalableHigh: |
| 132 case VideoStreamType::VideoProfile::kH264StereoHigh: | 126 case VideoStreamType::VideoProfile::kH264StereoHigh: |
| 133 case VideoStreamType::VideoProfile::kH264MultiviewHigh: | 127 case VideoStreamType::VideoProfile::kH264MultiviewHigh: |
| 134 default: | 128 default: |
| 135 return FF_PROFILE_UNKNOWN; | 129 return FF_PROFILE_UNKNOWN; |
| 136 } | 130 } |
| 137 } | 131 } |
| 138 | 132 |
| 139 // Converts an AVPixelFormat to a PixelFormat. | 133 // Converts an AVPixelFormat to a PixelFormat. |
| 140 VideoStreamType::PixelFormat PixelFormatFromAVPixelFormat( | 134 VideoStreamType::PixelFormat PixelFormatFromAVPixelFormat( |
| 141 AVPixelFormat av_pixel_format) { | 135 AVPixelFormat av_pixel_format) { |
| 142 // TODO(dalesat): Blindly copied from Chromium. | 136 // TODO(dalesat): Blindly copied from Chromium. |
| 143 switch (av_pixel_format) { | 137 switch (av_pixel_format) { |
| 144 case AV_PIX_FMT_YUV422P: | 138 case AV_PIX_FMT_YUV422P: |
| 145 case AV_PIX_FMT_YUVJ422P: | 139 case AV_PIX_FMT_YUVJ422P: |
| 146 return VideoStreamType::PixelFormat::kYv16; | 140 return VideoStreamType::PixelFormat::kYv16; |
| 147 case AV_PIX_FMT_YUV444P: | 141 case AV_PIX_FMT_YUV444P: |
| 148 case AV_PIX_FMT_YUVJ444P: | 142 case AV_PIX_FMT_YUVJ444P: |
| 149 return VideoStreamType::PixelFormat::kYv24; | 143 return VideoStreamType::PixelFormat::kYv24; |
| 150 case AV_PIX_FMT_YUV420P: | 144 case AV_PIX_FMT_YUV420P: |
| 151 case AV_PIX_FMT_YUVJ420P: | 145 case AV_PIX_FMT_YUVJ420P: |
| 152 return VideoStreamType::PixelFormat::kYv12; | 146 return VideoStreamType::PixelFormat::kYv12; |
| 153 case AV_PIX_FMT_YUVA420P: | 147 case AV_PIX_FMT_YUVA420P: |
| 154 return VideoStreamType::PixelFormat::kYv12A; | 148 return VideoStreamType::PixelFormat::kYv12A; |
| 155 default: | 149 default: |
| 156 return VideoStreamType::PixelFormat::kUnknown; | 150 return VideoStreamType::PixelFormat::kUnknown; |
| 157 } | 151 } |
| 158 } | 152 } |
| 159 | 153 |
| 160 // Converts a PixelFormat to an AVPixelFormat. | 154 // Converts a PixelFormat to an AVPixelFormat. |
| 161 AVPixelFormat AVPixelFormatFromPixelFormat( | 155 AVPixelFormat AVPixelFormatFromPixelFormat( |
| 162 VideoStreamType::PixelFormat pixel_format) { | 156 VideoStreamType::PixelFormat pixel_format) { |
| 163 // TODO(dalesat): Blindly copied from Chromium. | 157 // TODO(dalesat): Blindly copied from Chromium. |
| 164 switch (pixel_format) { | 158 switch (pixel_format) { |
| 165 case VideoStreamType::PixelFormat::kYv12: | 159 case VideoStreamType::PixelFormat::kYv12: |
| 166 return AV_PIX_FMT_YUV420P; | 160 return AV_PIX_FMT_YUV420P; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 185 default: | 179 default: |
| 186 return AV_PIX_FMT_NONE; | 180 return AV_PIX_FMT_NONE; |
| 187 } | 181 } |
| 188 } | 182 } |
| 189 | 183 |
| 190 // Creates a StreamType from an AVCodecContext describing a video type. | 184 // Creates a StreamType from an AVCodecContext describing a video type. |
| 191 std::unique_ptr<StreamType> StreamTypeFromVideoCodecContext( | 185 std::unique_ptr<StreamType> StreamTypeFromVideoCodecContext( |
| 192 const AVCodecContext& from) { | 186 const AVCodecContext& from) { |
| 193 VideoStreamType::VideoEncoding encoding; | 187 VideoStreamType::VideoEncoding encoding; |
| 194 switch (from.codec_id) { | 188 switch (from.codec_id) { |
| 195 case AV_CODEC_ID_THEORA : | 189 case AV_CODEC_ID_THEORA: |
| 196 encoding = VideoStreamType::VideoEncoding::kTheora; | 190 encoding = VideoStreamType::VideoEncoding::kTheora; |
| 197 break; | 191 break; |
| 198 case CODEC_ID_VP8: | 192 case CODEC_ID_VP8: |
| 199 encoding = VideoStreamType::VideoEncoding::kVp8; | 193 encoding = VideoStreamType::VideoEncoding::kVp8; |
| 200 break; | 194 break; |
| 201 default: | 195 default: |
| 202 encoding = VideoStreamType::VideoEncoding::kUnknown; | 196 encoding = VideoStreamType::VideoEncoding::kUnknown; |
| 203 break; | 197 break; |
| 204 } | 198 } |
| 205 | 199 |
| 206 return VideoStreamType::Create( | 200 return VideoStreamType::Create( |
| 207 encoding, | 201 encoding, VideoStreamType::VideoProfile::kNotApplicable, |
| 208 VideoStreamType::VideoProfile::kNotApplicable, | |
| 209 PixelFormatFromAVPixelFormat(from.pix_fmt), | 202 PixelFormatFromAVPixelFormat(from.pix_fmt), |
| 210 ColorSpaceFromAVColorSpaceAndRange(from.colorspace, from.color_range), | 203 ColorSpaceFromAVColorSpaceAndRange(from.colorspace, from.color_range), |
| 211 from.width, | 204 from.width, from.height, from.coded_width, from.coded_height, |
| 212 from.height, | 205 from.extradata_size == 0 ? nullptr : Bytes::Create(from.extradata, |
| 213 from.coded_width, | 206 from.extradata_size)); |
| 214 from.coded_height, | |
| 215 from.extradata_size == 0 ? | |
| 216 nullptr : | |
| 217 Bytes::Create(from.extradata, from.extradata_size)); | |
| 218 } | 207 } |
| 219 | 208 |
| 220 // Creates a StreamType from an AVCodecContext describing a data type. | 209 // Creates a StreamType from an AVCodecContext describing a data type. |
| 221 std::unique_ptr<StreamType> StreamTypeFromDataCodecContext( | 210 std::unique_ptr<StreamType> StreamTypeFromDataCodecContext( |
| 222 const AVCodecContext& from) { | 211 const AVCodecContext& from) { |
| 223 // TODO(dalesat): Implement. | 212 // TODO(dalesat): Implement. |
| 224 return StreamType::Create(StreamType::Scheme::kUnknown); | 213 return StreamType::Create(StreamType::Scheme::kUnknown); |
| 225 } | 214 } |
| 226 | 215 |
| 227 // Creates a StreamType from an AVCodecContext describing a subtitle type. | 216 // Creates a StreamType from an AVCodecContext describing a subtitle type. |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 default: | 339 default: |
| 351 if (from.codec == nullptr) { | 340 if (from.codec == nullptr) { |
| 352 return StreamTypeFromCompressedAudioCodecContext(from); | 341 return StreamTypeFromCompressedAudioCodecContext(from); |
| 353 } else { | 342 } else { |
| 354 return StreamTypeFromLpcmCodecContext(from); | 343 return StreamTypeFromLpcmCodecContext(from); |
| 355 } | 344 } |
| 356 } | 345 } |
| 357 case AVMEDIA_TYPE_VIDEO: | 346 case AVMEDIA_TYPE_VIDEO: |
| 358 return StreamTypeFromVideoCodecContext(from); | 347 return StreamTypeFromVideoCodecContext(from); |
| 359 case AVMEDIA_TYPE_UNKNOWN: | 348 case AVMEDIA_TYPE_UNKNOWN: |
| 360 // Treated as AVMEDIA_TYPE_DATA. | 349 // Treated as AVMEDIA_TYPE_DATA. |
| 361 case AVMEDIA_TYPE_DATA: | 350 case AVMEDIA_TYPE_DATA: |
| 362 return StreamTypeFromDataCodecContext(from); | 351 return StreamTypeFromDataCodecContext(from); |
| 363 case AVMEDIA_TYPE_SUBTITLE: | 352 case AVMEDIA_TYPE_SUBTITLE: |
| 364 return StreamTypeFromSubtitleCodecContext(from); | 353 return StreamTypeFromSubtitleCodecContext(from); |
| 365 case AVMEDIA_TYPE_ATTACHMENT: | 354 case AVMEDIA_TYPE_ATTACHMENT: |
| 366 case AVMEDIA_TYPE_NB: | 355 case AVMEDIA_TYPE_NB: |
| 367 default: | 356 default: |
| 368 return StreamType::Create(StreamType::Scheme::kUnknown); | 357 return StreamType::Create(StreamType::Scheme::kUnknown); |
| 369 } | 358 } |
| 370 } | 359 } |
| 371 | 360 |
| 372 // static | 361 // static |
| 373 AvCodecContextPtr AvCodecContext::Create(const StreamType& stream_type) { | 362 AvCodecContextPtr AvCodecContext::Create(const StreamType& stream_type) { |
| 374 InitFfmpeg(); | 363 InitFfmpeg(); |
| 375 | 364 |
| 376 switch (stream_type.scheme()) { | 365 switch (stream_type.scheme()) { |
| 377 case StreamType::Scheme::kLpcm: | 366 case StreamType::Scheme::kLpcm: |
| 378 return CodecContextFromLpcmDetails(*stream_type.lpcm()); | 367 return CodecContextFromLpcmDetails(*stream_type.lpcm()); |
| 379 case StreamType::Scheme::kCompressedAudio: | 368 case StreamType::Scheme::kCompressedAudio: |
| 380 return AVCodecContextFromCompressedAudioStreamType( | 369 return AVCodecContextFromCompressedAudioStreamType( |
| 381 *stream_type.compressed_audio()); | 370 *stream_type.compressed_audio()); |
| 382 case StreamType::Scheme::kVideo: | 371 case StreamType::Scheme::kVideo: |
| 383 return AVCodecContextFromVideoStreamType(*stream_type.video()); | 372 return AVCodecContextFromVideoStreamType(*stream_type.video()); |
| 384 default: | 373 default: |
| 385 return nullptr; | 374 return nullptr; |
| 386 } | 375 } |
| 387 } | 376 } |
| 388 | 377 |
| 389 } // namespace media | 378 } // namespace media |
| 390 } // namespace mojo | 379 } // namespace mojo |
| OLD | NEW |