OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this | |
2 // source code is governed by a BSD-style license that can be found in the | |
3 // LICENSE file. | |
4 | |
5 // A test program that drives an OpenMAX video decoder module. This program | |
6 // will take video in elementary stream and read into the decoder. | |
7 // Usage of this program: | |
8 // ./omx_test --file=<file> --component=<component> --codec=<codec> | |
9 // <file> = Input file name | |
10 // <component> = Name of the OpenMAX component | |
11 // <codec> = Codec to be used, available codecs: h264, vc1, mpeg4, h263. | |
12 | |
13 #include "base/at_exit.h" | |
14 #include "base/command_line.h" | |
15 #include "base/message_loop.h" | |
16 #include "base/scoped_ptr.h" | |
17 #include "media/omx/input_buffer.h" | |
18 #include "media/omx/omx_video_decoder.h" | |
19 | |
20 // This is the driver object to feed the decoder with data from a file. | |
21 // It also provides callbacks for the decoder to receive events from the | |
22 // decoder. | |
23 class TestApp { | |
24 public: | |
25 TestApp(const char* filename, | |
26 const char* component, | |
27 media::OmxVideoDecoder::Codec codec) | |
28 : filename_(filename), | |
29 component_(component), | |
30 codec_(codec), | |
31 stopped_(false), | |
32 error_(false) { | |
33 } | |
34 | |
35 void StopCallback() { | |
36 // If this callback is received, mark the |stopped_| flag so that we don't | |
37 // feed more buffers into the decoder. | |
38 // We need to exit the current message loop because we have no more work | |
39 // to do on the message loop. This is done by calling | |
40 // message_loop_.Quit(). | |
41 stopped_ = true; | |
42 message_loop_.Quit(); | |
43 } | |
44 | |
45 void ErrorCallback() { | |
46 // In case of error, this method is called. Mark the error flag and | |
47 // exit the message loop because we have no more work to do. | |
48 printf("Error callback received!\n"); | |
49 error_ = true; | |
50 message_loop_.Quit(); | |
51 } | |
52 | |
53 void FeedCallback(media::InputBuffer* buffer) { | |
54 // We receive this callback when the decoder has consumed an input buffer. | |
55 // In this case, delete the previous buffer and enqueue a new one. | |
56 // There are some conditions we don't want to enqueue, for example when | |
57 // the last buffer is an end-of-stream buffer, when we have stopped, and | |
58 // when we have received an error. | |
59 bool eos = buffer->Eos(); | |
60 delete buffer; | |
61 if (!eos && !stopped_ && !error_) | |
62 FeedDecoder(); | |
63 } | |
64 | |
65 void ReadCompleteCallback(uint8* buffer, int size) { | |
66 // This callback is received when the decoder has completed a decoding | |
67 // task and give us some output data. The buffer is owned by the decoder. | |
awong
2009/11/13 22:03:09
give us -> given us
| |
68 if (stopped_ || error_) | |
69 return; | |
70 | |
71 // If we are readding to the end, then stop. | |
72 if (!size) { | |
73 decoder_->Stop(NewCallback(this, &TestApp::StopCallback)); | |
74 return; | |
75 } | |
76 | |
77 // Read one more from the decoder. | |
78 decoder_->Read(NewCallback(this, &TestApp::ReadCompleteCallback)); | |
79 } | |
80 | |
81 void FeedDecoder() { | |
82 // This method feeds the decoder with 32KB of input data. | |
83 const int kSize = 32768; | |
84 uint8* data = new uint8[kSize]; | |
85 int read = fread(data, 1, kSize, file_); | |
86 decoder_->Feed(new media::InputBuffer(data, read), | |
87 NewCallback(this, &TestApp::FeedCallback)); | |
88 } | |
89 | |
90 void Run() { | |
91 // Open the input file. | |
92 file_ = fopen(filename_, "rb"); | |
93 if (!file_) { | |
94 printf("Error - can't open file %s\n", filename_); | |
95 return; | |
96 } | |
97 | |
98 // Setup the decoder with the message loop of the current thread. Also | |
99 // setup component name, codec and callbacks. | |
100 decoder_ = new media::OmxVideoDecoder(&message_loop_); | |
101 decoder_->Setup(component_, codec_); | |
102 decoder_->SetErrorCallback(NewCallback(this, &TestApp::ErrorCallback)); | |
103 | |
104 // Start the decoder. | |
105 decoder_->Start(); | |
106 for (int i = 0; i < 20; ++i) | |
107 FeedDecoder(); | |
108 decoder_->Read(NewCallback(this, &TestApp::ReadCompleteCallback)); | |
109 | |
110 // Execute the message loop so that we can run tasks on it. This call | |
111 // will return when we call message_loop_.Quit(). | |
112 message_loop_.Run(); | |
113 } | |
114 | |
115 scoped_refptr<media::OmxVideoDecoder> decoder_; | |
116 MessageLoop message_loop_; | |
117 const char* filename_; | |
118 const char* component_; | |
119 media::OmxVideoDecoder::Codec codec_; | |
120 FILE* file_; | |
121 bool stopped_; | |
122 bool error_; | |
123 }; | |
124 | |
125 int main(int argc, char** argv) { | |
126 base::AtExitManager at_exit_manager; | |
127 | |
128 CommandLine::Init(argc, argv); | |
129 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
130 | |
131 if (argc < 2) { | |
132 printf("Usage: omx_test --file=FILE --component=COMPONENT --codec=CODEC\n"); | |
133 printf(" COMPONENT: OpenMAX component name\n"); | |
134 printf(" CODEC: h264/mpeg4/h263/vc1\n"); | |
135 return 1; | |
136 } | |
137 | |
138 std::string filename = cmd_line->GetSwitchValueASCII("file"); | |
139 std::string component = cmd_line->GetSwitchValueASCII("component"); | |
140 std::string codec = cmd_line->GetSwitchValueASCII("codec"); | |
141 | |
142 media::OmxVideoDecoder::Codec codec_id = media::OmxVideoDecoder::kCodecNone; | |
143 if (codec == "h264") | |
144 codec_id = media::OmxVideoDecoder::kCodecH264; | |
145 else if (codec == "mpeg4") | |
146 codec_id = media::OmxVideoDecoder::kCodecMpeg4; | |
147 else if (codec == "h263") | |
148 codec_id = media::OmxVideoDecoder::kCodecH263; | |
149 else if (codec == "vc1") | |
150 codec_id = media::OmxVideoDecoder::kCodecVc1; | |
151 else { | |
152 printf("Unknown codec.\n"); | |
153 return 1; | |
154 } | |
155 | |
156 // Create a TestApp object and run the decoder. | |
157 TestApp test(filename.c_str(), component.c_str(), codec_id); | |
158 | |
159 // This call will run the decoder until EOS is reached or an error | |
160 // is encountered. | |
161 test.Run(); | |
162 return 0; | |
163 } | |
OLD | NEW |