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_decoder_config.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(
89 const AVStream
* stream
,
90 AudioDecoderConfig
* config
,
92 void AudioDecoderConfigToAVCodecContext(
93 const AudioDecoderConfig
& config
,
94 AVCodecContext
* codec_context
);
96 void AVStreamToVideoDecoderConfig(
97 const AVStream
* stream
,
98 VideoDecoderConfig
* config
,
100 void VideoDecoderConfigToAVCodecContext(
101 const VideoDecoderConfig
& config
,
102 AVCodecContext
* codec_context
);
104 MEDIA_EXPORT
void AVCodecContextToAudioDecoderConfig(
105 const AVCodecContext
* codec_context
,
107 AudioDecoderConfig
* config
,
110 // Converts FFmpeg's channel layout to chrome's ChannelLayout. |channels| can
111 // be used when FFmpeg's channel layout is not informative in order to make a
112 // good guess about the plausible channel layout based on number of channels.
113 MEDIA_EXPORT ChannelLayout
ChannelLayoutToChromeChannelLayout(int64_t layout
,
116 // Converts FFmpeg's audio sample format to Chrome's SampleFormat.
117 MEDIA_EXPORT SampleFormat
118 AVSampleFormatToSampleFormat(AVSampleFormat sample_format
);
120 // Converts FFmpeg's pixel formats to its corresponding supported video format.
121 MEDIA_EXPORT VideoPixelFormat
122 PixelFormatToVideoPixelFormat(PixelFormat pixel_format
);
124 // Converts video formats to its corresponding FFmpeg's pixel formats.
125 PixelFormat
VideoPixelFormatToPixelFormat(VideoPixelFormat video_format
);
127 ColorSpace
AVColorSpaceToColorSpace(AVColorSpace color_space
,
128 AVColorRange color_range
);
130 // Convert FFmpeg UTC representation (YYYY-MM-DD HH:MM:SS) to base::Time.
131 // Returns true and sets |*out| if |date_utc| contains a valid
132 // date string. Otherwise returns fals and timeline_offset is unmodified.
133 MEDIA_EXPORT
bool FFmpegUTCDateToTime(const char* date_utc
, base::Time
* out
);
137 #endif // MEDIA_FFMPEG_FFMPEG_COMMON_H_