【VC开源代码栏目提醒】:网学会员VC开源代码为您提供mpeg4_file.cpp参考,解决您在mpeg4_file.cpp学习中工作中的难题,参考学习。
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is MPEG4IP.
*
* The Initial Developer of the Original Code is Cisco Systems Inc.
* Portions created by Cisco Systems Inc. are
* Copyright (C) Cisco Systems Inc. 2000, 2001. All Rights Reserved.
*
* Contributor(s):
* Bill May wmay@cisco.com
*/
/*
* mpeg4_file.cpp
* Create media structures for session for an mp4v file (raw
mpeg4)
*/
#include "mpeg4.h"
#include "type/typeapi.h"
#include "sys/mode.hpp"
#include "sys/vopses.hpp"
//#include "sys/tps_enhcbuf.hpp"
//#include "sys/decoder/enhcbufdec.hpp"
#include "tools/entropy/bitstrm.hpp"
#include "sys/decoder/vopsedec.hpp"
#define logit divx->m_vft->log_msg
/*
* divx_find_header
* Search buffer to find the next start code
*/
static int divx_find_header (iso_decode_t *divx,
uint32_t start_offset)
{
for (uint32_t ix = start_offset; ix + 4 < divx->m_buffer_size; ix++) {
if ((divx->m_buffer[ix] == 0) &&
(divx->m_buffer[ix + 1] == 0) &&
(divx->m_buffer[ix + 2] == 1)) {
return ix;
}
}
return -1;
}
/*
* divx_reset_buffer
* Move end of buffer to beginning, fill in rest of buffer
*/
static int divx_reset_buffer (iso_decode_t *divx)
{
uint32_t diff, read;
if (divx->m_buffer_size > divx->m_buffer_on) {
diff = divx->m_buffer_size - divx->m_buffer_on;
memmove(divx->m_buffer,
&divx->m_buffer[divx->m_buffer_on],
diff);
} else diff = 0;
divx->m_buffer_size = diff;
read = fread(divx->m_buffer + diff,
1,
divx->m_buffer_size_max - diff,
divx->m_ifile);
divx->m_buffer_on = 0;
if (read <= 0) {
if (divx->m_buffer_size < 4) divx->m_buffer_size = 0;
return -1;
}
divx->m_buffer_size += read;
if (divx->m_buffer_size < 4) {
divx->m_buffer_size = 0;
return -1;
}
return diff;
}
/*
* divx_buffer_load
* Make sure we have at least 1 full VOP frame in the buffer
*/
static int divx_buffer_load (iso_decode_t *divx, uint8_t *ftype)
{
int next_hdr, left, value;
if (divx->m_buffer_on + 3 >= divx->m_buffer_size) {
if (divx_reset_buffer(divx) < 0) return -1;
}
next_hdr = divx_find_header(divx, divx->m_buffer_on);
if (next_hdr < 0) return -1;
// We've got the first header pointed to by m_buffer_on
divx->m_buffer_on = next_hdr;
// Is it a VOP header ? If not, find the first VOP header
if (divx->m_buffer[next_hdr + 3] != 0xb6) {
value = 0;
do {
// Increment when we've got a header pointed to by next_hdr
if (value >= 0) next_hdr += 4;
value = divx_find_header(divx, next_hdr);
if (value < 0) {
if (divx->m_buffer_on == 0 &&
divx->m_buffer_size == divx->m_buffer_size_max) {
// weirdness has happened. We've got a full buffer of
// headers, no frames
return -1;
}
left = divx_reset_buffer(divx);
if (left < 0) {
// No more new data - we've reached the end...
return divx->m_buffer_size;
}
// okay - this case is gross - we'll start checking from the
// beginning
next_hdr = left - 4;
} else {
next_hdr = value;
}
} while (value < 0 || divx->m_buffer[next_hdr + 3] != 0xb6);
}
// next_hdr contains the location of the first VOP.
// Record the file type (top 2 bits) after 00 00 01 b6
*ftype = divx->m_buffer[next_hdr + 4];
// Find the next header.
value = divx_find_header(divx, next_hdr + 4);
if (value >= 0) {
// Cool - it's in the header...
return value;
}
//
// Not in the header - reset the buffer, then continue search
//
value = divx->m_buffer_size - divx->m_buffer_on;
left = divx_reset_buffer(divx);
if (left < 0) return divx->m_buffer_size;
value = divx_find_header(divx, value);
if (value >= 0) {
return value;
}
// We don't have enough read in... Go forward
while (divx->m_buffer_size_max < 65535) {
int old, readsize;
divx->m_buffer = (uint8_t *)realloc(divx->m_buffer,
divx->m_buffer_size_max + 1024);
readsize = fread(&divx->m_buffer[divx->m_buffer_size_max],
1,
1024,
divx->m_ifile);
if (readsize <= 0) {
return (divx->m_buffer_size - divx->m_buffer_on);
}
old = divx->m_buffer_size;
divx->m_buffer_size += readsize;
divx->m_buffer_size_max += 1024;
value = divx_find_header(divx, old - 4);
if (value >= 0) return value;
}
return -1;
}
/*
* divx_file_check
* Check the file. If it's a .divx file, look for the V