Debian Codegraph @

lib/aviread/ReadStreamV.cpp

raw
1 /*
2 * ReadStreamV ( video AVI stream ) functions
3 */
4
5 #include "ReadStreamV.h"
6 #include "videodecoder.h"
7 #include "avm_creators.h"
8 #include "avm_fourcc.h"
9 #include "avm_cpuinfo.h"
10 #include "avm_output.h"
11
12 #include "utils.h"
13
14 #include <string.h>
15 #include <stdio.h>
16
17 AVM_BEGIN_NAMESPACE;
18 //static float ttt = 0;
19 //static float pttt = 0;
20 //static float gttt = 0;
21
22 // it's quite similar to qring - but
23 // for now there are few differencies which do not
24 // make possible to use qring instead of this VideoQueue
25 class VideoQueue : public IImageAllocator
26 {
27 friend class ReadStreamV;
28 static const size_t MAX_QSIZE = 100;
29 public:
30 VideoQueue(IVideoDecoder* vd, size_t maxsize, IImageAllocator* ia)
31 : m_Free(MAX_QSIZE), m_Ready(MAX_QSIZE), m_pIA(ia), m_uiImages(0)
32 {
33 const BITMAPINFOHEADER& bh = vd->GetDestFmt();
34 IVideoDecoder::CAPS caps = vd->GetCapabilities();
35 uint_t align = 0;
36 if (caps & IVideoDecoder::CAP_ALIGN16)
37 align = 16;
38 if (caps & IVideoDecoder::CAP_ALIGN64)
39 align = 64;
40 //printf("CAPS %d %d %p\n", align, caps, m_pIA);
41 //BitmapInfo(bh).Print();
42
43 if (!m_pIA)
44 m_pIA = this;
45 while (m_Buffers.size() < maxsize)
46 {
47 CImage* ci = m_pIA->ImageAlloc(bh, (uint_t)m_Buffers.size(), align);
48 if (!ci)
49 {
50 if (!m_Buffers.size() && m_pIA)
51 {
52 m_pIA = this; // no direct
53 AVM_WRITE("video reader", "NODIRECT\n");
54 continue;
55 }
56 break;
57 }
58 m_Buffers.push_back(ci);
59 //AVM_WRITE("video reader", "w:%d h:%d bpp:%d 0x%08x - %" PRIsz ":%" PRIsz "\n", bh.biWidth, bh.biHeight, bh.biBitCount, bh.biCompression, m_Buffers.size(), maxsize);
60 }
61 //assert(m_Buffers.size() < m_Free.capacity());
62 //for (unsigned i = 0; i < m_Buffers.size(); i++) m_Free.push(m_Buffers[i]);
63 Flush();
64 //AVM_WRITE("video reader", "VideoQueue: size %d (%p)\n", m_Buffers.size(), ia);
65 }
66 virtual ~VideoQueue()
67 {
68 if (m_pIA)
69 m_pIA->ReleaseImages();
70 for (size_t i = 0; i < m_Buffers.size(); i++)
71 m_Buffers[i]->Release();
72 }
73 void Flush()
74 {
75 m_Free.clear();
76 m_Ready.clear();
77 for (unsigned i = 0; i < m_Buffers.size(); i++)
78 {
79 m_Buffers[i]->Age();
80 m_Buffers[i]->m_lTimestamp = 0;
81 m_Free.push(m_Buffers[i]);
82 }
83 }
84 size_t GetSize() const { return m_Buffers.size(); }
85 size_t GetFreeSize() const { return m_Free.size(); }
86 size_t GetReadySize() const { return m_Ready.size(); }
87
88 CImage* FrontFree() const { return m_Free.size() ? m_Free.front() : 0; }
89 void PopFree() { if (m_Free.size()) m_Free.pop(); }
90 void PushFree(CImage* ci) { if (m_Free.size() < m_Buffers.size()) m_Free.push(ci); }
91
92 CImage* FrontReady() const { return m_Ready.size() ? m_Ready.front() : 0; }
93 void PopReady() { if (m_Ready.size()) m_Ready.pop(); }
94 void PushReady(CImage* ci) { if (m_Ready.size() < m_Buffers.size()) m_Ready.push(ci); }
95
96 // implement ImageAllocator interface
97 virtual size_t GetImages() const { return m_uiImages; }
98 virtual CImage* ImageAlloc(const BITMAPINFOHEADER& bh, uint_t idx, size_t align)
99 {
100 CImage* ci = new CImage((const BitmapInfo*)&bh);
101 if (ci)
102 {
103 m_uiImages++;
104 ci->SetAllocator(this);
105 }
106 return ci;
107 }
108 virtual void ReleaseImages() { m_uiImages = 0; }
109
110 protected:
111 avm::qring<CImage*> m_Free;
112 avm::qring<CImage*> m_Ready;
113 IImageAllocator* m_pIA;
114 avm::vector<CImage*> m_Buffers;
115 size_t m_uiImages;
116 };
117
118
119 ReadStreamV::ReadStreamV(IMediaReadStream* stream)
120 :ReadStream(stream), m_pVideodecoder(0), m_pQueue(0), m_pFrame(0),
121 m_pQueueAllocator(0), m_uiQueueSize(1), flip(0)
122 {
123 Flush();
124 }
125
126 ReadStreamV::~ReadStreamV()
127 {
128 StopStreaming();
129 //printf("XXTTT %f %f %f\n", ttt, pttt, gttt);
130 }
131
132 bool ReadStreamV::Eof() const
133 {
134 if (GetBuffering() > 0)
135 return false;
136 //AVM_WRITE("video reader", "-- %d p:%d l:%d %d\n", ReadStream::Eof(), GetPos(), GetLength(), GetBuffering());
137 return ReadStream::Eof();
138 }
139
140 void ReadStreamV::Flush()
141 {
142 ReadStream::Flush();
143 if (m_pVideodecoder)
144 m_pVideodecoder->Flush();
145
146 if (m_pQueue)
147 m_pQueue->Flush();
148 m_pFrame = 0;
149 Update();
150
151 m_bHadKeyFrame = false;
152 //m_bHadKeyFrame = true;
153 }
154
155 size_t ReadStreamV::GetBuffering(size_t* bufsz) const
156 {
157 size_t bs = m_uiQueueSize;
158 size_t s;
159
160 if (m_pQueue)
161 {
162 s = m_pQueue->GetReadySize();
163 bs = m_pQueue->GetFreeSize() + s;
164 //printf("free:%" PRIsz " sz:%" PRIsz " fil:%" PRIsz "\n", m_pQueue->GetSize(), bs, s);
165 }
166 else
167 s = 0;
168 if (bufsz)
169 *bufsz = bs;
170
171 return s;
172 }
173
174 framepos_t ReadStreamV::GetPos() const
175 {
176 return m_uiVPos;
177 }
178
179 double ReadStreamV::GetTime(framepos_t framep) const
180 {
181 if (framep == ERR)
182 return m_dVTime;
183
184 return m_pStream->GetTime(framep);
185 }
186
187 /******************************************************************
188
189 These functions are meaningless for 'unknown type' stream
190
191 ******************************************************************/
192
193 size_t ReadStreamV::GetVideoFormat(void* format, size_t size) const
194 {
195 if (format)
196 memcpy(format, m_pFormat, (size < m_uiFormatSize) ? size : m_uiFormatSize);
197
198 return m_uiFormatSize;
199 }
200
201 CImage* ReadStreamV::GetFrame(bool readframe)
202 {
203 if (m_pVideodecoder)
204 {
205 //printf("GETGRAME %p %d\n", m_pFrame, readframe);
206 // we decode frame here in direct unbuffered mode
207 // it's not being decoded in separate thread
208 if (!m_pFrame && readframe)
209 ReadFrame();
210 if (m_pFrame)
211 {
212 CImage* ci = m_pFrame;
213 ci->AddRef();
214 m_pQueue->PopReady();
215 //printf("GETFRAME:READY %d f:%d\n", m_pQueue->GetReadySize(),m_pQueue->GetFreeSize());
216 m_pQueue->PushFree(m_pFrame);
217 m_pFrame = m_pQueue->GetReadySize() ? m_pQueue->FrontReady() : 0;
218 //printf("GetFrame new: %p (ready: %d)\n", m_pFrame, m_pQueue->GetReadySize());
219 Update();
220 return ci;
221 }
222 //else { printf("Avifile ERROR\n"); abort(); }
223 }
224 return 0;
225 }
226
227 size_t ReadStreamV::GetFrameSize() const
228 {
229 return (m_pVideodecoder) ? m_pVideodecoder->GetDestFmt().biSizeImage : 0;
230 }
231
232 size_t ReadStreamV::GetOutputFormat(void* format, size_t size) const
233 {
234 if (!m_pVideodecoder)
235 return 0;
236
237 if (format)
238 memcpy(format, &m_pVideodecoder->GetDestFmt(),
239 (size < sizeof(BITMAPINFOHEADER))
240 ? size : sizeof(BITMAPINFOHEADER));
241
242 return sizeof(BITMAPINFOHEADER);
243 }
244
245 int ReadStreamV::ReadDirect(void* buffer, size_t bufsize, size_t samples,
246 size_t& samples_read, size_t& bytes_read,
247 int* flags)
248 {
249 int r = ReadStream::ReadDirect(buffer, bufsize, samples,
250 samples_read, bytes_read, flags);
251 m_pFrame = 0;
252 Update();
253 return r;
254 }
255
256 int ReadStreamV::ReadFrame(bool render)
257 {
258 if (!m_pVideodecoder)
259 return -1;
260
261 CImage* pFrame = 0;
262 int hr = -1;
263 for (;;)
264 {
265 if (!ReadPacket())
266 {
267 // regular Eof usually - we should check if we have reached last pos
268 m_iEof++;
269 return -1;
270 }
271 m_iEof = 0;
272 size_t size = m_pPacket->GetSize();
273 //printf("pkttm %" PRId64 " %d\n", m_pPacket->timestamp, size);
274 if (size > 0)
275 {
276 if (!pFrame)
277 {
278 if (m_pQueue)
279 {
280 //printf("READFRAME F:%" PRIsz " R:%" PRIsz " %p\n", m_pQueue->GetFreeSize(), m_pQueue->GetReadySize(), pFrame);
281 if (!m_pQueue->GetFreeSize())
282 {
283 pFrame = m_pQueue->FrontReady();
284 m_pQueue->PopReady();
285 m_pQueue->PushFree(pFrame);
286 AVM_WRITE("video reader", 0, "???NO FREE FRAME???\n");
287 //abort();
288 }
289 const BITMAPINFOHEADER& bh = m_pVideodecoder->GetDestFmt();
290 pFrame = m_pQueue->FrontFree();
291 if (!pFrame)
292 return -1;
293 if (pFrame->GetFmt()->biCompression != bh.biCompression)
294 {
295 int x = pFrame->Format();
296 //printf("Frame format: 0x%x %.4s 0x%x %.4s\n",
297 //x, (char*)&x, bh.biCompression, (char*)&bh.biCompression);
298 //frame->GetImage()->GetFmt()->Print();
299 //AVM_WRITE("video reader", "FORMAT doesn't match!!!\n");
300 // reinitialize queue
301 delete m_pQueue;
302 m_pQueue = 0;
303 }
304 }
305 if (!m_pQueue)
306 {
307 AVM_WRITE("video reader", 1, "queue create: %" PRIsz " %p\n", m_uiQueueSize, m_pQueueAllocator);
308 m_pFrame = 0;
309 if ((m_pQueue = new VideoQueue(m_pVideodecoder, m_uiQueueSize, m_pQueueAllocator)))
310 pFrame = m_pQueue->FrontFree();
311 }
312 assert(pFrame);
313 }
314 //printf("READFRAME F:%d R:%d %p\n", m_pQueue->GetFreeSize(), m_pQueue->GetReadySize(), pFrame);
315 if (m_pPacket->GetTimestamp() != StreamPacket::NO_TIMESTAMP)
316 {
317 pFrame->m_uiPosition = m_pPacket->GetPos();
318 pFrame->m_lTimestamp = m_pPacket->GetTimestamp();
319 }
320
321 if (avm_is_mmx_state())
322 {
323 m_pPacket->Read(0, m_pPacket->GetSize());
324 AVM_WRITE("AVI Read", "Internal ERROR - avifile left MMX STATE!\n");
325 break;
326 }
327 //uint_t sum = 0; for (uint_t i = 0 ; i < m_pPacket->size; i++) sum += ((unsigned char*) m_pPacket->memory)[i];
328 //printf("Mem %p Size %d Flg %d PACKETSUM %d pos: %d size: %d\n", m_pPacket->memory, m_pPacket->size, m_pPacket->flags, sum, m_pPacket->position, m_pPacket->size);
329 //int s = 0; for (unsigned i = 0; i < lBytes; i++) s += ((unsigned char*)temp_buffer)[i]; printf("SUM %d\n", s);
330 hr = 0;
331 if (!m_bHadKeyFrame && m_pPacket->GetFlags())
332 m_bHadKeyFrame = true;
333
334 CImage* pOut = 0;
335
336 //printf("FRAMEin %p %lld\n", pFrame, m_pPacket->timestamp);
337 if (m_bHadKeyFrame)
338 {
339 if (m_pPacket->GetRead())
340 memset(m_pPacket->GetData() + m_pPacket->GetSize(), 0, 8);
341 hr = m_pVideodecoder->DecodeFrame(pFrame, m_pPacket->GetData(),
342 size, m_pPacket->GetFlags(), render, &pOut);
343 }
344 if (avm_is_mmx_state())
345 {
346 AVM_WRITE("AVI Read", "Warning - codec left MMX STATE!\n");
347 #if ARCH_X86
348 __asm__ __volatile__ ("emms");// ::: "memory" );
349 #endif
350 }
351 m_pPacket->Read(0, (hr <= 0) ? size : hr
352 & ~(IVideoDecoder::NEXT_PICTURE | IVideoDecoder::NO_PICTURE));
353
354 if (hr >= 0)
355 {
356 //printf("PACKET %p %f %f\n", frame, m_pPacket->timestamp, m_dSubBTime);
357 //printf("SETTIME %d %f %f\n", m_uiLastPos, m_dLastTime, frame->GetTime());
358 // even when there is no new picture always
359 // rotate buffers - as it could be used to store
360 // temporal image (i.e. by ffmpeg)
361 if (hr & IVideoDecoder::NEXT_PICTURE)
362 {
363 //printf("GET NEW %f\n", m_pPacket->timestamp);
364 assert(m_pQueue->GetFreeSize());
365 m_pQueue->PopFree();
366 pFrame = 0;
367 //printf("NEXTPIC %d\n", m_pQueue->GetFreeSize());
368 hr &= ~IVideoDecoder::NEXT_PICTURE;
369 }
370 if (hr & IVideoDecoder::NO_PICTURE)
371 {
372 if (pOut)
373 {
374 m_pQueue->PushFree(pOut);
375 printf("???OUTPUSH-NOPICTURE %" PRId64 " FREE: %" PRIsz "\n", pOut->m_lTimestamp, m_pQueue->GetFreeSize());
376 }
377 //printf("NOPICTURE %x\n", hr);
378 continue;
379 }
380 //printf("HAVE PICTURE %p %p\n", pOut, pFrame);
381 if (!pOut)
382 {
383 if (pFrame)
384 {
385 m_pQueue->PopFree();
386 //printf("NOOUTPOP_FRAME %d\n", m_pQueue->GetFreeSize());
387 }
388 }
389 else // pOut
390 pFrame = pOut;
391
392 if (render)
393 {
394 if (!m_pQueue->m_Ready.size() || m_pQueue->m_Ready.back() != pFrame)
395 m_pQueue->PushReady(pFrame);
396 else
397 printf("FFMPEG BUG - repeated insert\n");
398 m_pFrame = m_pQueue->FrontReady();
399 }
400 else
401 m_pQueue->PushFree(pFrame);
402
403 #if 0
404 printf("PACKET %p Frame %p Queue %p\n", m_pPacket, pFrame, m_pQueue);
405 if (pFrame)
406 printf("Packet %d %d 0x%x t:%f p:%d ft:%f f:%d tp:%d p:%p\n",
407 m_pPacket->size, m_pPacket->read, hr,
408 m_pPacket->timestamp / 1000000.0,
409 m_pPacket->position,
410 pFrame->m_lTimestamp / 1000000.0,
411 pFrame->m_iType,
412 m_pQueue->GetFreeSize(), pOut);
413 #endif
414 //printf("FRAMEout %p %f\n", pFrame, pFrame->m_dTimestamp);
415 pFrame = 0;
416 ///printf("SETPF %d %d\n", m_pQueue->GetReadySize(), m_pQueue->GetFreeSize());
417 //printf("SetFrame %p %d %f\n", m_pFrame, m_pFrame->GetPos(), m_pFrame->GetTime());
418 break;
419 }
420 }
421 }
422
423 ReadPacket(); // just to be sure we always try next packet
424 Update();
425
426 return hr;
427 }
428
429 framepos_t ReadStreamV::SeekToKeyFrame(framepos_t pos)
430 {
431 AVM_WRITE("video reader", 1, "SeekToKeyFrame() %d\n", pos);
432
433 framepos_t n = ReadStream::SeekToKeyFrame(pos);
434 if (!(GetFrameFlags(0) & KEYFRAME))
435 n = SeekToPrevKeyFrame();
436
437 return n;
438 }
439
440 double ReadStreamV::SeekTimeToKeyFrame(double timepos)
441 {
442 AVM_WRITE("video reader", 1, "SeekTimeToKeyFrame() %f\n", timepos);
443 double t = ReadStream::SeekTimeToKeyFrame(timepos);
444 if (t >= 0. && !(GetFrameFlags(0) & KEYFRAME))
445 SeekToPrevKeyFrame();
446
447 return GetTime();
448 }
449
450 int ReadStreamV::SetDirection(bool d)
451 {
452 flip = d;
453 return 0;
454 }
455
456 int ReadStreamV::SetBuffering(size_t maxsz, IImageAllocator* ia)
457 {
458 m_pFrame = 0;
459 delete m_pQueue;
460 m_pQueue = 0;
461 m_pQueueAllocator = ia;
462 m_uiQueueSize = maxsz;
463 AVM_WRITE("video reader", 5, "Queue size %" PRIsz "\n", m_uiQueueSize);
464 return 0;
465 }
466
467 int ReadStreamV::SetOutputFormat(void* bi, size_t size)
468 {
469 if (!m_pVideodecoder)
470 return -1;
471 // FIXME
472 AVM_WRITE("video reader", "ReadStreamV::SetOutputFormat: not implemented!\n");
473 return -1; //not implemented
474 }
475
476 int ReadStreamV::StartStreaming(const char* privname)
477 {
478 if (m_pVideodecoder)
479 return 0; // already streaming
480
481 if (!(m_pVideodecoder = CreateDecoderVideo(*(const BITMAPINFOHEADER*)m_pFormat,
482 24, flip, privname)))
483 return -1;
484
485 m_pVideodecoder->Start();
486
487 AVM_WRITE("video reader", 2, "ReadStreamV::StartStreaming() %f\n", m_dLastTime);
488 return 0;
489 }
490
491 int ReadStreamV::StopStreaming()
492 {
493 if (m_pVideodecoder)
494 {
495 AVM_WRITE("video reader", "ReadStreamV::StopStreaming()\n");
496 FreeDecoderVideo(m_pVideodecoder);
497 m_pVideodecoder = 0;
498 m_pFrame = 0;
499 delete m_pQueue;
500 m_pQueue = 0;
501 }
502 return 0;
503 }
504
505 AVM_END_NAMESPACE;