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 // Temporarily disable possible loss of data warning.
23 // TODO(scherkus): fix and upstream the compiler warnings.
24 MSVC_PUSH_DISABLE_WARNING(4244);
25 #include <libavcodec/avcodec.h>
26 #include <libavformat/avformat.h>
27 #include <libavformat/avio.h>
28 #include <libavutil/audioconvert.h>
29 #include <libavutil/avutil.h>
30 #include <libavutil/mathematics.h>
31 #include <libavutil/log.h>
32 #include <libavutil/imgutils.h>
38 class AudioDecoderConfig
;
39 class VideoDecoderConfig
;
41 // The following implement the deleters declared in ffmpeg_deleters.h (which
42 // contains the declarations needed for use with |scoped_ptr| without #include
45 inline void ScopedPtrAVFree::operator()(void* x
) const {
49 inline void ScopedPtrAVFreePacket::operator()(void* x
) const {
50 AVPacket
* packet
= static_cast<AVPacket
*>(x
);
51 av_free_packet(packet
);
55 inline void ScopedPtrAVFreeContext::operator()(void* x
) const {
56 AVCodecContext
* codec_context
= static_cast<AVCodecContext
*>(x
);
57 av_free(codec_context
->extradata
);
58 avcodec_close(codec_context
);
59 av_free(codec_context
);
62 inline void ScopedPtrAVFreeFrame::operator()(void* x
) const {
63 AVFrame
* frame
= static_cast<AVFrame
*>(x
);
64 avcodec_free_frame(&frame
);
67 // Converts an int64 timestamp in |time_base| units to a base::TimeDelta.
68 // For example if |timestamp| equals 11025 and |time_base| equals {1, 44100}
69 // then the return value will be a base::TimeDelta for 0.25 seconds since that
70 // is how much time 11025/44100ths of a second represents.
71 MEDIA_EXPORT
base::TimeDelta
ConvertFromTimeBase(const AVRational
& time_base
,
74 // Converts a base::TimeDelta into an int64 timestamp in |time_base| units.
75 // For example if |timestamp| is 0.5 seconds and |time_base| is {1, 44100}, then
76 // the return value will be 22050 since that is how many 1/44100ths of a second
77 // represent 0.5 seconds.
78 MEDIA_EXPORT int64
ConvertToTimeBase(const AVRational
& time_base
,
79 const base::TimeDelta
& timestamp
);
81 void AVStreamToAudioDecoderConfig(
82 const AVStream
* stream
,
83 AudioDecoderConfig
* config
,
85 void AudioDecoderConfigToAVCodecContext(
86 const AudioDecoderConfig
& config
,
87 AVCodecContext
* codec_context
);
89 void AVStreamToVideoDecoderConfig(
90 const AVStream
* stream
,
91 VideoDecoderConfig
* config
,
93 void VideoDecoderConfigToAVCodecContext(
94 const VideoDecoderConfig
& config
,
95 AVCodecContext
* codec_context
);
97 MEDIA_EXPORT
void AVCodecContextToAudioDecoderConfig(
98 const AVCodecContext
* codec_context
,
100 AudioDecoderConfig
* config
,
103 // Converts FFmpeg's channel layout to chrome's ChannelLayout. |channels| can
104 // be used when FFmpeg's channel layout is not informative in order to make a
105 // good guess about the plausible channel layout based on number of channels.
106 ChannelLayout
ChannelLayoutToChromeChannelLayout(int64_t layout
,
109 // Converts FFmpeg's audio sample format to Chrome's SampleFormat.
110 MEDIA_EXPORT SampleFormat
111 AVSampleFormatToSampleFormat(AVSampleFormat sample_format
);
113 // Converts FFmpeg's pixel formats to its corresponding supported video format.
114 MEDIA_EXPORT
VideoFrame::Format
PixelFormatToVideoFormat(
115 PixelFormat pixel_format
);
117 // Converts video formats to its corresponding FFmpeg's pixel formats.
118 PixelFormat
VideoFormatToPixelFormat(VideoFrame::Format video_format
);
120 // Convert FFmpeg UTC representation (YYYY-MM-DD HH:MM:SS) to base::Time.
121 // Returns true and sets |*out| if |date_utc| contains a valid
122 // date string. Otherwise returns fals and timeline_offset is unmodified.
123 MEDIA_EXPORT
bool FFmpegUTCDateToTime(const char* date_utc
, base::Time
* out
);
127 #endif // MEDIA_FFMPEG_FFMPEG_COMMON_H_