czanyou

2011年9月9日 #

如何在 iPhone 下面生成 H.264 TS 流实现 HTTP live streaming 开发记录 (1)


待整理...

posted @ 2011-09-09 10:40 暗黑长老 阅读(1215) | 评论 (1)编辑 收藏

如何在 iPhone 下面用 OpenGL ES 显示视频开发记录 (3)


通过 OpenGL ES 显示视频的方法:
(注意, 这里需要用到 <<iPhone 开发基础教程>> 12 章中的示例代码中的两个类: OpenGLES2DView, Texture2D, 具体请参考相关的资料)

由于时间关系, 记录比较简单 (可通过 anyou@qq.com 联系我) 


 1 - (void) draw {
 2     
 3     if (fBitmapBuffer == nil) {
 4         return;
 5     }
 6     
 7     glLoadIdentity();
 8     glClearColor(0.0f0.0f0.0f1.0f);
 9     glClear(GL_COLOR_BUFFER_BIT);
10     
11     if (self.sprite) {
12         [self.sprite release];
13         self.sprite = nil;
14     }
15     
16     char* bitmap = [self getBitmapBuffer];
17     
18     if (self.sprite == nil) {
19         CGSize size = CGSizeMake(self.width, self.height);
20         self.sprite = [[Texture2D alloc] initWithData:bitmap 
21                 pixelFormat:kTexture2DPixelFormat_RGBA8888 
22                 pixelsWide:512 pixelsHigh:512 contentSize:size];
23     }
24 
25     if (self.sprite) {
26         glEnable(GL_TEXTURE_2D);
27         int width  = self.bounds.size.width;
28         //int height = self.bounds.size.height;
29         if (width <= 320) {
30             //[self.sprite drawAtPoint:CGPointMake(170, 224)];
31             [self.sprite drawInRect:CGRectMake(00320240)];
32             
33         } else {
34             [self.sprite drawInRect:CGRectMake(00320240)];
35         }
36         
37     }
38     
39     glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
40     [context presentRenderbuffer:GL_RENDERBUFFER_OES];
41 }












posted @ 2011-09-09 10:38 暗黑长老 阅读(1259) | 评论 (0)编辑 收藏

如何在 iPhone 下面用 ffmpeg 解码 H.264 流 (2)

     摘要: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->  下面是如果解码以及将  YV12 数据转换成 32 位 ARGB 数据的代码  1   2 #include ...  阅读全文

posted @ 2011-09-09 10:31 暗黑长老 阅读(3073) | 评论 (2)编辑 收藏

如何在 iPhone 下面用 ffmpeg 播放 H.264 流开发记录 (1)

下载最新的 ffmpeg, 我下的是 0.8.2
下载最新的 gas-preprocessor 脚本 (用来在 MAC OS X 下编译汇编代码的):
https://github.com/yuvi/gas-preprocessor
请将 gas-preprocessor.pl 脚本安装在 /usr/sbin 或者其他执行文件目录下, 并添加执行权限
分别使用下面的配置进行编译:
IOS4.3 armv7 版本 (iPhone 3Gs, iPhone4):
./configure --disable-doc --disable-ffmpeg --disable-ffplay --disable-ffserver --disable-avdevice --disable-swscale --disable-avfilter --disable-avformat --disable-debug --disable-encoders --enable-cross-compile --disable-decoders --disable-armv5te --enable-decoder=h264 --enable-pic --arch=arm --target-os=darwin --cc=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc --as='gas-preprocessor/gas-preprocessor.pl /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc' --sysroot=/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk --cpu=cortex-a8 --extra-cflags='-arch armv7' --extra-ldflags='-arch armv7 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk'
IOS4.3 armv6 版本 (iPhone 3G):
./configure --disable-doc --disable-ffmpeg --disable-ffplay --disable-ffserver --disable-avdevice --disable-swscale --disable-avfilter --disable-avformat --disable-debug --disable-encoders --enable-cross-compile --disable-decoders --disable-armv5te --enable-decoder=h264 --enable-pic --arch=arm --target-os=darwin --cc=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc --as='gas-preprocessor/gas-preprocessor.pl /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc' --sysroot=/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk --cpu=arm1176jzf-s --extra-cflags='-arch armv6' --extra-ldflags='-arch armv6 -isysroot /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk'
IOS4.3 i386 版本 (用于模拟器):
./configure --disable-doc --disable-ffmpeg --disable-ffplay --disable-ffserver --disable-avdevice --disable-swscale --disable-avfilter --disable-avformat --disable-debug --disable-encoders --enable-cross-compile --disable-decoders --disable-armv5te --enable-decoder=h264 --enable-pic --cc=/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc --as='gas-preprocessor/gas-preprocessor.pl /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc' --extra-ldflags=-L/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/usr/lib/system --sysroot=/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk --target-os=darwin --arch=i386 --cpu=i386 --extra-cflags='-arch i386' --extra-ldflags='-arch i386'
将上述分别编译出来的库, 打包成一个统一的版本的方法:
lipo -create -arch armv6 armv6/libavcodec.a -arch armv7 armv7/libavcodec.a -arch i386 i386/libavcodec.a -output libavcodec.a
lipo -create -arch armv6 armv6/libavutil.a -arch armv7 armv7/libavutil.a -arch i386 i386/libavutil.a -output libavutil.a
添加 libavcodec.a, libavutil.a, libavcodec/avcodec.h, 以及 libavutil 下所有头文件到工程中 (请注意路径).
解码的方法 (代码片断如下, 完整代码请参考相关附件):
 1 struct AVCodec *fCodec = NULL;              // Codec
 2 struct AVCodecContext *fCodecContext = NULL;          // Codec Context
 3 struct AVFrame *fVideoFrame = NULL;          // Frame    
 4 
 5 int fDisplayWidth    = 0;
 6 int fDisplayHeight    = 0;
 7 
 8  
 9 
10 int avcodec_decode_video(AVCodecContext *avctx, AVFrame *picture,
11                          int *got_picture_ptr,
12                          const uint8_t *buf, int buf_size)
13 {
14     AVPacket avpkt;
15     av_init_packet(&avpkt);
16     avpkt.data = buf;
17     avpkt.size = buf_size;
18     // HACK for CorePNG to decode as normal PNG by default
19     avpkt.flags = AV_PKT_FLAG_KEY;
20     return avcodec_decode_video2(avctx, picture, got_picture_ptr, &avpkt);
21 }
22 
23  
24 
25 int avc_decode_init(int width, int height)
26 {
27     if (fCodecContext != NULL) {
28         return 0;
29     }
30     avcodec_init();      
31     avcodec_register_all();
32     fCodec = avcodec_find_decoder(CODEC_ID_H264);
33     
34     fDisplayWidth  = width;
35     fDisplayHeight = height;
36 
37     CreateYUVTable();    
38     
39     fCodecContext = avcodec_alloc_context(); 
40     avcodec_open(fCodecContext, fCodec); 
41     fVideoFrame  = avcodec_alloc_frame();
42         
43     return 1;
44 }
45 
46  
47 
48 int avc_decode_release()
49 {
50     if (fCodecContext) {
51         avcodec_close(fCodecContext);
52         free(fCodecContext->priv_data);
53         free(fCodecContext);
54         fCodecContext = NULL;
55     }
56 
57     if (fVideoFrame) {
58         free(fVideoFrame);
59         fVideoFrame = NULL;
60     }
61 
62     DeleteYUVTable();
63     return 1;
64 }
65 
66  
67 
68 int avc_decode(char* buf, int nalLen, char* out)
69 {
70     byte_t* data = (byte_t*)buf;
71     int frameSize = 0;
72     
73     int ret = avcodec_decode_video(fCodecContext, fVideoFrame, &frameSize, data, nalLen); 
74     if (ret <= 0) {
75         return ret;
76     }
77     
78     int width  = fCodecContext->width;
79     int height = fCodecContext->height;
80     DisplayYUV_32((uint32_t*)out, width, height, fDisplayWidth);
81     return ret;    
82 }
83 
84  

posted @ 2011-09-09 10:23 暗黑长老 阅读(3794) | 评论 (2)编辑 收藏

2008年11月26日 #

H.264 RTP payload 格式


H.264 视频 RTP 负载格式

1. 网络抽象层单元类型 (NALU)

NALU 头由一个字节组成, 它的语法如下:

      +---------------+
      |0|1|2|3|4|5|6|7|
      +-+-+-+-+-+-+-+-+
      |F|NRI|  Type   |
      +---------------+

F: 1 个比特.
  forbidden_zero_bit. 在 H.264 规范中规定了这一位必须为 0.

NRI: 2 个比特.
  nal_ref_idc. 取 00 ~ 11, 似乎指示这个 NALU 的重要性, 如 00 的 NALU 解码器可以丢弃它而不影响图像的回放. 不过一般情况下不太关心

这个属性.

Type: 5 个比特.
  nal_unit_type. 这个 NALU 单元的类型. 简述如下:

  0     没有定义
  1-23  NAL单元  单个 NAL 单元包.
  24    STAP-A   单一时间的组合包
  25    STAP-B   单一时间的组合包
  26    MTAP16   多个时间的组合包
  27    MTAP24   多个时间的组合包
  28    FU-A     分片的单元
  29    FU-B     分片的单元
  30-31 没有定义

2. 打包模式

  下面是 RFC 3550 中规定的 RTP 头的结构.

       0                   1                   2                   3
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |V=2|P|X|  CC   |M|     PT      |       sequence number         |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                           timestamp                           |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |           synchronization source (SSRC) identifier            |
      +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
      |            contributing source (CSRC) identifiers             |
      |                             ....                              |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

  负载类型 Payload type (PT): 7 bits
  序列号 Sequence number (SN): 16 bits
  时间戳 Timestamp: 32 bits
 
  H.264 Payload 格式定义了三种不同的基本的负载(Payload)结构. 接收端可能通过 RTP Payload
  的第一个字节来识别它们. 这一个字节类似 NALU 头的格式, 而这个头结构的 NAL 单元类型字段
  则指出了代表的是哪一种结构,

  这个字节的结构如下, 可以看出它和 H.264 的 NALU 头结构是一样的.
      +---------------+
      |0|1|2|3|4|5|6|7|
      +-+-+-+-+-+-+-+-+
      |F|NRI|  Type   |
      +---------------+
  字段 Type: 这个 RTP payload 中 NAL 单元的类型. 这个字段和 H.264 中类型字段的区别是, 当 type
  的值为 24 ~ 31 表示这是一个特别格式的 NAL 单元, 而 H.264 中, 只取 1~23 是有效的值.
  
  24    STAP-A   单一时间的组合包
  25    STAP-B   单一时间的组合包
  26    MTAP16   多个时间的组合包
  27    MTAP24   多个时间的组合包
  28    FU-A     分片的单元
  29    FU-B     分片的单元
  30-31 没有定义

  可能的结构类型分别有:

  1. 单一 NAL 单元模式
     即一个 RTP 包仅由一个完整的 NALU 组成. 这种情况下 RTP NAL 头类型字段和原始的 H.264的
  NALU 头类型字段是一样的.

  2. 组合封包模式
    即可能是由多个 NAL 单元组成一个 RTP 包. 分别有4种组合方式: STAP-A, STAP-B, MTAP16, MTAP24.
  那么这里的类型值分别是 24, 25, 26 以及 27.

  3. 分片封包模式
    用于把一个 NALU 单元封装成多个 RTP 包. 存在两种类型 FU-A 和 FU-B. 类型值分别是 28 和 29.

2.1 单一 NAL 单元模式

  对于 NALU 的长度小于 MTU 大小的包, 一般采用单一 NAL 单元模式.
  对于一个原始的 H.264 NALU 单元常由 [Start Code] [NALU Header] [NALU Payload] 三部分组成, 其中 Start Code 用于标示这是一个

NALU 单元的开始, 必须是 "00 00 00 01" 或 "00 00 01", NALU 头仅一个字节, 其后都是 NALU 单元内容.
  打包时去除 "00 00 01" 或 "00 00 00 01" 的开始码, 把其他数据封包的 RTP 包即可.

       0                   1                   2                   3
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |F|NRI|  type   |                                               |
      +-+-+-+-+-+-+-+-+                                               |
      |                                                               |
      |               Bytes 2..n of a Single NAL unit                 |
      |                                                               |
      |                               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                               :...OPTIONAL RTP padding        |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


  如有一个 H.264 的 NALU 是这样的:

  [00 00 00 01 67 42 A0 1E 23 56 0E 2F ... ]

  这是一个序列参数集 NAL 单元. [00 00 00 01] 是四个字节的开始码, 67 是 NALU 头, 42 开始的数据是 NALU 内容.

  封装成 RTP 包将如下:

  [ RTP Header ] [ 67 42 A0 1E 23 56 0E 2F ]

  即只要去掉 4 个字节的开始码就可以了.


2.2 组合封包模式

  其次, 当 NALU 的长度特别小时, 可以把几个 NALU 单元封在一个 RTP 包中.

 
       0                   1                   2                   3
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                          RTP Header                           |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |STAP-A NAL HDR |         NALU 1 Size           | NALU 1 HDR    |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                         NALU 1 Data                           |
      :                                                               :
      +               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |               | NALU 2 Size                   | NALU 2 HDR    |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                         NALU 2 Data                           |
      :                                                               :
      |                               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                               :...OPTIONAL RTP padding        |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


2.3 Fragmentation Units (FUs).

  而当 NALU 的长度超过 MTU 时, 就必须对 NALU 单元进行分片封包. 也称为 Fragmentation Units (FUs).
 
       0                   1                   2                   3
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      | FU indicator  |   FU header   |                               |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               |
      |                                                               |
      |                         FU payload                            |
      |                                                               |
      |                               +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                               :...OPTIONAL RTP padding        |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

      Figure 14.  RTP payload format for FU-A

   The FU indicator octet has the following format:

      +---------------+
      |0|1|2|3|4|5|6|7|
      +-+-+-+-+-+-+-+-+
      |F|NRI|  Type   |
      +---------------+

   The FU header has the following format:

      +---------------+
      |0|1|2|3|4|5|6|7|
      +-+-+-+-+-+-+-+-+
      |S|E|R|  Type   |
      +---------------+


3. SDP 参数

  下面描述了如何在 SDP 中表示一个 H.264 流:

  . "m=" 行中的媒体名必须是 "video"
  . "a=rtpmap" 行中的编码名称必须是 "H264".
  . "a=rtpmap" 行中的时钟频率必须是 90000.
  . 其他参数都包括在 "a=fmtp" 行中.

  如:

  m=video 49170 RTP/AVP 98
  a=rtpmap:98 H264/90000
  a=fmtp:98 profile-level-id=42A01E; sprop-parameter-sets=Z0IACpZTBYmI,aMljiA==

  下面介绍一些常用的参数.

3.1 packetization-mode:
  表示支持的封包模式.
  当 packetization-mode 的值为 0 时或不存在时, 必须使用单一 NALU 单元模式.
  当 packetization-mode 的值为 1 时必须使用非交错(non-interleaved)封包模式.
  当 packetization-mode 的值为 2 时必须使用交错(interleaved)封包模式.
  这个参数不可以取其他的值.

3.2 sprop-parameter-sets:
  这个参数可以用于传输 H.264 的序列参数集和图像参数 NAL 单元. 这个参数的值采用 Base64 进行编码. 不同的参数集间用","号隔开.
 
3.3 profile-level-id:
  这个参数用于指示 H.264 流的 profile 类型和级别. 由 Base16(十六进制) 表示的 3 个字节. 第一个字节表示 H.264 的 Profile 类型, 第

三个字节表示 H.264 的 Profile 级别:
 
3.4 max-mbps:
  这个参数的值是一个整型, 指出了每一秒最大的宏块处理速度.

 

posted @ 2008-11-26 23:39 暗黑长老 阅读(59056) | 评论 (17)编辑 收藏

仅列出标题