Home | History | Annotate | Download | only in ffmpeg
      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 #include "media/ffmpeg/ffmpeg_deleters.h"
     19 
     20 // Include FFmpeg header files.
     21 extern "C" {
     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
     29 
     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/avutil.h>
     37 #include <libavutil/imgutils.h>
     38 #include <libavutil/log.h>
     39 #include <libavutil/mathematics.h>
     40 #include <libavutil/opt.h>
     41 MSVC_POP_WARNING();
     42 }  // extern "C"
     43 
     44 namespace media {
     45 
     46 class AudioDecoderConfig;
     47 class VideoDecoderConfig;
     48 
     49 // The following implement the deleters declared in ffmpeg_deleters.h (which
     50 // contains the declarations needed for use with |scoped_ptr| without #include
     51 // "pollution").
     52 
     53 inline void ScopedPtrAVFree::operator()(void* x) const {
     54   av_free(x);
     55 }
     56 
     57 inline void ScopedPtrAVFreePacket::operator()(void* x) const {
     58   AVPacket* packet = static_cast<AVPacket*>(x);
     59   av_free_packet(packet);
     60   delete packet;
     61 }
     62 
     63 inline void ScopedPtrAVFreeContext::operator()(void* x) const {
     64   AVCodecContext* codec_context = static_cast<AVCodecContext*>(x);
     65   avcodec_free_context(&codec_context);
     66 }
     67 
     68 inline void ScopedPtrAVFreeFrame::operator()(void* x) const {
     69   AVFrame* frame = static_cast<AVFrame*>(x);
     70   av_frame_free(&frame);
     71 }
     72 
     73 // Converts an int64 timestamp in |time_base| units to a base::TimeDelta.
     74 // For example if |timestamp| equals 11025 and |time_base| equals {1, 44100}
     75 // then the return value will be a base::TimeDelta for 0.25 seconds since that
     76 // is how much time 11025/44100ths of a second represents.
     77 MEDIA_EXPORT base::TimeDelta ConvertFromTimeBase(const AVRational& time_base,
     78                                                  int64 timestamp);
     79 
     80 // Converts a base::TimeDelta into an int64 timestamp in |time_base| units.
     81 // For example if |timestamp| is 0.5 seconds and |time_base| is {1, 44100}, then
     82 // the return value will be 22050 since that is how many 1/44100ths of a second
     83 // represent 0.5 seconds.
     84 MEDIA_EXPORT int64 ConvertToTimeBase(const AVRational& time_base,
     85                                      const base::TimeDelta& timestamp);
     86 
     87 void AVStreamToAudioDecoderConfig(
     88     const AVStream* stream,
     89     AudioDecoderConfig* config,
     90     bool record_stats);
     91 void AudioDecoderConfigToAVCodecContext(
     92     const AudioDecoderConfig& config,
     93     AVCodecContext* codec_context);
     94 
     95 void AVStreamToVideoDecoderConfig(
     96     const AVStream* stream,
     97     VideoDecoderConfig* config,
     98     bool record_stats);
     99 void VideoDecoderConfigToAVCodecContext(
    100     const VideoDecoderConfig& config,
    101     AVCodecContext* codec_context);
    102 
    103 MEDIA_EXPORT void AVCodecContextToAudioDecoderConfig(
    104     const AVCodecContext* codec_context,
    105     bool is_encrypted,
    106     AudioDecoderConfig* config,
    107     bool record_stats);
    108 
    109 // Converts FFmpeg's channel layout to chrome's ChannelLayout.  |channels| can
    110 // be used when FFmpeg's channel layout is not informative in order to make a
    111 // good guess about the plausible channel layout based on number of channels.
    112 MEDIA_EXPORT ChannelLayout ChannelLayoutToChromeChannelLayout(int64_t layout,
    113                                                               int channels);
    114 
    115 // Converts FFmpeg's audio sample format to Chrome's SampleFormat.
    116 MEDIA_EXPORT SampleFormat
    117     AVSampleFormatToSampleFormat(AVSampleFormat sample_format);
    118 
    119 // Converts FFmpeg's pixel formats to its corresponding supported video format.
    120 MEDIA_EXPORT VideoFrame::Format PixelFormatToVideoFormat(
    121     PixelFormat pixel_format);
    122 
    123 // Converts video formats to its corresponding FFmpeg's pixel formats.
    124 PixelFormat VideoFormatToPixelFormat(VideoFrame::Format video_format);
    125 
    126 // Convert FFmpeg UTC representation (YYYY-MM-DD HH:MM:SS) to base::Time.
    127 // Returns true and sets |*out| if |date_utc| contains a valid
    128 // date string. Otherwise returns fals and timeline_offset is unmodified.
    129 MEDIA_EXPORT bool FFmpegUTCDateToTime(const char* date_utc, base::Time* out);
    130 
    131 }  // namespace media
    132 
    133 #endif  // MEDIA_FFMPEG_FFMPEG_COMMON_H_
    134