- add sources.
[platform/framework/web/crosswalk.git] / src / media / ffmpeg / ffmpeg_common.h
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.
4
5 #ifndef MEDIA_FFMPEG_FFMPEG_COMMON_H_
6 #define MEDIA_FFMPEG_FFMPEG_COMMON_H_
7
8 // Used for FFmpeg error codes.
9 #include <cerrno>
10
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
19 // Include FFmpeg header files.
20 extern "C" {
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 <libavutil/audioconvert.h>
28 #include <libavutil/avutil.h>
29 #include <libavutil/mathematics.h>
30 #include <libavutil/log.h>
31 #include <libavutil/imgutils.h>
32 MSVC_POP_WARNING();
33 }  // extern "C"
34
35 namespace media {
36
37 class AudioDecoderConfig;
38 class VideoDecoderConfig;
39
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 {
43  public:
44   inline void operator()(void* x) const {
45     av_free(x);
46   }
47 };
48
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 {
53  public:
54   inline void operator()(void* x) const {
55     AVPacket* packet = static_cast<AVPacket*>(x);
56     av_free_packet(packet);
57     delete packet;
58   }
59 };
60
61 // Frees an AVCodecContext object in a class that can be passed as a Deleter
62 // argument to scoped_ptr_malloc.
63 class ScopedPtrAVFreeContext {
64  public:
65   inline void operator()(void* x) const {
66     AVCodecContext* codec_context = static_cast<AVCodecContext*>(x);
67     av_free(codec_context->extradata);
68     avcodec_close(codec_context);
69     av_free(codec_context);
70   }
71 };
72
73 // Frees an AVFrame object in a class that can be passed as a Deleter argument
74 // to scoped_ptr_malloc.
75 class ScopedPtrAVFreeFrame {
76  public:
77   inline void operator()(void* x) const {
78     AVFrame* frame = static_cast<AVFrame*>(x);
79     avcodec_free_frame(&frame);
80   }
81 };
82
83 // Converts an int64 timestamp in |time_base| units to a base::TimeDelta.
84 // For example if |timestamp| equals 11025 and |time_base| equals {1, 44100}
85 // then the return value will be a base::TimeDelta for 0.25 seconds since that
86 // is how much time 11025/44100ths of a second represents.
87 MEDIA_EXPORT base::TimeDelta ConvertFromTimeBase(const AVRational& time_base,
88                                                  int64 timestamp);
89
90 // Converts a base::TimeDelta into an int64 timestamp in |time_base| units.
91 // For example if |timestamp| is 0.5 seconds and |time_base| is {1, 44100}, then
92 // the return value will be 22050 since that is how many 1/44100ths of a second
93 // represent 0.5 seconds.
94 MEDIA_EXPORT int64 ConvertToTimeBase(const AVRational& time_base,
95                                      const base::TimeDelta& timestamp);
96
97 void AVStreamToAudioDecoderConfig(
98     const AVStream* stream,
99     AudioDecoderConfig* config,
100     bool record_stats);
101 void AudioDecoderConfigToAVCodecContext(
102     const AudioDecoderConfig& config,
103     AVCodecContext* codec_context);
104
105 void AVStreamToVideoDecoderConfig(
106     const AVStream* stream,
107     VideoDecoderConfig* config,
108     bool record_stats);
109 void VideoDecoderConfigToAVCodecContext(
110     const VideoDecoderConfig& config,
111     AVCodecContext* codec_context);
112
113 // Converts FFmpeg's channel layout to chrome's ChannelLayout.  |channels| can
114 // be used when FFmpeg's channel layout is not informative in order to make a
115 // good guess about the plausible channel layout based on number of channels.
116 ChannelLayout ChannelLayoutToChromeChannelLayout(int64_t layout,
117                                                  int channels);
118
119 // Converts FFmpeg's audio sample format to Chrome's SampleFormat.
120 MEDIA_EXPORT SampleFormat
121     AVSampleFormatToSampleFormat(AVSampleFormat sample_format);
122
123 // Converts FFmpeg's pixel formats to its corresponding supported video format.
124 VideoFrame::Format PixelFormatToVideoFormat(PixelFormat pixel_format);
125
126 // Converts video formats to its corresponding FFmpeg's pixel formats.
127 PixelFormat VideoFormatToPixelFormat(VideoFrame::Format video_format);
128
129 MEDIA_EXPORT AudioCodec CodecIDToAudioCodec(AVCodecID codec_id);
130
131 MEDIA_EXPORT VideoCodec CodecIDToVideoCodec(AVCodecID codec_id);
132
133 }  // namespace media
134
135 #endif  // MEDIA_FFMPEG_FFMPEG_COMMON_H_