| 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/types/audio_stream_type.h" | 6 #include "services/media/framework/types/audio_stream_type.h" |
| 7 #include "services/media/framework/types/subpicture_stream_type.h" | 7 #include "services/media/framework/types/subpicture_stream_type.h" |
| 8 #include "services/media/framework/types/text_stream_type.h" | 8 #include "services/media/framework/types/text_stream_type.h" |
| 9 #include "services/media/framework/types/video_stream_type.h" | 9 #include "services/media/framework/types/video_stream_type.h" |
| 10 #include "services/media/framework_ffmpeg/av_codec_context.h" | 10 #include "services/media/framework_ffmpeg/av_codec_context.h" |
| 11 #include "services/media/framework_ffmpeg/ffmpeg_init.h" | 11 #include "services/media/framework_ffmpeg/ffmpeg_init.h" |
| 12 extern "C" { | 12 extern "C" { |
| 13 #include "third_party/ffmpeg/libavformat/avformat.h" | 13 #include "third_party/ffmpeg/libavformat/avformat.h" |
| 14 } | 14 } |
| 15 | 15 |
| 16 // Ffmeg defines this...undefine. | |
| 17 #undef PixelFormat | |
| 18 | |
| 19 namespace mojo { | 16 namespace mojo { |
| 20 namespace media { | 17 namespace media { |
| 21 | 18 |
| 22 namespace { | 19 namespace { |
| 23 | 20 |
| 24 // Converts an AVSampleFormat into an AudioStreamType::SampleFormat. | 21 // Converts an AVSampleFormat into an AudioStreamType::SampleFormat. |
| 25 AudioStreamType::SampleFormat Convert(AVSampleFormat av_sample_format) { | 22 AudioStreamType::SampleFormat Convert(AVSampleFormat av_sample_format) { |
| 26 switch (av_sample_format) { | 23 switch (av_sample_format) { |
| 27 case AV_SAMPLE_FMT_U8: | 24 case AV_SAMPLE_FMT_U8: |
| 28 case AV_SAMPLE_FMT_U8P: | 25 case AV_SAMPLE_FMT_U8P: |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 case VideoStreamType::VideoProfile::kNotApplicable: | 150 case VideoStreamType::VideoProfile::kNotApplicable: |
| 154 case VideoStreamType::VideoProfile::kH264ScalableBaseline: | 151 case VideoStreamType::VideoProfile::kH264ScalableBaseline: |
| 155 case VideoStreamType::VideoProfile::kH264ScalableHigh: | 152 case VideoStreamType::VideoProfile::kH264ScalableHigh: |
| 156 case VideoStreamType::VideoProfile::kH264StereoHigh: | 153 case VideoStreamType::VideoProfile::kH264StereoHigh: |
| 157 case VideoStreamType::VideoProfile::kH264MultiviewHigh: | 154 case VideoStreamType::VideoProfile::kH264MultiviewHigh: |
| 158 default: | 155 default: |
| 159 return FF_PROFILE_UNKNOWN; | 156 return FF_PROFILE_UNKNOWN; |
| 160 } | 157 } |
| 161 } | 158 } |
| 162 | 159 |
| 163 // Converts an AVPixelFormat to a PixelFormat. | 160 // Creates a StreamType from an AVCodecContext describing a compressed video |
| 164 VideoStreamType::PixelFormat PixelFormatFromAVPixelFormat( | 161 // type. |
| 165 AVPixelFormat av_pixel_format) { | 162 std::unique_ptr<StreamType> StreamTypeFromCompressedVideoCodecContext( |
| 166 // TODO(dalesat): Blindly copied from Chromium. | |
| 167 switch (av_pixel_format) { | |
| 168 case AV_PIX_FMT_YUV422P: | |
| 169 case AV_PIX_FMT_YUVJ422P: | |
| 170 return VideoStreamType::PixelFormat::kYv16; | |
| 171 case AV_PIX_FMT_YUV444P: | |
| 172 case AV_PIX_FMT_YUVJ444P: | |
| 173 return VideoStreamType::PixelFormat::kYv24; | |
| 174 case AV_PIX_FMT_YUV420P: | |
| 175 case AV_PIX_FMT_YUVJ420P: | |
| 176 return VideoStreamType::PixelFormat::kYv12; | |
| 177 case AV_PIX_FMT_YUVA420P: | |
| 178 return VideoStreamType::PixelFormat::kYv12A; | |
| 179 default: | |
| 180 return VideoStreamType::PixelFormat::kUnknown; | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 // Converts a PixelFormat to an AVPixelFormat. | |
| 185 AVPixelFormat AVPixelFormatFromPixelFormat( | |
| 186 VideoStreamType::PixelFormat pixel_format) { | |
| 187 // TODO(dalesat): Blindly copied from Chromium. | |
| 188 switch (pixel_format) { | |
| 189 case VideoStreamType::PixelFormat::kYv12: | |
| 190 return AV_PIX_FMT_YUV420P; | |
| 191 case VideoStreamType::PixelFormat::kYv16: | |
| 192 return AV_PIX_FMT_YUV422P; | |
| 193 case VideoStreamType::PixelFormat::kYv12A: | |
| 194 return AV_PIX_FMT_YUVA420P; | |
| 195 case VideoStreamType::PixelFormat::kYv24: | |
| 196 return AV_PIX_FMT_YUV444P; | |
| 197 case VideoStreamType::PixelFormat::kUnknown: | |
| 198 case VideoStreamType::PixelFormat::kI420: | |
| 199 case VideoStreamType::PixelFormat::kNv12: | |
| 200 case VideoStreamType::PixelFormat::kNv21: | |
| 201 case VideoStreamType::PixelFormat::kUyvy: | |
| 202 case VideoStreamType::PixelFormat::kYuy2: | |
| 203 case VideoStreamType::PixelFormat::kArgb: | |
| 204 case VideoStreamType::PixelFormat::kXrgb: | |
| 205 case VideoStreamType::PixelFormat::kRgb24: | |
| 206 case VideoStreamType::PixelFormat::kRgb32: | |
| 207 case VideoStreamType::PixelFormat::kMjpeg: | |
| 208 case VideoStreamType::PixelFormat::kMt21: | |
| 209 default: | |
| 210 return AV_PIX_FMT_NONE; | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 // Creates a StreamType from an AVCodecContext describing a video type. | |
| 215 std::unique_ptr<StreamType> StreamTypeFromVideoCodecContext( | |
| 216 const AVCodecContext& from) { | 163 const AVCodecContext& from) { |
| 217 const char* encoding; | 164 const char* encoding; |
| 218 switch (from.codec_id) { | 165 switch (from.codec_id) { |
| 219 case CODEC_ID_H263: | 166 case CODEC_ID_H263: |
| 220 encoding = StreamType::kVideoEncodingH263; | 167 encoding = StreamType::kVideoEncodingH263; |
| 221 break; | 168 break; |
| 222 case CODEC_ID_H264: | 169 case CODEC_ID_H264: |
| 223 encoding = StreamType::kVideoEncodingH264; | 170 encoding = StreamType::kVideoEncodingH264; |
| 224 break; | 171 break; |
| 225 case CODEC_ID_MPEG4: | 172 case CODEC_ID_MPEG4: |
| (...skipping 16 matching lines...) Expand all Loading... |
| 242 return VideoStreamType::Create( | 189 return VideoStreamType::Create( |
| 243 encoding, from.extradata_size == 0 | 190 encoding, from.extradata_size == 0 |
| 244 ? nullptr | 191 ? nullptr |
| 245 : Bytes::Create(from.extradata, from.extradata_size), | 192 : Bytes::Create(from.extradata, from.extradata_size), |
| 246 VideoStreamType::VideoProfile::kNotApplicable, | 193 VideoStreamType::VideoProfile::kNotApplicable, |
| 247 PixelFormatFromAVPixelFormat(from.pix_fmt), | 194 PixelFormatFromAVPixelFormat(from.pix_fmt), |
| 248 ColorSpaceFromAVColorSpaceAndRange(from.colorspace, from.color_range), | 195 ColorSpaceFromAVColorSpaceAndRange(from.colorspace, from.color_range), |
| 249 from.width, from.height, from.coded_width, from.coded_height); | 196 from.width, from.height, from.coded_width, from.coded_height); |
| 250 } | 197 } |
| 251 | 198 |
| 199 // Creates a StreamType from an AVCodecContext describing an uncompressed video |
| 200 // type. |
| 201 std::unique_ptr<StreamType> StreamTypeFromUncompressedVideoCodecContext( |
| 202 const AVCodecContext& from) { |
| 203 return VideoStreamType::Create( |
| 204 StreamType::kVideoEncodingUncompressed, nullptr, |
| 205 VideoStreamType::VideoProfile::kNotApplicable, |
| 206 PixelFormatFromAVPixelFormat(from.pix_fmt), |
| 207 ColorSpaceFromAVColorSpaceAndRange(from.colorspace, from.color_range), |
| 208 from.width, from.height, from.coded_width, from.coded_height); |
| 209 } |
| 210 |
| 252 // Creates a StreamType from an AVCodecContext describing a data type. | 211 // Creates a StreamType from an AVCodecContext describing a data type. |
| 253 std::unique_ptr<StreamType> StreamTypeFromDataCodecContext( | 212 std::unique_ptr<StreamType> StreamTypeFromDataCodecContext( |
| 254 const AVCodecContext& from) { | 213 const AVCodecContext& from) { |
| 255 // TODO(dalesat): Implement. | 214 // TODO(dalesat): Implement. |
| 256 LOG(ERROR) << "StreamTypeFromDataCodecContext not implemented"; | 215 return TextStreamType::Create("UNSUPPORTED TYPE (FFMPEG DATA)", nullptr); |
| 257 abort(); | |
| 258 } | 216 } |
| 259 | 217 |
| 260 // Creates a StreamType from an AVCodecContext describing a subtitle type. | 218 // Creates a StreamType from an AVCodecContext describing a subtitle type. |
| 261 std::unique_ptr<StreamType> StreamTypeFromSubtitleCodecContext( | 219 std::unique_ptr<StreamType> StreamTypeFromSubtitleCodecContext( |
| 262 const AVCodecContext& from) { | 220 const AVCodecContext& from) { |
| 263 // TODO(dalesat): Implement. | 221 // TODO(dalesat): Implement. |
| 264 LOG(ERROR) << "StreamTypeFromSubtitleCodecContext not implemented"; | 222 return SubpictureStreamType::Create("UNSUPPORTED TYPE (FFMPEG SUBTITLE)", |
| 265 abort(); | 223 nullptr); |
| 266 } | 224 } |
| 267 | 225 |
| 268 // Creates an AVCodecContext from an AudioStreamType. | 226 // Creates an AVCodecContext from an AudioStreamType. |
| 269 AvCodecContextPtr AVCodecContextFromAudioStreamType( | 227 AvCodecContextPtr AVCodecContextFromAudioStreamType( |
| 270 const AudioStreamType& stream_type) { | 228 const AudioStreamType& stream_type) { |
| 271 DCHECK(stream_type.medium() == StreamType::Medium::kAudio); | 229 DCHECK(stream_type.medium() == StreamType::Medium::kAudio); |
| 272 | 230 |
| 273 AVCodecID codec_id; | 231 AVCodecID codec_id; |
| 274 AVSampleFormat sample_format = AV_SAMPLE_FMT_NONE; | 232 AVSampleFormat sample_format = AV_SAMPLE_FMT_NONE; |
| 275 | 233 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 } | 289 } |
| 332 | 290 |
| 333 return context; | 291 return context; |
| 334 } | 292 } |
| 335 | 293 |
| 336 // Creats an AVCodecContext from a VideoStreamType. | 294 // Creats an AVCodecContext from a VideoStreamType. |
| 337 AvCodecContextPtr AVCodecContextFromVideoStreamType( | 295 AvCodecContextPtr AVCodecContextFromVideoStreamType( |
| 338 const VideoStreamType& stream_type) { | 296 const VideoStreamType& stream_type) { |
| 339 AVCodecID codec_id = AV_CODEC_ID_NONE; | 297 AVCodecID codec_id = AV_CODEC_ID_NONE; |
| 340 | 298 |
| 341 // TODO(dalesat): codec_id | 299 if (stream_type.encoding() == StreamType::kVideoEncodingH263) { |
| 300 codec_id = AV_CODEC_ID_H263; |
| 301 } else if (stream_type.encoding() == StreamType::kVideoEncodingH264) { |
| 302 codec_id = AV_CODEC_ID_H264; |
| 303 } else if (stream_type.encoding() == StreamType::kVideoEncodingMpeg4) { |
| 304 codec_id = AV_CODEC_ID_MPEG4; |
| 305 } else if (stream_type.encoding() == StreamType::kVideoEncodingTheora) { |
| 306 codec_id = AV_CODEC_ID_THEORA; |
| 307 } else if (stream_type.encoding() == StreamType::kVideoEncodingVp3) { |
| 308 codec_id = AV_CODEC_ID_VP3; |
| 309 } else if (stream_type.encoding() == StreamType::kVideoEncodingVp8) { |
| 310 codec_id = AV_CODEC_ID_VP8; |
| 311 } else { |
| 312 LOG(ERROR) << "unsupported encoding " << stream_type.encoding(); |
| 313 abort(); |
| 314 } |
| 342 | 315 |
| 343 if (codec_id == AV_CODEC_ID_NONE) { | 316 if (codec_id == AV_CODEC_ID_NONE) { |
| 344 return nullptr; | 317 return nullptr; |
| 345 } | 318 } |
| 346 | 319 |
| 347 AvCodecContextPtr context(avcodec_alloc_context3(nullptr)); | 320 AvCodecContextPtr context(avcodec_alloc_context3(nullptr)); |
| 348 | 321 |
| 349 context->codec_type = AVMEDIA_TYPE_VIDEO; | 322 context->codec_type = AVMEDIA_TYPE_VIDEO; |
| 350 context->codec_id = codec_id; | 323 context->codec_id = codec_id; |
| 351 context->profile = FfmpegProfileFromVideoProfile(stream_type.profile()); | 324 context->profile = FfmpegProfileFromVideoProfile(stream_type.profile()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 374 // Creats an AVCodecContext from a SubpictureStreamType. | 347 // Creats an AVCodecContext from a SubpictureStreamType. |
| 375 AvCodecContextPtr AVCodecContextFromSubpictureStreamType( | 348 AvCodecContextPtr AVCodecContextFromSubpictureStreamType( |
| 376 const SubpictureStreamType& stream_type) { | 349 const SubpictureStreamType& stream_type) { |
| 377 // TODO(dalesat): Implement. | 350 // TODO(dalesat): Implement. |
| 378 LOG(ERROR) << "AVCodecContextFromSupictureStreamType not implemented"; | 351 LOG(ERROR) << "AVCodecContextFromSupictureStreamType not implemented"; |
| 379 abort(); | 352 abort(); |
| 380 } | 353 } |
| 381 | 354 |
| 382 } // namespace | 355 } // namespace |
| 383 | 356 |
| 357 VideoStreamType::PixelFormat PixelFormatFromAVPixelFormat( |
| 358 AVPixelFormat av_pixel_format) { |
| 359 // TODO(dalesat): Blindly copied from Chromium. |
| 360 switch (av_pixel_format) { |
| 361 case AV_PIX_FMT_YUV422P: |
| 362 case AV_PIX_FMT_YUVJ422P: |
| 363 return VideoStreamType::PixelFormat::kYv16; |
| 364 case AV_PIX_FMT_YUV444P: |
| 365 case AV_PIX_FMT_YUVJ444P: |
| 366 return VideoStreamType::PixelFormat::kYv24; |
| 367 case AV_PIX_FMT_YUV420P: |
| 368 case AV_PIX_FMT_YUVJ420P: |
| 369 return VideoStreamType::PixelFormat::kYv12; |
| 370 case AV_PIX_FMT_YUVA420P: |
| 371 return VideoStreamType::PixelFormat::kYv12A; |
| 372 default: |
| 373 return VideoStreamType::PixelFormat::kUnknown; |
| 374 } |
| 375 } |
| 376 |
| 377 AVPixelFormat AVPixelFormatFromPixelFormat( |
| 378 VideoStreamType::PixelFormat pixel_format) { |
| 379 // TODO(dalesat): Blindly copied from Chromium. |
| 380 switch (pixel_format) { |
| 381 case VideoStreamType::PixelFormat::kYv12: |
| 382 return AV_PIX_FMT_YUV420P; |
| 383 case VideoStreamType::PixelFormat::kYv16: |
| 384 return AV_PIX_FMT_YUV422P; |
| 385 case VideoStreamType::PixelFormat::kYv12A: |
| 386 return AV_PIX_FMT_YUVA420P; |
| 387 case VideoStreamType::PixelFormat::kYv24: |
| 388 return AV_PIX_FMT_YUV444P; |
| 389 case VideoStreamType::PixelFormat::kUnknown: |
| 390 case VideoStreamType::PixelFormat::kI420: |
| 391 case VideoStreamType::PixelFormat::kNv12: |
| 392 case VideoStreamType::PixelFormat::kNv21: |
| 393 case VideoStreamType::PixelFormat::kUyvy: |
| 394 case VideoStreamType::PixelFormat::kYuy2: |
| 395 case VideoStreamType::PixelFormat::kArgb: |
| 396 case VideoStreamType::PixelFormat::kXrgb: |
| 397 case VideoStreamType::PixelFormat::kRgb24: |
| 398 case VideoStreamType::PixelFormat::kRgb32: |
| 399 case VideoStreamType::PixelFormat::kMjpeg: |
| 400 case VideoStreamType::PixelFormat::kMt21: |
| 401 default: |
| 402 return AV_PIX_FMT_NONE; |
| 403 } |
| 404 } |
| 405 |
| 384 // static | 406 // static |
| 385 std::unique_ptr<StreamType> AvCodecContext::GetStreamType( | 407 std::unique_ptr<StreamType> AvCodecContext::GetStreamType( |
| 386 const AVCodecContext& from) { | 408 const AVCodecContext& from) { |
| 387 switch (from.codec_type) { | 409 switch (from.codec_type) { |
| 388 case AVMEDIA_TYPE_AUDIO: | 410 case AVMEDIA_TYPE_AUDIO: |
| 389 switch (from.codec_id) { | 411 switch (from.codec_id) { |
| 390 case CODEC_ID_PCM_S16BE: | 412 case CODEC_ID_PCM_S16BE: |
| 391 case CODEC_ID_PCM_S16LE: | 413 case CODEC_ID_PCM_S16LE: |
| 392 case CODEC_ID_PCM_S24BE: | 414 case CODEC_ID_PCM_S24BE: |
| 393 case CODEC_ID_PCM_S24LE: | 415 case CODEC_ID_PCM_S24LE: |
| 394 case CODEC_ID_PCM_U8: | 416 case CODEC_ID_PCM_U8: |
| 395 return StreamTypeFromLpcmCodecContext(from); | 417 return StreamTypeFromLpcmCodecContext(from); |
| 396 default: | 418 default: |
| 397 if (from.codec == nullptr) { | 419 if (from.codec == nullptr) { |
| 398 return StreamTypeFromCompressedAudioCodecContext(from); | 420 return StreamTypeFromCompressedAudioCodecContext(from); |
| 399 } else { | 421 } else { |
| 400 return StreamTypeFromLpcmCodecContext(from); | 422 return StreamTypeFromLpcmCodecContext(from); |
| 401 } | 423 } |
| 402 } | 424 } |
| 403 case AVMEDIA_TYPE_VIDEO: | 425 case AVMEDIA_TYPE_VIDEO: |
| 404 return StreamTypeFromVideoCodecContext(from); | 426 if (from.codec == nullptr) { |
| 427 return StreamTypeFromCompressedVideoCodecContext(from); |
| 428 } else { |
| 429 return StreamTypeFromUncompressedVideoCodecContext(from); |
| 430 } |
| 405 case AVMEDIA_TYPE_UNKNOWN: | 431 case AVMEDIA_TYPE_UNKNOWN: |
| 406 // Treated as AVMEDIA_TYPE_DATA. | 432 // Treated as AVMEDIA_TYPE_DATA. |
| 407 case AVMEDIA_TYPE_DATA: | 433 case AVMEDIA_TYPE_DATA: |
| 408 return StreamTypeFromDataCodecContext(from); | 434 return StreamTypeFromDataCodecContext(from); |
| 409 case AVMEDIA_TYPE_SUBTITLE: | 435 case AVMEDIA_TYPE_SUBTITLE: |
| 410 return StreamTypeFromSubtitleCodecContext(from); | 436 return StreamTypeFromSubtitleCodecContext(from); |
| 411 case AVMEDIA_TYPE_ATTACHMENT: | 437 case AVMEDIA_TYPE_ATTACHMENT: |
| 412 case AVMEDIA_TYPE_NB: | 438 case AVMEDIA_TYPE_NB: |
| 413 default: | 439 default: |
| 414 LOG(ERROR) << "unsupported code type " << from.codec_type; | 440 LOG(ERROR) << "unsupported code type " << from.codec_type; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 429 return AVCodecContextFromTextStreamType(*stream_type.text()); | 455 return AVCodecContextFromTextStreamType(*stream_type.text()); |
| 430 case StreamType::Medium::kSubpicture: | 456 case StreamType::Medium::kSubpicture: |
| 431 return AVCodecContextFromSubpictureStreamType(*stream_type.subpicture()); | 457 return AVCodecContextFromSubpictureStreamType(*stream_type.subpicture()); |
| 432 default: | 458 default: |
| 433 return nullptr; | 459 return nullptr; |
| 434 } | 460 } |
| 435 } | 461 } |
| 436 | 462 |
| 437 } // namespace media | 463 } // namespace media |
| 438 } // namespace mojo | 464 } // namespace mojo |
| OLD | NEW |