/*
* 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. 2004. All Rights Reserved.
*
* Contributor(s):
* Bill May wmay@cisco.com
*/
/*
* mpeg2ps.c - parse program stream and vob files
*/
#include "mpeg2_ps.h"
#include "mpeg2ps_private.h"
#include <mp4av.h>
#include <mp4av_h264.h>
//#define DEBUG_LOC 1
//#define DEBUG_STATE 1
static const uint lpcm_freq_tab[4] = {48000, 96000, 44100, 32000};
/*************************************************************************
* File access routines. Could all be inlined
*************************************************************************/
static FDTYPE file_open (const char *name)
{
return open(name, OPEN_RDONLY);
}
static bool file_okay (FDTYPE fd)
{
return fd >= 0;
}
static void file_close (FDTYPE fd)
{
close(fd);
}
static bool file_read_bytes (FDTYPE fd,
uint8_t *buffer,
uint32_t len)
{
uint32_t readval = read(fd, buffer, len);
return readval == len;
}
// note: len could be negative.
static void file_skip_bytes (FDTYPE fd, int32_t len)
{
lseek(fd, len, SEEK_CUR);
}
static off_t file_location (FDTYPE fd)
{
return lseek(fd, 0, SEEK_CUR);
}
static off_t file_seek_to (FDTYPE fd, off_t loc)
{
return lseek(fd, loc, SEEK_SET);
}
static off_t file_size (FDTYPE fd)
{
off_t ret = lseek(fd, 0, SEEK_END);
file_seek_to(fd, 0);
return ret;
}
static uint64_t read_pts (uint8_t *pak)
{
uint64_t pts;
uint16_t temp;
pts = ((pak[0] >> 1) & 0x7);
pts <<= 15;
temp = convert16(&pak[1]) >> 1;
pts |= temp;
pts <<= 15;
temp = convert16(&pak[3]) >> 1;
pts |= temp;
return pts;
}
static mpeg2ps_stream_t *mpeg2ps_stream_create (uint8_t stream_id,
uint8_t substream)
{
mpeg2ps_stream_t *ptr = MALLOC_STRUCTURE(mpeg2ps_stream_t);
memset(ptr, 0, sizeof(*ptr));
ptr->m_stream_id = stream_id;
ptr->m_substream_id = substream;
ptr->is_video = stream_id >= 0xe0;
ptr->pes_buffer = (uint8_t *)malloc(4*4096);
ptr->pes_buffer_size_max = 4 * 4096;
return ptr;
}
static void mpeg2ps_stream_destroy (mpeg2ps_stream_t *sptr)
{
mpeg2ps_record_pes_t *p;
while (sptr->record_first != NULL) {
p = sptr->record_first;
sptr->record_first = p->next_rec;
free(p);
}
if (sptr->m_fd != FDNULL) {
file_close(sptr->m_fd);
sptr->m_fd = FDNULL;
}
CHECK_AND_FREE(sptr->pes_buffer);
free(sptr);
}
/*
* adv_past_pack_hdr - read the pack header, advance past it
* we don't do anything with the data
*/
static void adv_past_pack_hdr (FDTYPE fd,
uint8_t *pak,
uint32_t read_from_start)
{
uint8_t stuffed;
uint8_t readbyte;
uint8_t val;
if (read_from_start < 5) {
file_skip_bytes(fd, 5 - read_from_start);
file_read_bytes(fd, &readbyte, 1);
val = readbyte;
} else {
val = pak[4];
}
// we've read 6 bytes
if ((val & 0xc0) != 0x40) {
// mpeg1
file_skip_bytes(fd, 12 - read_from_start); // skip 6 more bytes
return;
}
file_skip_bytes(fd, 13 - read_from_start);
file_read_bytes(fd, &readbyte, 1);
stuffed = readbyte & 0x7;
file_skip_bytes(fd, stuffed);
}
/*
* find_pack_start
* look for the pack start code in the file - read 512 bytes at a time,
* searching for that code.
* Note: we may also be okay looking for >= 00 00 01 bb
*/
static bool find_pack_start (FDTYPE fd,
uint8_t *saved,
uint32_t len)
{
uint8_t buffer[512];
uint32_t buffer_on = 0, new_offset, scode;
memcpy(buffer, saved, len);
if (file_read_bytes(fd, buffer + len, sizeof(buffer) - len) == FALSE) {
return FALSE;
}
while (1) {
if (MP4AV_Mpeg3FindNextStart(buffer + buffer_on,
sizeof(buffer) - buffer_on,
&new_offset,
&scode) >= 0) {
buffer_on += new_offset;
if (scode == MPEG2_PS_PACKSTART) {
file_skip_bytes(fd, buffer_on - 512); // go back to header
return TRUE;
}
buffer_on += 1;
} else {
len = 0;
if (buffer[sizeof(buffer) - 3] == 0 &&
buffer[sizeof(buffer) - 2] == 0 &&
buffer[sizeof(buffer) - 1] == 1) {
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 1;
len = 3;
} else if (*(uint16_t *)(buffer + sizeof(buffer) - 2) == 0) {
buffer[0] = 0;
buffer[1] = 0;
len = 2;
} else if (buffer[sizeof(buffer) - 1] == 0) {
buffer[0] = 0;
len = 1;
}
if (file_read_bytes(fd, buffer + len, sizeof(buffer) - len) == FALSE) {
return FALSE;
}
buffer_on = 0;
}
}
return FALSE;
}
/*
* copy_bytes_to_pes_buffer - read pes_len bytes into the buffer,
* adjusting it if we need it
*/
static void copy_bytes_to_pes_buffer (mpeg2ps_stream_t *sptr,
uint16_t pes_len)
{
uint32_t to_move;
if (sptr->pes_buffer_size + pes_len > sptr->pes_buffer_size_max) {
// if no room in the buffer, we'll move it - otherwise, just fill
// note - we might want a better strategy about moving the buffer -
// right now, we might be moving a number of bytes if we have a large
// followed by large frame.
to_move = sptr->pes_buffer_size - sptr->pes_buffer_on;
memmove(sptr->pes_buffer,
sptr->pes_buffer + sptr->pes_buffer_on,
to_move);
sptr->pes_buffer_size = to_move;
sptr->pes_buffer_on = 0;
//printf("moving %d bytes\n", to_move);
if (to_move + pes_len > sptr->pes_buffer_size_max) {
sptr->pes_buffer = (uint8_t *)realloc(sptr->pes_buffer,
to_move + pes_len + 2048);
sptr->pes_buffer_size_max = to_move + pes_len + 2048;
}
}
file_read_bytes(sptr->m_fd, sptr->pes_buffer + sptr->pes_buffer_size, pes_len);
sptr->pes_buffer_size += pes_len;
#if 0
printf("copying %u bytes - on %u size %u\n",
pes_len, sptr->pes_buffer_on, sptr->pes_buffer_size);
#endif
}
/*
* read_to_next_pes_header - read the file, look for the next valid
* pes header. We will skip over PACK headers, but not over any of the
* headers listed in 13818-1, table 2-18 - basically, anything with the
* 00 00 01 and the next byte > 0xbb.
* We return the pes len to read, and the "next byte"
*/
static bool read_to_next_pes_header (FDTYPE fd,
uint8_t *stream_id,
uint16_t *pes_len)
{
uint32_t hdr;
uint8_t local[6];
while (1) {
// read the pes header
if (file_read_bytes(fd, local, 6) == FALSE) {
return FALSE;
}
hdr = convert32(local);
// if we're not a 00 00 01, read until we get the next pack start
// we might want to also read until next PES - look into that.
if (((hdr & MPEG2_PS_START_MASK) != MPEG2_PS_START) ||
(hdr < MPEG2_PS_END)) {
if (find_pack_start(fd, local, 6) == FALSE) {
return FALSE;
}
continue;
}
if (hdr == MPEG2_PS_PACKSTART) {
// pack start code - we can skip down
adv_past_pack_hdr(fd, local, 6);
continue;
}
if (hdr == MPEG2_PS_END) {
file_skip_bytes(fd, -2);
continue;
}
// we should have a valid stream and pes_len here...
*stream_id = hdr & 0xff;
*pes_len = convert16(local + 4);
#if 0
printf("loc: "X64" %x %x len %u\n", file_location(fd) - 6,
local[3],
*stream_id,
*pes_len);
#endif
return TRUE;
}
return FALSE;
}
/*
* read_pes_header_data
* this should read past the pes header for the audio and video streams
* it will store the timestamps if it reads them
*/
static bool read_pes_header_data (FDTYPE fd,
uint16_t orig_pes_len,
uint16_t *pes_left,
b
没有合适的资源?快使用搜索试试~ 我知道了~
mp4creator-src-1.6.1d
4星 · 超过85%的资源 需积分: 13 39 下载量 54 浏览量
2013-03-20
20:57:33
上传
评论
收藏 1.64MB GZ 举报
温馨提示
共319个文件
cpp:118个
c:66个
h:52个
mp4creator-src-1.6.1d源代码,包括了多个mp4lib库源代码;可实现H264,aac等音视频ES流的MP4打包及二次开发。
资源推荐
资源详情
资源评论
收起资源包目录
mp4creator-src-1.6.1d (319个子文件)
Makefile.am 2KB
Makefile.am 1KB
Makefile.am 1021B
Makefile.am 808B
Makefile.am 779B
Makefile.am 776B
Makefile.am 743B
Makefile.am 548B
Makefile.am 335B
Makefile.am 267B
Makefile.am 219B
Makefile.am 196B
Makefile.am 149B
Makefile.am 136B
Makefile.am 82B
API_CHANGES 4KB
AUTHORS 2KB
bootstrap 3KB
mpeg2ps.c 60KB
quicktime.c 41KB
avilib.c 32KB
ismacryplib.c 31KB
getopt.c 28KB
trak.c 15KB
stsdtable.c 12KB
rtphint.c 11KB
util.c 10KB
esds.c 5KB
minf.c 5KB
stbl.c 5KB
tkhd.c 5KB
hinf.c 4KB
mvhd.c 4KB
udta.c 4KB
stsc.c 4KB
mpeg2ps_util.c 4KB
ctts.c 4KB
hdlr.c 4KB
stts.c 3KB
dref.c 3KB
stsz.c 3KB
moov.c 3KB
atom.c 3KB
stsd.c 3KB
iods.c 3KB
getopt1.c 3KB
mdia.c 3KB
elst.c 3KB
mdhd.c 3KB
hintudta.c 2KB
hmhd.c 2KB
rtp.c 2KB
stco.c 2KB
sdp.c 2KB
stss.c 2KB
hint.c 2KB
tref.c 2KB
hinthnti.c 2KB
hnti.c 2KB
payt.c 2KB
gmin.c 2KB
maxr.c 2KB
c_api.c 2KB
vmhd.c 2KB
ctab.c 1KB
tmax.c 1KB
tmin.c 1KB
nump.c 1KB
tpyl.c 1KB
dmax.c 1KB
dimm.c 1KB
drep.c 1KB
dmed.c 1KB
trpy.c 1KB
pmax.c 1KB
strcasestr.c 1KB
mdat.c 1KB
smhd.c 1KB
edts.c 1KB
dinf.c 1KB
gmhd.c 971B
matrix.c 776B
strsep.c 387B
need_for_win32.c 207B
COPYING 29KB
mp4file.cpp 103KB
mp4.cpp 96KB
mp4drm.cpp 67KB
mp4track.cpp 48KB
mp4creator.cpp 47KB
rfccrypto.cpp 45KB
rtphint.cpp 33KB
mp4meta.cpp 25KB
isma.cpp 24KB
mp4atom.cpp 24KB
mp4property.cpp 21KB
mp4v.cpp 20KB
h264.cpp 20KB
h263.cpp 19KB
mp4info.cpp 19KB
共 319 条
- 1
- 2
- 3
- 4
旋转牧码
- 粉丝: 1
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- unity开发教程.docx
- 代码使用Pygame库实现了一个简单的烟花模拟 核心逻辑包括烟花和粒子类的定义,处理位置、爆炸、尾迹和绘制等操作
- Matlab Simulink 电力电子仿真-Flyback(反激电路)电路分析
- tudou-android-release.apk
- 数据分析教程.docx
- 基于matlab实现用有限元法计算电磁场的Matlab工具 .rar
- 基于matlab实现有限元算法 计算电磁场问题 边界条件包括第一类边界和第二类边界.rar
- 基于matlab实现用于计算不同车重下的电动汽车动力性和经济性.rar
- 基于matlab实现遗传算法求解多车场车辆路径问题 有多组算例可以用.rar
- 浏览器.apk
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页