ogg文件封装格式简介

本文介绍了Ogg文件的数据结构,重点讲解了编码和解码过程。在编码方面,涉及ogg_packet和ogg_stream_state的使用,数据通过ogg_stream_packetin写入,并通过ogg_stream_pageout输出到ogg_page。解码部分则涵盖了从创建到销毁的基本方法,以及音频数据的解析,强调了对每个ogg_page的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

核心参考 xiph官网

ogg 数据结构

datatype purpose
ogg_page This structure encapsulates data into one ogg bitstream page. 编码时page的信息在此输出
ogg_stream_state This structure contains current encode/decode data for a logical bitstream. 代表当前流
ogg_packet This structure encapsulates the data and metadata for a single Ogg packet. 编码时数据输入的结构包
ogg_sync_state Contains bitstream synchronization information.

image

以上ogg中最大的结构为ogg流,对比ogg的存储结构来说都是以ogg_page来进行数据分页,但是对于用于来说是以ogg_packet来对数据进行装载和解析。

编码相关

概览

When encoding, the encoding engine will output raw packets which must be placed into an Ogg bitstream. Raw packets are inserted into the stream, and an ogg_page is output when enough packets have been written to create a full page. The pages output are pointers to buffered packet segments, and can then be written out and saved as an ogg stream. There are a couple of basic steps: Use the encoding engine to produce a raw packet of data. Call ogg_stream_packetin to submit a raw packet to the stream. Use ogg_stream_pageout to output a page, if enough data has been submitted. Otherwise, continue submitting data.
意思就是原始数据要封装在ogg_packet中通过ogg_stream_packetin方法写入到ogg_stream_state中,如果包足够通过ogg_stream_pageout方法将数据写在ogg_page中

function purpose
ogg_stream_packetin Submits a raw packet to the streaming layer, so that it can be formed into a page.
ogg_stream_iovecin iovec version of ogg_stream_packetin() above.
ogg_stream_pageout Outputs a completed page if the stream contains enough packets to form a full page. 返回值非0代码输出成功
ogg_stream_pageout_fill Similar to ogg_stream_pageout(), but specifies a page spill threshold in bytes.
ogg_stream_flush Forces any remaining packets in the stream to be returned as a page of any size.
ogg_stream_flush_fill Similar to ogg_stream_flush(), but specifies a page spill threshold in bytes.

创建与销毁

创建
ogg_stream_init(&stream_, 0 /* serial number */);
销毁
ogg_stream_clear(&stream_);

编码数据封装

typedef struct {
   
  unsigned char *packet;//数据
  long  bytes;//数据大小
  long  b_o_s;//1代表开始包
  long  e_o_s;//1代表结束包
  //A number indicating the position of this packet in the decoded data. This is the last sample, frame or other unit of information ('granule') that can be completely decoded from this packet.
  ogg_int64_t  granulepos;
  ogg_int64_t  packetno;//Sequential number of this packet in the ogg bitstream.

} ogg_packet;

单流数据写入

// Write the most recent buffer of Opus data into an Ogg packet.
  ogg_packet frame_packet;
  frame_packet.b_o_s 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值