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.
5 #ifndef MEDIA_FFMPEG_FFMPEG_COMMON_H_
6 #define MEDIA_FFMPEG_FFMPEG_COMMON_H_
8 // Used for FFmpeg error codes.
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_codecs.h"
17 #include "media/base/video_frame.h"
18 #include "media/ffmpeg/ffmpeg_deleters.h"
20 // Include FFmpeg header files.
22 // Disable deprecated features which result in spammy compile warnings. This
23 // list of defines must mirror those in the 'defines' section of the ffmpeg.gyp
24 // file or the headers below will generate different structures.
25 #define FF_API_PIX_FMT_DESC 0
26 #define FF_API_OLD_DECODE_AUDIO 0
27 #define FF_API_DESTRUCT_PACKET 0
28 #define FF_API_GET_BUFFER 0
30 // Temporarily disable possible loss of data warning.
31 // TODO(scherkus): fix and upstream the compiler warnings.
32 MSVC_PUSH_DISABLE_WARNING(4244);
33 #include <libavcodec/avcodec.h>
34 #include <libavformat/avformat.h>
35 #include <libavformat/internal.h>
36 #include <libavformat/avio.h>
37 #include <libavutil/avutil.h>
38 #include <libavutil/imgutils.h>
39 #include <libavutil/log.h>
40 #include <libavutil/mathematics.h>
41 #include <libavutil/opt.h>
47 class AudioDecoderConfig
;
48 class VideoDecoderConfig
;
50 // The following implement the deleters declared in ffmpeg_deleters.h (which
51 // contains the declarations needed for use with |scoped_ptr| without #include
54 inline void ScopedPtrAVFree::operator()(void* x
) const {
58 inline void ScopedPtrAVFreePacket::operator()(void* x
) const {
59 AVPacket
* packet
= static_cast<AVPacket
*>(x
);
60 av_free_packet(packet
);
64 inline void ScopedPtrAVFreeContext::operator()(void* x
) const {
65 AVCodecContext
* codec_context
= static_cast<AVCodecContext
*>(x
);
66 avcodec_free_context(&codec_context
);
69 inline void ScopedPtrAVFreeFrame::operator()(void* x
) const {
70 AVFrame
* frame
= static_cast<AVFrame
*>(x
);
71 av_frame_free(&frame
);
74 // Converts an int64 timestamp in |time_base| units to a base::TimeDelta.
75 // For example if |timestamp| equals 11025 and |time_base| equals {1, 44100}
76 // then the return value will be a base::TimeDelta for 0.25 seconds since that
77 // is how much time 11025/44100ths of a second represents.
78 MEDIA_EXPORT
base::TimeDelta
ConvertFromTimeBase(const AVRational
& time_base
,
81 // Converts a base::TimeDelta into an int64 timestamp in |time_base| units.
82 // For example if |timestamp| is 0.5 seconds and |time_base| is {1, 44100}, then
83 // the return value will be 22050 since that is how many 1/44100ths of a second
84 // represent 0.5 seconds.
85 MEDIA_EXPORT int64
ConvertToTimeBase(const AVRational
& time_base
,
86 const base::TimeDelta
& timestamp
);
88 void AVStreamToAudioDecoderConfig(const AVStream
* stream
,
89 AudioDecoderConfig
* config
);
90 void AudioDecoderConfigToAVCodecContext(
91 const AudioDecoderConfig
& config
,
92 AVCodecContext
* codec_context
);
94 void AVStreamToVideoDecoderConfig(const AVStream
* stream
,
95 VideoDecoderConfig
* config
);
96 void VideoDecoderConfigToAVCodecContext(
97 const VideoDecoderConfig
& config
,
98 AVCodecContext
* codec_context
);
100 MEDIA_EXPORT
void AVCodecContextToAudioDecoderConfig(
101 const AVCodecContext
* codec_context
,
103 AudioDecoderConfig
* config
);
105 // Converts FFmpeg's channel layout to chrome's ChannelLayout. |channels| can
106 // be used when FFmpeg's channel layout is not informative in order to make a
107 // good guess about the plausible channel layout based on number of channels.
108 MEDIA_EXPORT ChannelLayout
ChannelLayoutToChromeChannelLayout(int64_t layout
,
111 MEDIA_EXPORT AVCodecID
VideoCodecToCodecID(VideoCodec video_codec
);
113 // Converts FFmpeg's audio sample format to Chrome's SampleFormat.
114 MEDIA_EXPORT SampleFormat
115 AVSampleFormatToSampleFormat(AVSampleFormat sample_format
);
117 // Converts FFmpeg's pixel formats to its corresponding supported video format.
118 MEDIA_EXPORT VideoPixelFormat
119 AVPixelFormatToVideoPixelFormat(AVPixelFormat pixel_format
);
121 // Converts video formats to its corresponding FFmpeg's pixel formats.
122 AVPixelFormat
VideoPixelFormatToAVPixelFormat(VideoPixelFormat video_format
);
124 ColorSpace
AVColorSpaceToColorSpace(AVColorSpace color_space
,
125 AVColorRange color_range
);
127 // Convert FFmpeg UTC representation (YYYY-MM-DD HH:MM:SS) to base::Time.
128 // Returns true and sets |*out| if |date_utc| contains a valid
129 // date string. Otherwise returns fals and timeline_offset is unmodified.
130 MEDIA_EXPORT
bool FFmpegUTCDateToTime(const char* date_utc
, base::Time
* out
);
134 #endif // MEDIA_FFMPEG_FFMPEG_COMMON_H_