| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #include "media/base/media.h" | 5 #include "media/base/media.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include <dlfcn.h> | 9 #include <dlfcn.h> |
| 10 | 10 |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "media/filters/ffmpeg_common.h" | 14 #include "media/filters/ffmpeg_common.h" |
| 15 | 15 |
| 16 // We create stub references to dynamically loaded functions in ffmpeg | 16 // We create stub references to dynamically loaded functions in ffmpeg |
| 17 // for ease of linking. | 17 // for ease of linking. |
| 18 // | 18 // |
| 19 // TODO(ajwong): We need to find a more maintainable way to have this work. | 19 // TODO(ajwong): We need to find a more maintainable way to have this work. |
| 20 // Also, this code should really be in the ffmpeg wrapper, and not here | 20 // Also, this code should really be in the ffmpeg wrapper, and not here |
| 21 // in the media level. The concept of "weak symbols" looks like it might | 21 // in the media level. The concept of "weak symbols" looks like it might |
| 22 // be promising, but I don't quite understand it yet. | 22 // be promising, but I don't quite understand it yet. |
| 23 extern "C" { | 23 extern "C" { |
| 24 | 24 |
| 25 int (*av_get_bits_per_sample_format_ptr)(enum SampleFormat sample_fmt); | |
| 26 int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) { | |
| 27 return av_get_bits_per_sample_format(sample_fmt); | |
| 28 } | |
| 29 | |
| 30 void (*avcodec_init_ptr)(void) = NULL; | 25 void (*avcodec_init_ptr)(void) = NULL; |
| 31 void avcodec_init(void) { | 26 void avcodec_init(void) { |
| 32 avcodec_init_ptr(); | 27 avcodec_init_ptr(); |
| 33 } | 28 } |
| 34 | 29 |
| 35 AVCodec* (*avcodec_find_decoder_ptr)(enum CodecID id) = NULL; | 30 AVCodec* (*avcodec_find_decoder_ptr)(enum CodecID id) = NULL; |
| 36 AVCodec* avcodec_find_decoder(enum CodecID id) { | 31 AVCodec* avcodec_find_decoder(enum CodecID id) { |
| 37 return avcodec_find_decoder_ptr(id); | 32 return avcodec_find_decoder_ptr(id); |
| 38 } | 33 } |
| 39 | 34 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 68 int* got_picture_ptr, const uint8_t* buf, | 63 int* got_picture_ptr, const uint8_t* buf, |
| 69 int buf_size) = NULL; | 64 int buf_size) = NULL; |
| 70 int avcodec_decode_video(AVCodecContext* avctx, AVFrame* picture, | 65 int avcodec_decode_video(AVCodecContext* avctx, AVFrame* picture, |
| 71 int* got_picture_ptr, const uint8_t* buf, | 66 int* got_picture_ptr, const uint8_t* buf, |
| 72 int buf_size) { | 67 int buf_size) { |
| 73 return avcodec_decode_video_ptr(avctx, picture, got_picture_ptr, buf, | 68 return avcodec_decode_video_ptr(avctx, picture, got_picture_ptr, buf, |
| 74 buf_size); | 69 buf_size); |
| 75 } | 70 } |
| 76 | 71 |
| 77 | 72 |
| 73 int (*av_get_bits_per_sample_format_ptr)(enum SampleFormat sample_fmt); |
| 74 int av_get_bits_per_sample_format(enum SampleFormat sample_fmt) { |
| 75 return av_get_bits_per_sample_format_ptr(sample_fmt); |
| 76 } |
| 77 |
| 78 void (*av_register_all_ptr)(void); | 78 void (*av_register_all_ptr)(void); |
| 79 void av_register_all(void) { | 79 void av_register_all(void) { |
| 80 av_register_all_ptr(); | 80 av_register_all_ptr(); |
| 81 } | 81 } |
| 82 | 82 |
| 83 int (*av_open_input_file_ptr)(AVFormatContext** ic_ptr, const char* filename, | 83 int (*av_open_input_file_ptr)(AVFormatContext** ic_ptr, const char* filename, |
| 84 AVInputFormat* fmt, int buf_size, | 84 AVInputFormat* fmt, int buf_size, |
| 85 AVFormatParameters* ap) = NULL; | 85 AVFormatParameters* ap) = NULL; |
| 86 int av_open_input_file(AVFormatContext** ic_ptr, const char* filename, | 86 int av_open_input_file(AVFormatContext** ic_ptr, const char* filename, |
| 87 AVInputFormat* fmt, int buf_size, | 87 AVInputFormat* fmt, int buf_size, |
| 88 AVFormatParameters* ap) { | 88 AVFormatParameters* ap) { |
| 89 return av_open_input_file_ptr(ic_ptr, filename, fmt, buf_size, ap); | 89 return av_open_input_file_ptr(ic_ptr, filename, fmt, buf_size, ap); |
| 90 } | 90 } |
| 91 | 91 |
| 92 int (*av_find_stream_info_ptr)(AVFormatContext* ic) = NULL; | 92 int (*av_find_stream_info_ptr)(AVFormatContext* ic) = NULL; |
| 93 int av_find_stream_info(AVFormatContext* ic) { | 93 int av_find_stream_info(AVFormatContext* ic) { |
| 94 return av_find_stream_info_ptr(ic); | 94 return av_find_stream_info_ptr(ic); |
| 95 } | 95 } |
| 96 | 96 |
| 97 int (*av_read_frame_ptr)(AVFormatContext* s, AVPacket* pkt) = NULL; | 97 int (*av_read_frame_ptr)(AVFormatContext* s, AVPacket* pkt) = NULL; |
| 98 int av_read_frame(AVFormatContext* s, AVPacket* pkt) { | 98 int av_read_frame(AVFormatContext* s, AVPacket* pkt) { |
| 99 return av_read_frame_ptr(s, pkt); | 99 return av_read_frame_ptr(s, pkt); |
| 100 } | 100 } |
| 101 | 101 |
| 102 int (*av_seek_frame_ptr)(AVFormatContext* s, int stream_index, |
| 103 int64_t timestamp, int flags) = NULL; |
| 104 int av_seek_frame(AVFormatContext* s, int stream_index, |
| 105 int64_t timestamp, int flags) { |
| 106 return av_seek_frame_ptr(s, stream_index, timestamp, flags); |
| 107 } |
| 108 |
| 109 int (*av_register_protocol_ptr)(URLProtocol* protocol) = NULL; |
| 110 int av_register_protocol(URLProtocol* protocol) { |
| 111 return av_register_protocol_ptr(protocol); |
| 112 } |
| 113 |
| 102 | 114 |
| 103 void* (*av_malloc_ptr)(unsigned int size) = NULL; | 115 void* (*av_malloc_ptr)(unsigned int size) = NULL; |
| 104 void* av_malloc(unsigned int size) { | 116 void* av_malloc(unsigned int size) { |
| 105 return av_malloc_ptr(size); | 117 return av_malloc_ptr(size); |
| 106 } | 118 } |
| 107 | 119 |
| 120 void (*av_free_ptr)(void* ptr) = NULL; |
| 121 void av_free(void* ptr) { |
| 122 return av_free_ptr(ptr); |
| 123 } |
| 124 |
| 108 } // extern "C" | 125 } // extern "C" |
| 109 | 126 |
| 110 | 127 |
| 111 namespace media { | 128 namespace media { |
| 112 | 129 |
| 113 namespace { | 130 namespace { |
| 114 | 131 |
| 115 enum FFmpegDSOKeys { | 132 enum FFmpegDSOKeys { |
| 116 FILE_LIBAVCODEC, // full path to libavcodec media decoding library. | 133 FILE_LIBAVCODEC, // full path to libavcodec media decoding library. |
| 117 FILE_LIBAVFORMAT, // full path to libavformat media parsing library. | 134 FILE_LIBAVFORMAT, // full path to libavformat media parsing library. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 for (size_t i = 0; i < arraysize(libs) && libs[i] != NULL; ++i) { | 179 for (size_t i = 0; i < arraysize(libs) && libs[i] != NULL; ++i) { |
| 163 dlclose(libs[i]); | 180 dlclose(libs[i]); |
| 164 libs[i] = NULL; // Just to be safe. | 181 libs[i] = NULL; // Just to be safe. |
| 165 } | 182 } |
| 166 return false; | 183 return false; |
| 167 } | 184 } |
| 168 | 185 |
| 169 // TODO(ajwong): Extract this to somewhere saner, and hopefully | 186 // TODO(ajwong): Extract this to somewhere saner, and hopefully |
| 170 // autogenerate the bindings from the .def files. Having all this | 187 // autogenerate the bindings from the .def files. Having all this |
| 171 // code here is incredibly ugly. | 188 // code here is incredibly ugly. |
| 172 av_get_bits_per_sample_format_ptr = | |
| 173 reinterpret_cast<int (*)(enum SampleFormat)>( | |
| 174 dlsym(libs[FILE_LIBAVCODEC], "av_get_bits_per_sample_format")); | |
| 175 avcodec_init_ptr = | 189 avcodec_init_ptr = |
| 176 reinterpret_cast<void(*)(void)>( | 190 reinterpret_cast<void(*)(void)>( |
| 177 dlsym(libs[FILE_LIBAVCODEC], "avcodec_init")); | 191 dlsym(libs[FILE_LIBAVCODEC], "avcodec_init")); |
| 178 avcodec_find_decoder_ptr = | 192 avcodec_find_decoder_ptr = |
| 179 reinterpret_cast<AVCodec* (*)(enum CodecID)>( | 193 reinterpret_cast<AVCodec* (*)(enum CodecID)>( |
| 180 dlsym(libs[FILE_LIBAVCODEC], "avcodec_find_decoder")); | 194 dlsym(libs[FILE_LIBAVCODEC], "avcodec_find_decoder")); |
| 181 avcodec_thread_init_ptr = | 195 avcodec_thread_init_ptr = |
| 182 reinterpret_cast<int (*)(AVCodecContext*, int)>( | 196 reinterpret_cast<int (*)(AVCodecContext*, int)>( |
| 183 dlsym(libs[FILE_LIBAVCODEC], "avcodec_thread_init")); | 197 dlsym(libs[FILE_LIBAVCODEC], "avcodec_thread_init")); |
| 184 avcodec_open_ptr = | 198 avcodec_open_ptr = |
| 185 reinterpret_cast<int (*)(AVCodecContext*, AVCodec*)>( | 199 reinterpret_cast<int (*)(AVCodecContext*, AVCodec*)>( |
| 186 dlsym(libs[FILE_LIBAVCODEC], "avcodec_open")); | 200 dlsym(libs[FILE_LIBAVCODEC], "avcodec_open")); |
| 187 avcodec_alloc_frame_ptr = | 201 avcodec_alloc_frame_ptr = |
| 188 reinterpret_cast<AVFrame* (*)(void)>( | 202 reinterpret_cast<AVFrame* (*)(void)>( |
| 189 dlsym(libs[FILE_LIBAVCODEC], "avcodec_alloc_frame")); | 203 dlsym(libs[FILE_LIBAVCODEC], "avcodec_alloc_frame")); |
| 190 avcodec_decode_audio2_ptr = | 204 avcodec_decode_audio2_ptr = |
| 191 reinterpret_cast<int (*)(AVCodecContext*, int16_t*, int*, | 205 reinterpret_cast<int (*)(AVCodecContext*, int16_t*, int*, |
| 192 const uint8_t*, int)>( | 206 const uint8_t*, int)>( |
| 193 dlsym(libs[FILE_LIBAVCODEC], "avcodec_decode_audio2")); | 207 dlsym(libs[FILE_LIBAVCODEC], "avcodec_decode_audio2")); |
| 194 avcodec_decode_video_ptr = | 208 avcodec_decode_video_ptr = |
| 195 reinterpret_cast<int (*)(AVCodecContext*, AVFrame*, int*, | 209 reinterpret_cast<int (*)(AVCodecContext*, AVFrame*, int*, |
| 196 const uint8_t*, int)>( | 210 const uint8_t*, int)>( |
| 197 dlsym(libs[FILE_LIBAVCODEC], "avcodec_decode_video")); | 211 dlsym(libs[FILE_LIBAVCODEC], "avcodec_decode_video")); |
| 198 | 212 |
| 213 av_get_bits_per_sample_format_ptr = |
| 214 reinterpret_cast<int (*)(enum SampleFormat)>( |
| 215 dlsym(libs[FILE_LIBAVCODEC], "av_get_bits_per_sample_format")); |
| 199 av_register_all_ptr = | 216 av_register_all_ptr = |
| 200 reinterpret_cast<void(*)(void)>( | 217 reinterpret_cast<void(*)(void)>( |
| 201 dlsym(libs[FILE_LIBAVFORMAT], "av_register_all")); | 218 dlsym(libs[FILE_LIBAVFORMAT], "av_register_all")); |
| 202 av_open_input_file_ptr = | 219 av_open_input_file_ptr = |
| 203 reinterpret_cast<int (*)(AVFormatContext**, const char*, | 220 reinterpret_cast<int (*)(AVFormatContext**, const char*, |
| 204 AVInputFormat*, int, | 221 AVInputFormat*, int, |
| 205 AVFormatParameters*)>( | 222 AVFormatParameters*)>( |
| 206 dlsym(libs[FILE_LIBAVFORMAT], "av_open_input_file")); | 223 dlsym(libs[FILE_LIBAVFORMAT], "av_open_input_file")); |
| 207 av_find_stream_info_ptr = | 224 av_find_stream_info_ptr = |
| 208 reinterpret_cast<int (*)(AVFormatContext*)>( | 225 reinterpret_cast<int (*)(AVFormatContext*)>( |
| 209 dlsym(libs[FILE_LIBAVFORMAT], "av_find_stream_info")); | 226 dlsym(libs[FILE_LIBAVFORMAT], "av_find_stream_info")); |
| 210 av_read_frame_ptr = | 227 av_read_frame_ptr = |
| 211 reinterpret_cast<int (*)(AVFormatContext*, AVPacket*)>( | 228 reinterpret_cast<int (*)(AVFormatContext*, AVPacket*)>( |
| 212 dlsym(libs[FILE_LIBAVFORMAT], "av_read_frame")); | 229 dlsym(libs[FILE_LIBAVFORMAT], "av_read_frame")); |
| 230 av_seek_frame_ptr = |
| 231 reinterpret_cast<int (*)(AVFormatContext*, int, int64_t, int)>( |
| 232 dlsym(libs[FILE_LIBAVFORMAT], "av_seek_frame")); |
| 233 av_register_protocol_ptr = |
| 234 reinterpret_cast<int (*)(URLProtocol*)>( |
| 235 dlsym(libs[FILE_LIBAVFORMAT], "av_register_protocol")); |
| 213 | 236 |
| 214 av_malloc_ptr = | 237 av_malloc_ptr = |
| 215 reinterpret_cast<void* (*)(unsigned int)>( | 238 reinterpret_cast<void* (*)(unsigned int)>( |
| 216 dlsym(libs[FILE_LIBAVUTIL], "av_malloc")); | 239 dlsym(libs[FILE_LIBAVUTIL], "av_malloc")); |
| 240 av_free_ptr = |
| 241 reinterpret_cast<void (*)(void*)>( |
| 242 dlsym(libs[FILE_LIBAVUTIL], "av_free")); |
| 217 | 243 |
| 218 // Check that all the symbols were loaded correctly before returning true. | 244 // Check that all the symbols were loaded correctly before returning true. |
| 219 if (avcodec_init_ptr && | 245 if (avcodec_init_ptr && |
| 220 avcodec_find_decoder_ptr && | 246 avcodec_find_decoder_ptr && |
| 221 avcodec_thread_init_ptr && | 247 avcodec_thread_init_ptr && |
| 222 avcodec_open_ptr && | 248 avcodec_open_ptr && |
| 223 avcodec_alloc_frame_ptr && | 249 avcodec_alloc_frame_ptr && |
| 224 avcodec_decode_audio2_ptr && | 250 avcodec_decode_audio2_ptr && |
| 225 avcodec_decode_video_ptr && | 251 avcodec_decode_video_ptr && |
| 226 | 252 |
| 253 av_get_bits_per_sample_format_ptr && |
| 227 av_register_all_ptr && | 254 av_register_all_ptr && |
| 228 av_open_input_file_ptr && | 255 av_open_input_file_ptr && |
| 229 av_find_stream_info_ptr && | 256 av_find_stream_info_ptr && |
| 230 av_read_frame_ptr && | 257 av_read_frame_ptr && |
| 258 av_seek_frame_ptr && |
| 259 av_register_protocol_ptr && |
| 231 | 260 |
| 261 av_free_ptr && |
| 232 av_malloc_ptr) { | 262 av_malloc_ptr) { |
| 233 return true; | 263 return true; |
| 234 } | 264 } |
| 235 | 265 |
| 236 return false; | 266 return false; |
| 237 } | 267 } |
| 238 | 268 |
| 239 } // namespace media | 269 } // namespace media |
| OLD | NEW |