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