content/media/fmp4/ffmpeg/FFmpegDataDecoder.cpp

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include <string.h>
michael@0 8 #include <unistd.h>
michael@0 9
michael@0 10 #include "MediaTaskQueue.h"
michael@0 11 #include "mp4_demuxer/mp4_demuxer.h"
michael@0 12 #include "FFmpegRuntimeLinker.h"
michael@0 13
michael@0 14 #include "FFmpegDataDecoder.h"
michael@0 15
michael@0 16 namespace mozilla
michael@0 17 {
michael@0 18
michael@0 19 bool FFmpegDataDecoder::sFFmpegInitDone = false;
michael@0 20
michael@0 21 FFmpegDataDecoder::FFmpegDataDecoder(MediaTaskQueue* aTaskQueue,
michael@0 22 AVCodecID aCodecID)
michael@0 23 : mTaskQueue(aTaskQueue), mCodecID(aCodecID)
michael@0 24 {
michael@0 25 MOZ_COUNT_CTOR(FFmpegDataDecoder);
michael@0 26 }
michael@0 27
michael@0 28 FFmpegDataDecoder::~FFmpegDataDecoder() {
michael@0 29 MOZ_COUNT_DTOR(FFmpegDataDecoder);
michael@0 30 }
michael@0 31
michael@0 32 /**
michael@0 33 * FFmpeg calls back to this function with a list of pixel formats it supports.
michael@0 34 * We choose a pixel format that we support and return it.
michael@0 35 * For now, we just look for YUV420P as it is the only non-HW accelerated format
michael@0 36 * supported by FFmpeg's H264 decoder.
michael@0 37 */
michael@0 38 static PixelFormat
michael@0 39 ChoosePixelFormat(AVCodecContext* aCodecContext, const PixelFormat* aFormats)
michael@0 40 {
michael@0 41 FFMPEG_LOG("Choosing FFmpeg pixel format for video decoding.");
michael@0 42 for (; *aFormats > -1; aFormats++) {
michael@0 43 if (*aFormats == PIX_FMT_YUV420P) {
michael@0 44 FFMPEG_LOG("Requesting pixel format YUV420P.");
michael@0 45 return PIX_FMT_YUV420P;
michael@0 46 }
michael@0 47 }
michael@0 48
michael@0 49 NS_WARNING("FFmpeg does not share any supported pixel formats.");
michael@0 50 return PIX_FMT_NONE;
michael@0 51 }
michael@0 52
michael@0 53 nsresult
michael@0 54 FFmpegDataDecoder::Init()
michael@0 55 {
michael@0 56 FFMPEG_LOG("Initialising FFmpeg decoder.");
michael@0 57
michael@0 58 if (!FFmpegRuntimeLinker::Link()) {
michael@0 59 NS_WARNING("Failed to link FFmpeg shared libraries.");
michael@0 60 return NS_ERROR_FAILURE;
michael@0 61 }
michael@0 62
michael@0 63 if (!sFFmpegInitDone) {
michael@0 64 av_register_all();
michael@0 65 #ifdef DEBUG
michael@0 66 av_log_set_level(AV_LOG_DEBUG);
michael@0 67 #endif
michael@0 68 sFFmpegInitDone = true;
michael@0 69 }
michael@0 70
michael@0 71 AVCodec* codec = avcodec_find_decoder(mCodecID);
michael@0 72 if (!codec) {
michael@0 73 NS_WARNING("Couldn't find ffmpeg decoder");
michael@0 74 return NS_ERROR_FAILURE;
michael@0 75 }
michael@0 76
michael@0 77 if (avcodec_get_context_defaults3(&mCodecContext, codec) < 0) {
michael@0 78 NS_WARNING("Couldn't init ffmpeg context");
michael@0 79 return NS_ERROR_FAILURE;
michael@0 80 }
michael@0 81
michael@0 82 mCodecContext.opaque = this;
michael@0 83
michael@0 84 // FFmpeg takes this as a suggestion for what format to use for audio samples.
michael@0 85 mCodecContext.request_sample_fmt = AV_SAMPLE_FMT_FLT;
michael@0 86
michael@0 87 // FFmpeg will call back to this to negotiate a video pixel format.
michael@0 88 mCodecContext.get_format = ChoosePixelFormat;
michael@0 89
michael@0 90 AVDictionary* opts = nullptr;
michael@0 91 if (avcodec_open2(&mCodecContext, codec, &opts) < 0) {
michael@0 92 NS_WARNING("Couldn't initialise ffmpeg decoder");
michael@0 93 return NS_ERROR_FAILURE;
michael@0 94 }
michael@0 95
michael@0 96 if (mCodecContext.codec_type == AVMEDIA_TYPE_AUDIO &&
michael@0 97 mCodecContext.sample_fmt != AV_SAMPLE_FMT_FLT &&
michael@0 98 mCodecContext.sample_fmt != AV_SAMPLE_FMT_FLTP) {
michael@0 99 NS_WARNING("FFmpeg AAC decoder outputs unsupported audio format.");
michael@0 100 return NS_ERROR_FAILURE;
michael@0 101 }
michael@0 102
michael@0 103 FFMPEG_LOG("FFmpeg init successful.");
michael@0 104 return NS_OK;
michael@0 105 }
michael@0 106
michael@0 107 nsresult
michael@0 108 FFmpegDataDecoder::Flush()
michael@0 109 {
michael@0 110 mTaskQueue->Flush();
michael@0 111 avcodec_flush_buffers(&mCodecContext);
michael@0 112 return NS_OK;
michael@0 113 }
michael@0 114
michael@0 115 nsresult
michael@0 116 FFmpegDataDecoder::Shutdown()
michael@0 117 {
michael@0 118 if (sFFmpegInitDone) {
michael@0 119 avcodec_close(&mCodecContext);
michael@0 120 }
michael@0 121 return NS_OK;
michael@0 122 }
michael@0 123
michael@0 124 } // namespace mozilla

mercurial