summaryrefslogtreecommitdiffstats
path: root/media/mf/main.cc
blob: 93ca4718b6bcde7b0b1de7bd57a246ad90aa4ae3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// 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.
//
// Demonstrates the use of H264Mft.

#include <d3d9.h>
#include <dxva2api.h>
#include <mfapi.h>

#include "base/command_line.h"
#include "base/file_path.h"
#include "base/logging.h"
#include "base/scoped_comptr_win.h"
#include "base/scoped_ptr.h"
#include "base/time.h"
#include "media/base/media.h"
#include "media/ffmpeg/ffmpeg_common.h"
#include "media/ffmpeg/file_protocol.h"
#include "media/mf/file_reader_util.h"
#include "media/mf/h264mft.h"

using media::FFmpegFileReader;
using media::H264Mft;
using media::VideoFrame;

namespace {

void usage() {
  static char* usage_msg =
      "Usage: h264mft [--enable-dxva] --input-file=FILE\n"
      "enable-dxva: Enables hardware accelerated decoding\n"
      "To display this message: h264mft --help";
  fprintf(stderr, "%s\n", usage_msg);
}

static bool InitFFmpeg() {
  if (!media::InitializeMediaLibrary(FilePath()))
    return false;
  avcodec_init();
  av_register_all();
  av_register_protocol(&kFFmpegFileProtocol);
  return true;
}

bool InitComLibraries() {
  HRESULT hr;
  hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
  if (FAILED(hr)) {
    LOG(ERROR) << "CoInit fail";
    return false;
  }
  hr = MFStartup(MF_VERSION, MFSTARTUP_FULL);
  if (FAILED(hr)) {
    LOG(ERROR) << "MFStartup fail";
    CoUninitialize();
    return false;
  }
  return true;
}

void ShutdownComLibraries() {
  HRESULT hr;
  hr = MFShutdown();
  if (FAILED(hr)) {
    LOG(WARNING) << "Warning: MF failed to shutdown";
  }
  CoUninitialize();
}

IDirect3DDeviceManager9* CreateD3DDevManager(HWND video_window,
                                             int width,
                                             int height,
                                             IDirect3D9** direct3d,
                                             IDirect3DDevice9** device) {
  CHECK(video_window != NULL);
  CHECK(direct3d != NULL);
  CHECK(device != NULL);

  ScopedComPtr<IDirect3DDeviceManager9> dev_manager;
  ScopedComPtr<IDirect3D9> d3d;
  d3d.Attach(Direct3DCreate9(D3D_SDK_VERSION));
  if (d3d == NULL) {
    LOG(ERROR) << "Failed to create D3D9";
    return NULL;
  }
  D3DPRESENT_PARAMETERS present_params = {0};

  present_params.BackBufferWidth = width;
  present_params.BackBufferHeight = height;
  present_params.BackBufferFormat = D3DFMT_UNKNOWN;
  present_params.BackBufferCount = 1;
  present_params.SwapEffect = D3DSWAPEFFECT_DISCARD;
  present_params.hDeviceWindow = video_window;
  present_params.Windowed = TRUE;
  present_params.Flags = D3DPRESENTFLAG_VIDEO;
  present_params.FullScreen_RefreshRateInHz = 0;
  present_params.PresentationInterval = 0;

  ScopedComPtr<IDirect3DDevice9> temp_device;

  // D3DCREATE_HARDWARE_VERTEXPROCESSING specifies hardware vertex processing.
  // (Is it even needed for just video decoding?)
  HRESULT hr = d3d->CreateDevice(D3DADAPTER_DEFAULT,
                                 D3DDEVTYPE_HAL,
                                 video_window,
                                 D3DCREATE_HARDWARE_VERTEXPROCESSING,
                                 &present_params,
                                 temp_device.Receive());
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to create D3D Device";
    return NULL;
  }
  UINT dev_manager_reset_token = 0;
  hr = DXVA2CreateDirect3DDeviceManager9(&dev_manager_reset_token,
                                         dev_manager.Receive());
  if (FAILED(hr)) {
    LOG(ERROR) << "Couldn't create D3D Device manager";
    return NULL;
  }
  hr = dev_manager->ResetDevice(temp_device.get(), dev_manager_reset_token);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to set device to device manager";
    return NULL;
  }
  *direct3d = d3d.Detach();
  *device = temp_device.Detach();
  return dev_manager.Detach();
}

// Example usage of how to get a decoded frame from the decoder.
bool GetDecodedSample(FFmpegFileReader* reader, H264Mft* decoder,
                      scoped_refptr<VideoFrame>* decoded_frame) {
  // Keep feeding the MFT with inputs until it spits out an output.
  for (;;) {
    // First check if there is output.
    H264Mft::DecoderOutputState state = decoder->GetOutput(decoded_frame);
    if (state == H264Mft::kOutputOk) {
      LOG(INFO) << "Got an output from decoder";
      return true;
    } else if (state == H264Mft::kResetOutputStreamFailed) {
      LOG(ERROR) << "Reset output stream failed, quitting";
      return false;
    } else if (state == H264Mft::kResetOutputStreamOk) {
      LOG(INFO) << "Reset output stream, try to get output again";
      continue;
    } else if (state == H264Mft::kNeedMoreInput) {
      LOG(INFO) << "Need more input";
      uint8* input_stream_dummy;
      int size;
      int duration;
      int64 timestamp;
      reader->Read(&input_stream_dummy, &size, &duration, &timestamp);
      scoped_array<uint8> input_stream(input_stream_dummy);
      if (input_stream.get() == NULL) {
        LOG(INFO) << "No more input, sending drain message to decoder";
        if (!decoder->SendDrainMessage()) {
          LOG(ERROR) << "Failed to send drain message, quitting";
          return false;
        } else {
          continue;   // Try reading the rest of the drained outputs.
        }
      } else {
        // We read an input stream, we can feed it into the decoder.
        if (!decoder->SendInput(input_stream.get(), size,
            reader->ConvertFFmpegTimeBaseTo100Ns(timestamp),
            reader->ConvertFFmpegTimeBaseTo100Ns(duration))) {
          LOG(ERROR) << "Failed to send input, dropping frame...";
        }
        continue;  // Try reading the output after attempting to send an input.
      }
    } else if (state == H264Mft::kNoMoreOutput) {
      LOG(INFO) << "Decoder has processed everything, quitting";
      return false;
    } else if (state == H264Mft::kUnspecifiedError) {
      LOG(ERROR) << "Unknown error, quitting";
      return false;
    } else if (state == H264Mft::kNoMemory) {
      LOG(ERROR) << "Not enough memory for sample, quitting";
      return false;
    } else if (state == H264Mft::kOutputSampleError) {
      LOG(ERROR) << "Inconsistent sample, dropping...";
      continue;
    } else {
      NOTREACHED();
    }
  }  // for (;;)
  NOTREACHED();
}

static void ReleaseOutputBuffer(VideoFrame* frame) {
  if (frame->type() == VideoFrame::TYPE_MFBUFFER ||
      frame->type() == VideoFrame::TYPE_DIRECT3DSURFACE) {
    static_cast<IMFMediaBuffer*>(frame->private_buffer())->Release();
  } else {
    return;
  }
}

int Run(bool use_dxva, const std::string& input_file) {
  scoped_ptr<FFmpegFileReader> reader(new FFmpegFileReader(input_file));
  if (reader.get() == NULL) {
    LOG(ERROR) << "Failed to create reader";
    return -1;
  }
  if (!reader->Initialize()) {
    LOG(ERROR) << "Failed to initialize reader";
    return -1;
  }
  int frame_rate_num = 0, frame_rate_denom = 0;
  if (!reader->GetFrameRate(&frame_rate_num, &frame_rate_denom)) {
    LOG(WARNING) << "Failed to get frame rate from reader";
  }
  int width = 0, height = 0;
  if (!reader->GetWidth(&width) || !reader->GetHeight(&height)) {
    LOG(WARNING) << "Failed to get width/height from reader";
  }
  int aspect_ratio_num = 0, aspect_ratio_denom = 0;
  if (!reader->GetAspectRatio(&aspect_ratio_num, &aspect_ratio_denom)) {
    LOG(WARNING) << "Failed to get aspect ratio from reader";
  }
  ScopedComPtr<IDirect3D9> d3d9;
  ScopedComPtr<IDirect3DDevice9> device;
  ScopedComPtr<IDirect3DDeviceManager9> dev_manager;
  if (use_dxva) {
    dev_manager.Attach(CreateD3DDevManager(GetDesktopWindow(),
                                           width,
                                           height,
                                           d3d9.Receive(),
                                           device.Receive()));
    if (dev_manager.get() == NULL) {
      LOG(ERROR) << "Cannot create D3D9 manager";
      return -1;
    }
  }
  scoped_ptr<H264Mft> mft(new H264Mft(use_dxva));
  if (mft.get() == NULL) {
    LOG(ERROR) << "Failed to create MFT";
    return -1;
  }
  if (!mft->Init(dev_manager, frame_rate_num, frame_rate_denom, width, height,
                 aspect_ratio_num, aspect_ratio_denom)) {
    LOG(ERROR) << "Failed to initialize mft";
    return -1;
  }
  base::TimeDelta decode_time;
  while (true) {
    // Do nothing with the sample except to let it go out of scope
    scoped_refptr<VideoFrame> decoded_frame;
    base::Time decode_start(base::Time::Now());
    if (!GetDecodedSample(reader.get(), mft.get(), &decoded_frame))
      break;
    decode_time += base::Time::Now() - decode_start;
    ReleaseOutputBuffer(decoded_frame.get());
  }
  printf("All done, frames read: %d, frames decoded: %d\n",
         mft->frames_read(), mft->frames_decoded());
  printf("Took %lldms\n", decode_time.InMilliseconds());
  return 0;
}

}  // namespace

int main(int argc, char** argv) {
  CommandLine::Init(argc, argv);
  if (argc == 1) {
    fprintf(stderr, "Not enough arguments\n");
    usage();
    return -1;
  }

  const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
  if (cmd_line.HasSwitch("help")) {
    usage();
    return -1;
  }
  bool use_dxva = cmd_line.HasSwitch("enable-dxva");
  std::string input_file = cmd_line.GetSwitchValueASCII("input-file");
  if (input_file.empty()) {
    fprintf(stderr, "No input file provided\n");
    usage();
    return -1;
  }
  printf("enable-dxva: %d\n", use_dxva);
  printf("input-file: %s\n", input_file.c_str());

  if (!InitFFmpeg()) {
    LOG(ERROR) << "InitFFMpeg() failed";
    return -1;
  }
  if (!InitComLibraries()) {
    LOG(ERROR) << "InitComLibraries() failed";
    return -1;
  }
  int ret = Run(use_dxva, input_file);
  ShutdownComLibraries();
  printf("Done\n");
  return ret;
}