1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef MEDIA_FFMPEG_FFMPEG_COMMON_H_ 6 #define MEDIA_FFMPEG_FFMPEG_COMMON_H_ 7 8 // Used for FFmpeg error codes. 9 #include <cerrno> 10 11 #include "base/compiler_specific.h" 12 #include "base/time/time.h" 13 #include "media/base/audio_decoder_config.h" 14 #include "media/base/channel_layout.h" 15 #include "media/base/media_export.h" 16 #include "media/base/video_decoder_config.h" 17 #include "media/base/video_frame.h" 18 19 // Include FFmpeg header files. 20 extern "C" { 21 // Temporarily disable possible loss of data warning. 22 // TODO(scherkus): fix and upstream the compiler warnings. 23 MSVC_PUSH_DISABLE_WARNING(4244); 24 #include <libavcodec/avcodec.h> 25 #include <libavformat/avformat.h> 26 #include <libavformat/avio.h> 27 #include <libavutil/audioconvert.h> 28 #include <libavutil/avutil.h> 29 #include <libavutil/mathematics.h> 30 #include <libavutil/log.h> 31 #include <libavutil/imgutils.h> 32 MSVC_POP_WARNING(); 33 } // extern "C" 34 35 namespace media { 36 37 class AudioDecoderConfig; 38 class VideoDecoderConfig; 39 40 // Wraps FFmpeg's av_free() in a class that can be passed as a template argument 41 // to scoped_ptr_malloc. 42 class ScopedPtrAVFree { 43 public: 44 inline void operator()(void* x) const { 45 av_free(x); 46 } 47 }; 48 49 // This assumes that the AVPacket being captured was allocated outside of 50 // FFmpeg via the new operator. Do not use this with AVPacket instances that 51 // are allocated via malloc() or av_malloc(). 52 class ScopedPtrAVFreePacket { 53 public: 54 inline void operator()(void* x) const { 55 AVPacket* packet = static_cast<AVPacket*>(x); 56 av_free_packet(packet); 57 delete packet; 58 } 59 }; 60 61 // Converts an int64 timestamp in |time_base| units to a base::TimeDelta. 62 // For example if |timestamp| equals 11025 and |time_base| equals {1, 44100} 63 // then the return value will be a base::TimeDelta for 0.25 seconds since that 64 // is how much time 11025/44100ths of a second represents. 65 MEDIA_EXPORT base::TimeDelta ConvertFromTimeBase(const AVRational& time_base, 66 int64 timestamp); 67 68 // Converts a base::TimeDelta into an int64 timestamp in |time_base| units. 69 // For example if |timestamp| is 0.5 seconds and |time_base| is {1, 44100}, then 70 // the return value will be 22050 since that is how many 1/44100ths of a second 71 // represent 0.5 seconds. 72 MEDIA_EXPORT int64 ConvertToTimeBase(const AVRational& time_base, 73 const base::TimeDelta& timestamp); 74 75 void AVStreamToAudioDecoderConfig( 76 const AVStream* stream, 77 AudioDecoderConfig* config, 78 bool record_stats); 79 void AudioDecoderConfigToAVCodecContext( 80 const AudioDecoderConfig& config, 81 AVCodecContext* codec_context); 82 83 void AVStreamToVideoDecoderConfig( 84 const AVStream* stream, 85 VideoDecoderConfig* config, 86 bool record_stats); 87 void VideoDecoderConfigToAVCodecContext( 88 const VideoDecoderConfig& config, 89 AVCodecContext* codec_context); 90 91 // Converts FFmpeg's channel layout to chrome's ChannelLayout. |channels| can 92 // be used when FFmpeg's channel layout is not informative in order to make a 93 // good guess about the plausible channel layout based on number of channels. 94 ChannelLayout ChannelLayoutToChromeChannelLayout(int64_t layout, 95 int channels); 96 97 // Converts FFmpeg's audio sample format to Chrome's SampleFormat. 98 SampleFormat AVSampleFormatToSampleFormat(AVSampleFormat sample_format); 99 100 // Converts FFmpeg's pixel formats to its corresponding supported video format. 101 VideoFrame::Format PixelFormatToVideoFormat(PixelFormat pixel_format); 102 103 // Converts video formats to its corresponding FFmpeg's pixel formats. 104 PixelFormat VideoFormatToPixelFormat(VideoFrame::Format video_format); 105 106 } // namespace media 107 108 #endif // MEDIA_FFMPEG_FFMPEG_COMMON_H_ 109