核心参考 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.

以上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 = 0;frame_packet.e_o_s = flush ? 1 : 0;// According to// https://tools.ietf.org/html/draft-ietf-codec-oggopus-14#section-4 the// granule position should include all samples up to the last packet completed// on the page, so we need to update granule_position_ before assigning it to// the packet.  If we're closing the stream, we don't assume that the last// packet includes a full frame.if (flush) {granule_position_ += (elements_in_pcm_frame_ / num_channels_);} else {granule_position_ += frame_size_;}frame_packet.granulepos = granule_position_;frame_packet.packetno = packet_count_;frame_packet.packet = opus_frame_bytes;frame_packet.bytes = opus_bytes_length;// Add the data packet into the stream.packet_count_++;ogg_stream_packetin(&stream_, &frame_packet);

获取输出数据

void OggOpusEncoder::AppendOggStateToBuffer(std::vector<unsigned char>* buffer,bool flush_ogg_stream) {int (*write_fun)(ogg_stream_state*, ogg_page*) =flush_ogg_stream ? &ogg_stream_flush : &ogg_stream_pageout;while (write_fun(&stream_, &page_) != 0) {const int initial_size = buffer->size();buffer->resize(buffer->size() + page_.header_len + page_.body_len);memcpy(buffer->data() + initial_size, page_.header, page_.header_len);memcpy(buffer->data() + initial_size + page_.header_len, page_.body,page_.body_len);}
}

解码相关

基本方法

函数 用途
ogg_sync_pageseek Finds the borders of pages and resynchronizes the stream. -n means that we skipped n bytes within the bitstream.0 means that the page isn’t ready and we need more data, or than an internal error occurred. No bytes have been skipped. n means that the page was synced at the current location, with a page length of n bytes.
ogg_sync_buffer Exposes a buffer from the synchronization layer in order to read data. 返回缓冲区引入,为下一步数据输入做准备
ogg_sync_wrote Tells the synchronization layer how many bytes were written into the buffer. 同步已经写入了多少数据
ogg_stream_pagein Submits a complete page to the stream layer. 把page的信息提交到流处理层
ogg_stream_packetout Outputs a packet to the codec-specific decoding engine. 取出packet信息,可进行原始数据处理

创建与销毁

创建
ogg_sync_state ogsync; ogg_sync_init(&ogsync);
销毁
ogg_sync_clear

遍历处理数据

处理结构

ogg_sync_init(&ogsync);
while(get_next_page(file, &ogsync, &page, &written)) {...p->process_page(p, &page);...}
ogg_sync_clear(&ogsync)

写数据到同步器

while((ret = ogg_sync_pageseek(ogsync, page)) <= 0) {if(ret < 0) {/* unsynced, we jump over bytes to a possible capture - we don't need to read more just yet */oi_warn(_("WARNING: Hole in data (%d bytes) found at approximate offset %" I64FORMAT " bytes. Corrupted Ogg.\n"), -ret, *written);continue;}/* zero return, we didn't have enough data to find a whole page, read */buffer = ogg_sync_buffer(ogsync, CHUNK);bytes = fread(buffer, 1, CHUNK, f);if(bytes <= 0) {ogg_sync_wrote(ogsync, 0);return 0;}ogg_sync_wrote(ogsync, bytes);*written += bytes;
}

文件头处理示例

//初始化流
ogg_stream_init(&stream->os, serial);
//ogg_stream_init
ogg_stream_pagein(&stream->os, page);
res = ogg_stream_packetout(&stream->os, &packet);
if(res <= 0) {oi_warn(_("WARNING: Invalid header page, no packet found\n"));null_start(stream);
}
else if(packet.bytes >= 19 && memcmp(packet.packet, "OpusHead", 8)==0)
.../* re-init, ready for processing */
ogg_stream_clear(&stream->os);
ogg_stream_init(&stream->os, serial);
//此处是为了下次重新解析头

音频数据解析

音频数据的解析根据ogg格式也是每个page进行解析

ogg文件封装格式简介相关推荐

  1. 【Android 安装包优化】7z 文件压缩格式 ( 7z 格式简介 | 7z 命令使用说明 )

    文章目录 一.7z 文件压缩格式简介 二.7z 命令使用说明 1.压缩命令 2.解压命令 三.7z 命令示例 1.配置 7z 命令环境变量 2.压缩 3.解压缩 四.参考资料 一.7z 文件压缩格式简 ...

  2. mkv封装格式+ebml语法

    文章部分内容参考https://www.matroska.org/technical/specs/index.html 1.mkv封装格式简介 Matroska 开源多媒体容器标准.MKV属于其中的一 ...

  3. mp4封装格式各box类型讲解及IBP帧计算

    mp4封装格式各box类型讲解及IBP帧计算 文章目录 mp4封装格式各box类型讲解及IBP帧计算 box ftyp box moov box mvhd box (Movie Header Box) ...

  4. 创建ogg文件 c语言,Ogg音频格式文件的样本构造(CVE-2018-5146)

    原标题:Ogg音频格式文件的样本构造(CVE-2018-5146) *严正声明:本文仅限于技术讨论与分享,严禁用于非法途径 下面的所有分析都是在Firefox 59.0 32位上进行的.由于笔者是刚入 ...

  5. ogg文件怎么转换为mp3格式?

    ogg文件怎么转换为mp3格式?大家好!我专业为大家分享各种办公操作小技巧.今天给大家带来的是ogg文件怎么转换为mp3格式教程,可以说是在各种ogg文件转换为mp3格式的方法中是非常简单的了,即便零 ...

  6. AVI音视频封装格式学习(五)——h265与PCM合成AVI文件

    不知道是处于版权收费问题还是什么原因,H265现在也并没有非常广泛的被普及.将h265数据合成AVI的资料现在在网上也基本上没有.使用格式化工厂工具将h265数据封装成AVI格式,发现它在封装的时候其 ...

  7. 模拟输入H.264流,输出封装格式文件(API版)

    每次从H.264文件读入一定数据量的数据,模拟输入H.264流,最终输出封装格式文件. //H264ToContainer_Win32.h extern "C" {//@param ...

  8. 利用FFmpeg将H.264文件读入内存,再输出封装格式文件

    /***先将H.264文件读入内存,*再输出封装格式文件.*/ #include "stdafx.h"#define __STDC_CONSTANT_MACROSextern &q ...

  9. 模拟输入H.264流,输出封装格式文件

    /***每次从H.264文件读取IO_BUFFER_SIZE字节的数据,*模拟输入H.264流,最终输出封装格式文件.*/ #include "stdafx.h"#define _ ...

  10. 手机wem文件转换软件_wem文件如何播放和转换成Ogg或MP3格式?

    以下假定用户是在Windows下操作. 一.判断是否为可播放文件:file命令 如果可以确定是音频文件,可省略此步骤. 安装并打开Cygwin,使用file程序可以看到如下信息: Administra ...

最新文章

  1. Scikit-Learn大变化:合并Pandas
  2. 我第一次接私活,就被骗了···
  3. 漫说模板方法模式---学生时代的烦恼
  4. d3.js 实现烟花鲜果
  5. python新闻评论分析_从新闻文章中提取评论
  6. 画一个空心圆_圆形在PPT中的6大妙用,每一个都能瞬间提升PPT的逼格!
  7. python语言编写一个生成九宫格图片的代码_python生成九宫格图片
  8. 单系统 台电x80pro_台电X80HD安装Win8单系统教程
  9. RestTemplate.exchange各种用法(包括泛型等 --全)
  10. 融入动画技术的交互应用——简单弹幕游戏
  11. 因数分解——Pollard' p-1 Pollard rho
  12. 用python画篮球场_如何使用 Python 创建一个 NBA 得分图?
  13. quartz原理 java_Quartz原理解密
  14. java single threaded_[Java多线程设计模式]读书笔记 - 第一章 Single Threaded Execution
  15. HTML+js图片验证码编写
  16. APS系统如何让企业实现“多赢”?看高博通信是怎么做的
  17. 风口上的“低代码”:是技术变革?还是另一个风险敞口?
  18. 程序员应该多久跳槽一次?为何贵圈跳槽如此频繁?
  19. Calculate a+b
  20. vmware 网络不可达

热门文章

  1. 如何用WPS表格制作甘特图?WPS表格制作甘特图详解!
  2. 软件测试教学实训平台
  3. 一文搞懂 | Linux 同步管理(上)
  4. 数据结构课设 (快餐店 POS 机计费系统、成绩分析、算术表达式)
  5. 微信小程序中生成二维码工具以及扫一扫
  6. 管理的两大核心,工作目标、人的价值
  7. 计算机语言栏在哪里,电脑里的输入法不见了,去哪儿找
  8. 自定义异常 extends Exception
  9. python numpy逆_python-使用numpy的矩阵逆
  10. 每天学点Vue,学习笔记---DAY4