| 1 |
#include "AviWrite.h" |
| 2 |
#include "avm_creators.h" |
| 3 |
#include "videoencoder.h" |
| 4 |
#include <string.h> |
| 5 |
#include <stdio.h> |
| 6 |
|
| 7 |
AVM_BEGIN_NAMESPACE; |
| 8 |
|
| 9 |
#define __MODULE__ "VideoWrite" |
| 10 |
|
| 11 |
AviVideoWriteStream::AviVideoWriteStream(AviWriteFile* file, int ckid, |
| 12 |
const CodecInfo& ci, |
| 13 |
const BITMAPINFOHEADER* src_header, |
| 14 |
int frame_rate, int flags) |
| 15 |
|
| 16 |
:AviWriteStream(file, ckid, Video, ci.fourcc, frame_rate, flags), |
| 17 |
m_vstatus(0), m_pBuffer(0) |
| 18 |
{ |
| 19 |
m_pVideoEnc = CreateEncoderVideo(ci, *src_header); |
| 20 |
if (!m_pVideoEnc) |
| 21 |
throw FATAL("Could not create encoder"); |
| 22 |
AviWriteStream::m_stop = 1; |
| 23 |
} |
| 24 |
|
| 25 |
AviVideoWriteStream::~AviVideoWriteStream() |
| 26 |
{ |
| 27 |
//AVM_WRITEI("AVI writer", "AviAudioWriteStream::~AviAudioWriteStream()\n"); fflush(stdout); |
| 28 |
Stop(); |
| 29 |
FreeEncoderVideo(m_pVideoEnc); |
| 30 |
} |
| 31 |
|
| 32 |
int AviVideoWriteStream::AddFrame(CImage* chunk, size_t* pSize, int* pKeyframe, char** ppData) |
| 33 |
{ |
| 34 |
if (m_vstatus != 1) |
| 35 |
{ |
| 36 |
if (pSize) |
| 37 |
*pSize = 0; |
| 38 |
return -1; |
| 39 |
} |
| 40 |
|
| 41 |
size_t size = 0; |
| 42 |
int is_keyframe = 0; |
| 43 |
int hr; |
| 44 |
if (chunk) |
| 45 |
{ |
| 46 |
hr = m_pVideoEnc->EncodeFrame(chunk, m_pBuffer, &is_keyframe, &size); |
| 47 |
if (hr == 0) |
| 48 |
{ |
| 49 |
hr = AviWriteStream::AddChunk(m_pBuffer, size, is_keyframe); |
| 50 |
} |
| 51 |
else |
| 52 |
{ |
| 53 |
size = 0; |
| 54 |
is_keyframe = 0; |
| 55 |
} |
| 56 |
} |
| 57 |
else |
| 58 |
hr = AviWriteStream::AddChunk(NULL, 0); |
| 59 |
|
| 60 |
if (pSize) |
| 61 |
*pSize = size; |
| 62 |
if (pKeyframe) |
| 63 |
*pKeyframe = (is_keyframe != 0); |
| 64 |
if (ppData) |
| 65 |
*ppData = m_pBuffer; |
| 66 |
return hr; |
| 67 |
} |
| 68 |
|
| 69 |
const CodecInfo& AviVideoWriteStream::GetCodecInfo() const |
| 70 |
{ |
| 71 |
return m_pVideoEnc->GetCodecInfo(); |
| 72 |
} |
| 73 |
|
| 74 |
int AviVideoWriteStream::Start() |
| 75 |
{ |
| 76 |
if (m_vstatus) |
| 77 |
return -1; |
| 78 |
const BITMAPINFOHEADER& bh = m_pVideoEnc->GetOutputFormat(); |
| 79 |
m_uiFormatSize = bh.biSize; |
| 80 |
delete[] m_pcFormat; |
| 81 |
m_pcFormat = new char[bh.biSize]; |
| 82 |
if (m_pcFormat) |
| 83 |
{ |
| 84 |
memcpy(m_pcFormat, &bh, bh.biSize); |
| 85 |
m_pVideoEnc->Start(); |
| 86 |
m_pBuffer = new char[m_pVideoEnc->GetOutputSize()]; |
| 87 |
SetVideoHeader(100, |
| 88 |
//m_pVideoEnc->GetQuality(), |
| 89 |
bh.biWidth, bh.biHeight); |
| 90 |
m_vstatus = 1; |
| 91 |
return 0; |
| 92 |
} |
| 93 |
return -1; |
| 94 |
} |
| 95 |
|
| 96 |
int AviVideoWriteStream::Stop() |
| 97 |
{ |
| 98 |
if (!m_vstatus) |
| 99 |
return -1; |
| 100 |
m_pVideoEnc->Stop(); |
| 101 |
delete[] m_pBuffer; |
| 102 |
m_pBuffer = 0; |
| 103 |
m_vstatus = 0; |
| 104 |
return 0; |
| 105 |
} |
| 106 |
|
| 107 |
size_t AviVideoWriteStream::GetLength() const |
| 108 |
{ |
| 109 |
return AviWriteStream::GetLength(); |
| 110 |
} |
| 111 |
|
| 112 |
#undef __MODULE__ |
| 113 |
|
| 114 |
AVM_END_NAMESPACE; |