content/media/fmp4/ffmpeg/FFmpegDataDecoder.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/media/fmp4/ffmpeg/FFmpegDataDecoder.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,124 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include <string.h>
    1.11 +#include <unistd.h>
    1.12 +
    1.13 +#include "MediaTaskQueue.h"
    1.14 +#include "mp4_demuxer/mp4_demuxer.h"
    1.15 +#include "FFmpegRuntimeLinker.h"
    1.16 +
    1.17 +#include "FFmpegDataDecoder.h"
    1.18 +
    1.19 +namespace mozilla
    1.20 +{
    1.21 +
    1.22 +bool FFmpegDataDecoder::sFFmpegInitDone = false;
    1.23 +
    1.24 +FFmpegDataDecoder::FFmpegDataDecoder(MediaTaskQueue* aTaskQueue,
    1.25 +                                     AVCodecID aCodecID)
    1.26 +  : mTaskQueue(aTaskQueue), mCodecID(aCodecID)
    1.27 +{
    1.28 +  MOZ_COUNT_CTOR(FFmpegDataDecoder);
    1.29 +}
    1.30 +
    1.31 +FFmpegDataDecoder::~FFmpegDataDecoder() {
    1.32 +  MOZ_COUNT_DTOR(FFmpegDataDecoder);
    1.33 +}
    1.34 +
    1.35 +/**
    1.36 + * FFmpeg calls back to this function with a list of pixel formats it supports.
    1.37 + * We choose a pixel format that we support and return it.
    1.38 + * For now, we just look for YUV420P as it is the only non-HW accelerated format
    1.39 + * supported by FFmpeg's H264 decoder.
    1.40 + */
    1.41 +static PixelFormat
    1.42 +ChoosePixelFormat(AVCodecContext* aCodecContext, const PixelFormat* aFormats)
    1.43 +{
    1.44 +  FFMPEG_LOG("Choosing FFmpeg pixel format for video decoding.");
    1.45 +  for (; *aFormats > -1; aFormats++) {
    1.46 +    if (*aFormats == PIX_FMT_YUV420P) {
    1.47 +      FFMPEG_LOG("Requesting pixel format YUV420P.");
    1.48 +      return PIX_FMT_YUV420P;
    1.49 +    }
    1.50 +  }
    1.51 +
    1.52 +  NS_WARNING("FFmpeg does not share any supported pixel formats.");
    1.53 +  return PIX_FMT_NONE;
    1.54 +}
    1.55 +
    1.56 +nsresult
    1.57 +FFmpegDataDecoder::Init()
    1.58 +{
    1.59 +  FFMPEG_LOG("Initialising FFmpeg decoder.");
    1.60 +
    1.61 +  if (!FFmpegRuntimeLinker::Link()) {
    1.62 +    NS_WARNING("Failed to link FFmpeg shared libraries.");
    1.63 +    return NS_ERROR_FAILURE;
    1.64 +  }
    1.65 +
    1.66 +  if (!sFFmpegInitDone) {
    1.67 +    av_register_all();
    1.68 +#ifdef DEBUG
    1.69 +    av_log_set_level(AV_LOG_DEBUG);
    1.70 +#endif
    1.71 +    sFFmpegInitDone = true;
    1.72 +  }
    1.73 +
    1.74 +  AVCodec* codec = avcodec_find_decoder(mCodecID);
    1.75 +  if (!codec) {
    1.76 +    NS_WARNING("Couldn't find ffmpeg decoder");
    1.77 +    return NS_ERROR_FAILURE;
    1.78 +  }
    1.79 +
    1.80 +  if (avcodec_get_context_defaults3(&mCodecContext, codec) < 0) {
    1.81 +    NS_WARNING("Couldn't init ffmpeg context");
    1.82 +    return NS_ERROR_FAILURE;
    1.83 +  }
    1.84 +
    1.85 +  mCodecContext.opaque = this;
    1.86 +
    1.87 +  // FFmpeg takes this as a suggestion for what format to use for audio samples.
    1.88 +  mCodecContext.request_sample_fmt = AV_SAMPLE_FMT_FLT;
    1.89 +
    1.90 +  // FFmpeg will call back to this to negotiate a video pixel format.
    1.91 +  mCodecContext.get_format = ChoosePixelFormat;
    1.92 +
    1.93 +  AVDictionary* opts = nullptr;
    1.94 +  if (avcodec_open2(&mCodecContext, codec, &opts) < 0) {
    1.95 +    NS_WARNING("Couldn't initialise ffmpeg decoder");
    1.96 +    return NS_ERROR_FAILURE;
    1.97 +  }
    1.98 +
    1.99 +  if (mCodecContext.codec_type == AVMEDIA_TYPE_AUDIO &&
   1.100 +      mCodecContext.sample_fmt != AV_SAMPLE_FMT_FLT &&
   1.101 +      mCodecContext.sample_fmt != AV_SAMPLE_FMT_FLTP) {
   1.102 +    NS_WARNING("FFmpeg AAC decoder outputs unsupported audio format.");
   1.103 +    return NS_ERROR_FAILURE;
   1.104 +  }
   1.105 +
   1.106 +  FFMPEG_LOG("FFmpeg init successful.");
   1.107 +  return NS_OK;
   1.108 +}
   1.109 +
   1.110 +nsresult
   1.111 +FFmpegDataDecoder::Flush()
   1.112 +{
   1.113 +  mTaskQueue->Flush();
   1.114 +  avcodec_flush_buffers(&mCodecContext);
   1.115 +  return NS_OK;
   1.116 +}
   1.117 +
   1.118 +nsresult
   1.119 +FFmpegDataDecoder::Shutdown()
   1.120 +{
   1.121 +  if (sFFmpegInitDone) {
   1.122 +    avcodec_close(&mCodecContext);
   1.123 +  }
   1.124 +  return NS_OK;
   1.125 +}
   1.126 +
   1.127 +} // namespace mozilla

mercurial