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.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_frame.h"
17 #include "media/base/video_decoder_config.h"
19 // Include FFmpeg header files.
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 <libavformat/url.h>
28 #include <libavutil/avutil.h>
29 #include <libavutil/mathematics.h>
30 #include <libavutil/log.h>
31 #include <libavutil/imgutils.h>
37 class AudioDecoderConfig
;
38 class VideoDecoderConfig
;
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
{
44 inline void operator()(void* x
) const {
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
{
54 inline void operator()(void* x
) const {
55 AVPacket
* packet
= static_cast<AVPacket
*>(x
);
56 av_free_packet(packet
);
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
,
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
);
75 void AVCodecContextToAudioDecoderConfig(
76 const AVCodecContext
* codec_context
,
77 AudioDecoderConfig
* config
);
78 void AudioDecoderConfigToAVCodecContext(
79 const AudioDecoderConfig
& config
,
80 AVCodecContext
* codec_context
);
82 void AVStreamToVideoDecoderConfig(
83 const AVStream
* stream
,
84 VideoDecoderConfig
* config
);
85 void VideoDecoderConfigToAVCodecContext(
86 const VideoDecoderConfig
& config
,
87 AVCodecContext
* codec_context
);
89 // Converts FFmpeg's channel layout to chrome's ChannelLayout. |channels| can
90 // be used when FFmpeg's channel layout is not informative in order to make a
91 // good guess about the plausible channel layout based on number of channels.
92 ChannelLayout
ChannelLayoutToChromeChannelLayout(int64_t layout
,
95 // Converts FFmpeg's pixel formats to its corresponding supported video format.
96 VideoFrame::Format
PixelFormatToVideoFormat(PixelFormat pixel_format
);
98 // Converts video formats to its corresponding FFmpeg's pixel formats.
99 PixelFormat
VideoFormatToPixelFormat(VideoFrame::Format video_format
);
101 // Converts an FFmpeg video codec ID into its corresponding supported codec id.
102 VideoCodec
CodecIDToVideoCodec(CodecID codec_id
);
104 // Converts an FFmpeg audio codec ID into its corresponding supported codec id.
105 AudioCodec
CodecIDToAudioCodec(CodecID codec_id
);
107 // Closes & destroys all AVStreams in the context and then closes &
108 // destroys the AVFormatContext.
109 void DestroyAVFormatContext(AVFormatContext
* format_context
);
113 #endif // MEDIA_FFMPEG_FFMPEG_COMMON_H_