OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_FFMPEG_FFMPEG_COMMON_H_ | 5 #ifndef MEDIA_FFMPEG_FFMPEG_COMMON_H_ |
6 #define MEDIA_FFMPEG_FFMPEG_COMMON_H_ | 6 #define MEDIA_FFMPEG_FFMPEG_COMMON_H_ |
7 | 7 |
8 // Used for FFmpeg error codes. | 8 // Used for FFmpeg error codes. |
9 #include <cerrno> | 9 #include <cerrno> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "media/base/audio_decoder_config.h" | 13 #include "media/base/audio_decoder_config.h" |
14 #include "media/base/channel_layout.h" | 14 #include "media/base/channel_layout.h" |
15 #include "media/base/media_export.h" | 15 #include "media/base/media_export.h" |
16 #include "media/base/video_decoder_config.h" | 16 #include "media/base/video_decoder_config.h" |
17 #include "media/base/video_frame.h" | 17 #include "media/base/video_frame.h" |
| 18 #include "media/ffmpeg/ffmpeg_deleters.h" |
18 | 19 |
19 // Include FFmpeg header files. | 20 // Include FFmpeg header files. |
20 extern "C" { | 21 extern "C" { |
21 // Temporarily disable possible loss of data warning. | 22 // Temporarily disable possible loss of data warning. |
22 // TODO(scherkus): fix and upstream the compiler warnings. | 23 // TODO(scherkus): fix and upstream the compiler warnings. |
23 MSVC_PUSH_DISABLE_WARNING(4244); | 24 MSVC_PUSH_DISABLE_WARNING(4244); |
24 #include <libavcodec/avcodec.h> | 25 #include <libavcodec/avcodec.h> |
25 #include <libavformat/avformat.h> | 26 #include <libavformat/avformat.h> |
26 #include <libavformat/avio.h> | 27 #include <libavformat/avio.h> |
27 #include <libavutil/audioconvert.h> | 28 #include <libavutil/audioconvert.h> |
28 #include <libavutil/avutil.h> | 29 #include <libavutil/avutil.h> |
29 #include <libavutil/mathematics.h> | 30 #include <libavutil/mathematics.h> |
30 #include <libavutil/log.h> | 31 #include <libavutil/log.h> |
31 #include <libavutil/imgutils.h> | 32 #include <libavutil/imgutils.h> |
32 MSVC_POP_WARNING(); | 33 MSVC_POP_WARNING(); |
33 } // extern "C" | 34 } // extern "C" |
34 | 35 |
35 namespace media { | 36 namespace media { |
36 | 37 |
37 class AudioDecoderConfig; | 38 class AudioDecoderConfig; |
38 class VideoDecoderConfig; | 39 class VideoDecoderConfig; |
39 | 40 |
40 // Wraps FFmpeg's av_free() in a class that can be passed as a template argument | 41 // The following implement the deleters declared in ffmpeg_deleters.h (which |
41 // to scoped_ptr_malloc. | 42 // contains the declarations needed for use with |scoped_ptr| without #include |
42 class ScopedPtrAVFree { | 43 // "pollution"). |
43 public: | |
44 inline void operator()(void* x) const { | |
45 av_free(x); | |
46 } | |
47 }; | |
48 | 44 |
49 // This assumes that the AVPacket being captured was allocated outside of | 45 inline void ScopedPtrAVFree::operator()(void* x) const { |
50 // FFmpeg via the new operator. Do not use this with AVPacket instances that | 46 av_free(x); |
51 // are allocated via malloc() or av_malloc(). | 47 } |
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 | 48 |
61 // Frees an AVCodecContext object in a class that can be passed as a Deleter | 49 inline void ScopedPtrAVFreePacket::operator()(void* x) const { |
62 // argument to scoped_ptr_malloc. | 50 AVPacket* packet = static_cast<AVPacket*>(x); |
63 class ScopedPtrAVFreeContext { | 51 av_free_packet(packet); |
64 public: | 52 delete packet; |
65 inline void operator()(void* x) const { | 53 } |
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 | 54 |
73 // Frees an AVFrame object in a class that can be passed as a Deleter argument | 55 inline void ScopedPtrAVFreeContext::operator()(void* x) const { |
74 // to scoped_ptr_malloc. | 56 AVCodecContext* codec_context = static_cast<AVCodecContext*>(x); |
75 class ScopedPtrAVFreeFrame { | 57 av_free(codec_context->extradata); |
76 public: | 58 avcodec_close(codec_context); |
77 inline void operator()(void* x) const { | 59 av_free(codec_context); |
78 AVFrame* frame = static_cast<AVFrame*>(x); | 60 } |
79 avcodec_free_frame(&frame); | 61 |
80 } | 62 inline void ScopedPtrAVFreeFrame::operator()(void* x) const { |
81 }; | 63 AVFrame* frame = static_cast<AVFrame*>(x); |
| 64 avcodec_free_frame(&frame); |
| 65 } |
82 | 66 |
83 // Converts an int64 timestamp in |time_base| units to a base::TimeDelta. | 67 // 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} | 68 // 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 | 69 // 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. | 70 // is how much time 11025/44100ths of a second represents. |
87 MEDIA_EXPORT base::TimeDelta ConvertFromTimeBase(const AVRational& time_base, | 71 MEDIA_EXPORT base::TimeDelta ConvertFromTimeBase(const AVRational& time_base, |
88 int64 timestamp); | 72 int64 timestamp); |
89 | 73 |
90 // Converts a base::TimeDelta into an int64 timestamp in |time_base| units. | 74 // 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 | 75 // For example if |timestamp| is 0.5 seconds and |time_base| is {1, 44100}, then |
(...skipping 30 matching lines...) Expand all Loading... |
122 | 106 |
123 // Converts FFmpeg's pixel formats to its corresponding supported video format. | 107 // Converts FFmpeg's pixel formats to its corresponding supported video format. |
124 VideoFrame::Format PixelFormatToVideoFormat(PixelFormat pixel_format); | 108 VideoFrame::Format PixelFormatToVideoFormat(PixelFormat pixel_format); |
125 | 109 |
126 // Converts video formats to its corresponding FFmpeg's pixel formats. | 110 // Converts video formats to its corresponding FFmpeg's pixel formats. |
127 PixelFormat VideoFormatToPixelFormat(VideoFrame::Format video_format); | 111 PixelFormat VideoFormatToPixelFormat(VideoFrame::Format video_format); |
128 | 112 |
129 } // namespace media | 113 } // namespace media |
130 | 114 |
131 #endif // MEDIA_FFMPEG_FFMPEG_COMMON_H_ | 115 #endif // MEDIA_FFMPEG_FFMPEG_COMMON_H_ |
OLD | NEW |