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/avio.h>
36 #include <libavutil/audioconvert.h>
37 #include <libavutil/avutil.h>
38 #include <libavutil/mathematics.h>
39 #include <libavutil/log.h>
40 #include <libavutil/imgutils.h>
46 class AudioDecoderConfig
;
47 class VideoDecoderConfig
;
49 // The following implement the deleters declared in ffmpeg_deleters.h (which
50 // contains the declarations needed for use with |scoped_ptr| without #include
53 inline void ScopedPtrAVFree::operator()(void* x
) const {
57 inline void ScopedPtrAVFreePacket::operator()(void* x
) const {
58 AVPacket
* packet
= static_cast<AVPacket
*>(x
);
59 av_free_packet(packet
);
63 inline void ScopedPtrAVFreeContext::operator()(void* x
) const {
64 AVCodecContext
* codec_context
= static_cast<AVCodecContext
*>(x
);
65 av_free(codec_context
->extradata
);
66 avcodec_close(codec_context
);
67 av_free(codec_context
);
70 inline void ScopedPtrAVFreeFrame::operator()(void* x
) const {
71 AVFrame
* frame
= static_cast<AVFrame
*>(x
);
72 av_frame_free(&frame
);
75 // Converts an int64 timestamp in |time_base| units to a base::TimeDelta.
76 // For example if |timestamp| equals 11025 and |time_base| equals {1, 44100}
77 // then the return value will be a base::TimeDelta for 0.25 seconds since that
78 // is how much time 11025/44100ths of a second represents.
79 MEDIA_EXPORT
base::TimeDelta
ConvertFromTimeBase(const AVRational
& time_base
,
82 // Converts a base::TimeDelta into an int64 timestamp in |time_base| units.
83 // For example if |timestamp| is 0.5 seconds and |time_base| is {1, 44100}, then
84 // the return value will be 22050 since that is how many 1/44100ths of a second
85 // represent 0.5 seconds.
86 MEDIA_EXPORT int64
ConvertToTimeBase(const AVRational
& time_base
,
87 const base::TimeDelta
& timestamp
);
89 void AVStreamToAudioDecoderConfig(
90 const AVStream
* stream
,
91 AudioDecoderConfig
* config
,
93 void AudioDecoderConfigToAVCodecContext(
94 const AudioDecoderConfig
& config
,
95 AVCodecContext
* codec_context
);
97 void AVStreamToVideoDecoderConfig(
98 const AVStream
* stream
,
99 VideoDecoderConfig
* config
,
101 void VideoDecoderConfigToAVCodecContext(
102 const VideoDecoderConfig
& config
,
103 AVCodecContext
* codec_context
);
105 MEDIA_EXPORT
void AVCodecContextToAudioDecoderConfig(
106 const AVCodecContext
* codec_context
,
108 AudioDecoderConfig
* config
,
111 // Converts FFmpeg's channel layout to chrome's ChannelLayout. |channels| can
112 // be used when FFmpeg's channel layout is not informative in order to make a
113 // good guess about the plausible channel layout based on number of channels.
114 MEDIA_EXPORT ChannelLayout
ChannelLayoutToChromeChannelLayout(int64_t layout
,
117 // Converts FFmpeg's audio sample format to Chrome's SampleFormat.
118 MEDIA_EXPORT SampleFormat
119 AVSampleFormatToSampleFormat(AVSampleFormat sample_format
);
121 // Converts FFmpeg's pixel formats to its corresponding supported video format.
122 MEDIA_EXPORT
VideoFrame::Format
PixelFormatToVideoFormat(
123 PixelFormat pixel_format
);
125 // Converts video formats to its corresponding FFmpeg's pixel formats.
126 PixelFormat
VideoFormatToPixelFormat(VideoFrame::Format video_format
);
128 // Convert FFmpeg UTC representation (YYYY-MM-DD HH:MM:SS) to base::Time.
129 // Returns true and sets |*out| if |date_utc| contains a valid
130 // date string. Otherwise returns fals and timeline_offset is unmodified.
131 MEDIA_EXPORT
bool FFmpegUTCDateToTime(const char* date_utc
, base::Time
* out
);
135 #endif // MEDIA_FFMPEG_FFMPEG_COMMON_H_