diff options
Diffstat (limited to 'media/ffmpeg')
-rw-r--r-- | media/ffmpeg/ffmpeg_common.cc | 26 | ||||
-rw-r--r-- | media/ffmpeg/ffmpeg_common.h | 82 | ||||
-rw-r--r-- | media/ffmpeg/ffmpeg_util.cc | 8 | ||||
-rw-r--r-- | media/ffmpeg/file_protocol.cc | 84 | ||||
-rw-r--r-- | media/ffmpeg/file_protocol.h | 14 |
5 files changed, 210 insertions, 4 deletions
diff --git a/media/ffmpeg/ffmpeg_common.cc b/media/ffmpeg/ffmpeg_common.cc new file mode 100644 index 0000000..5100b31 --- /dev/null +++ b/media/ffmpeg/ffmpeg_common.cc @@ -0,0 +1,26 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "media/ffmpeg/ffmpeg_common.h" + +namespace media { + +FFmpegLock::FFmpegLock() { +} + +FFmpegLock::~FFmpegLock() { +} + +Lock& FFmpegLock::lock() { + return lock_; +} + +namespace mime_type { + +const char kFFmpegAudio[] = "audio/x-ffmpeg"; +const char kFFmpegVideo[] = "video/x-ffmpeg"; + +} // namespace mime_type + +} // namespace media diff --git a/media/ffmpeg/ffmpeg_common.h b/media/ffmpeg/ffmpeg_common.h new file mode 100644 index 0000000..9be245f --- /dev/null +++ b/media/ffmpeg/ffmpeg_common.h @@ -0,0 +1,82 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MEDIA_FFMPEG_FFMPEG_COMMON_H_ +#define MEDIA_FFMPEG_FFMPEG_COMMON_H_ + +// Used for FFmpeg error codes. +#include <cerrno> + +#include "base/compiler_specific.h" +#include "base/singleton.h" + +// Used with URLProtocol. +typedef int64 offset_t; + +// Include FFmpeg header files. +extern "C" { +// Temporarily disable possible loss of data warning. +// TODO(scherkus): fix and upstream the compiler warnings. +MSVC_PUSH_DISABLE_WARNING(4244); +#include "third_party/ffmpeg/include/libavcodec/avcodec.h" +#include "third_party/ffmpeg/include/libavformat/avformat.h" +#include "third_party/ffmpeg/include/libavutil/log.h" +MSVC_POP_WARNING(); +} // extern "C" + +namespace media { + +// FFmpegLock is used to serialize calls to avcodec_open(), avcodec_close(), +// and av_find_stream_info() for an entire process because for whatever reason +// it does Very Bad Things to other FFmpeg instances. +// +// TODO(scherkus): track down and upstream a fix to FFmpeg, if possible. +class FFmpegLock : public Singleton<FFmpegLock> { + public: + Lock& lock(); + + private: + // Only allow Singleton to create and delete FFmpegLock. + friend struct DefaultSingletonTraits<FFmpegLock>; + FFmpegLock(); + virtual ~FFmpegLock(); + + Lock lock_; + DISALLOW_COPY_AND_ASSIGN(FFmpegLock); +}; + + +// Wraps FFmpeg's av_free() in a class that can be passed as a template argument +// to scoped_ptr_malloc. +class ScopedPtrAVFree { + public: + inline void operator()(void* x) const { + av_free(x); + } +}; + +// This assumes that the AVPacket being captured was allocated outside of +// FFmpeg via the new operator. Do not use this with AVPacket instances that +// are allocated via malloc() or av_malloc(). +class ScopedPtrAVFreePacket { + public: + inline void operator()(void* x) const { + AVPacket* packet = static_cast<AVPacket*>(x); + av_free_packet(packet); + delete packet; + } +}; + + +// FFmpeg MIME types. +namespace mime_type { + +extern const char kFFmpegAudio[]; +extern const char kFFmpegVideo[]; + +} // namespace mime_type + +} // namespace media + +#endif // MEDIA_FFMPEG_FFMPEG_COMMON_H_ diff --git a/media/ffmpeg/ffmpeg_util.cc b/media/ffmpeg/ffmpeg_util.cc index 1be3b12..aa9f7a27 100644 --- a/media/ffmpeg/ffmpeg_util.cc +++ b/media/ffmpeg/ffmpeg_util.cc @@ -1,10 +1,10 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this -// source code is governed by a BSD-style license that can be found in the -// LICENSE file. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. #include "media/ffmpeg/ffmpeg_util.h" -#include "media/filters/ffmpeg_common.h" +#include "media/ffmpeg/ffmpeg_common.h" namespace media { diff --git a/media/ffmpeg/file_protocol.cc b/media/ffmpeg/file_protocol.cc new file mode 100644 index 0000000..2d1bd1d --- /dev/null +++ b/media/ffmpeg/file_protocol.cc @@ -0,0 +1,84 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "media/ffmpeg/file_protocol.h" + +#include "build/build_config.h" + +#if defined(OS_WIN) +#include <io.h> +#else +#include <unistd.h> +#endif +#include <fcntl.h> + +#include "base/compiler_specific.h" +#include "base/file_util.h" +#include "base/logging.h" +#include "media/ffmpeg/ffmpeg_common.h" + +// warning C4996: 'open': The POSIX name for this item is deprecated. +MSVC_PUSH_DISABLE_WARNING(4996) + +namespace { + +int GetHandle(URLContext *h) { + return static_cast<int>(reinterpret_cast<intptr_t>(h->priv_data)); +} + +// FFmpeg protocol interface. +int OpenContext(URLContext* h, const char* filename, int flags) { + int access = O_RDONLY; + if (flags & URL_RDWR) { + access = O_CREAT | O_TRUNC | O_RDWR; + } else if (flags & URL_WRONLY) { + access = O_CREAT | O_TRUNC | O_WRONLY; + } +#ifdef O_BINARY + access |= O_BINARY; +#endif + int f = open(filename, access, 0666); + if (f == -1) + return AVERROR(ENOENT); + h->priv_data = reinterpret_cast<void*>(static_cast<intptr_t>(f)); + h->is_streamed = false; + return 0; +} + +int ReadContext(URLContext* h, unsigned char* buf, int size) { + return read(GetHandle(h), buf, size); +} + +int WriteContext(URLContext* h, unsigned char* buf, int size) { + return write(GetHandle(h), buf, size); +} + +offset_t SeekContext(URLContext* h, offset_t offset, int whence) { +#if defined(OS_WIN) + return lseek(GetHandle(h), static_cast<long>(offset), whence); +#else + return lseek(GetHandle(h), offset, whence); +#endif +} + +int CloseContext(URLContext* h) { + return close(GetHandle(h)); +} + +} // namespace + +MSVC_POP_WARNING() + +URLProtocol kFFmpegFileProtocol = { + "file", + &OpenContext, + &ReadContext, + &WriteContext, + &SeekContext, + &CloseContext, + NULL, // *next + NULL, // url_read_pause + NULL, // url_read_seek + &GetHandle +}; diff --git a/media/ffmpeg/file_protocol.h b/media/ffmpeg/file_protocol.h new file mode 100644 index 0000000..ed37467 --- /dev/null +++ b/media/ffmpeg/file_protocol.h @@ -0,0 +1,14 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// Implements a basic file I/O URLProtocol for FFmpeg. Since we don't build +// FFmpeg binaries with protocols, we have to write our own. + +#ifndef MEDIA_FFMPEG_FILE_PROTOCOL_H_ +#define MEDIA_FFMPEG_FILE_PROTOCOL_H_ + +struct URLProtocol; +extern URLProtocol kFFmpegFileProtocol; + +#endif // MEDIA_FFMPEG_FILE_PROTOCOL_H_ |