PageRenderTime 22ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/flv.h

https://code.google.com/
C++ Header | 202 lines | 133 code | 33 blank | 36 comment | 0 complexity | 8c66c425d13f4a80ccded8ca2d5e0565 MD5 | raw file
  1/*
  2    $Id: flv.h 231 2011-06-27 13:46:19Z marc.noirot $
  3
  4    FLV Metadata updater
  5
  6    Copyright (C) 2007-2012 Marc Noirot <marc.noirot AT gmail.com>
  7
  8    This file is part of FLVMeta.
  9
 10    FLVMeta is free software; you can redistribute it and/or modify
 11    it under the terms of the GNU General Public License as published by
 12    the Free Software Foundation; either version 2 of the License, or
 13    (at your option) any later version.
 14
 15    FLVMeta is distributed in the hope that it will be useful,
 16    but WITHOUT ANY WARRANTY; without even the implied warranty of
 17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18    GNU General Public License for more details.
 19
 20    You should have received a copy of the GNU General Public License
 21    along with FLVMeta; if not, write to the Free Software
 22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 23*/
 24#ifndef __FLV_H__
 25#define __FLV_H__
 26
 27/* Configuration of the sources */
 28#ifdef HAVE_CONFIG_H
 29# include <config.h>
 30#endif
 31
 32#include "types.h"
 33#include "amf.h"
 34
 35/* error statuses */
 36#define FLV_OK                          0
 37#define FLV_ERROR_OPEN_READ             1
 38#define FLV_ERROR_NO_FLV                2
 39#define FLV_ERROR_EOF                   3
 40#define FLV_ERROR_MEMORY                4
 41#define FLV_ERROR_EMPTY_TAG             5
 42#define FLV_ERROR_INVALID_METADATA_NAME 6
 43#define FLV_ERROR_INVALID_METADATA      7
 44
 45/* flv file format structure and definitions */
 46
 47/* FLV file header */
 48#define FLV_SIGNATURE       "FLV"
 49#define FLV_VERSION         ((uint8)0x01)
 50
 51#define FLV_FLAG_VIDEO      ((uint8)0x01)
 52#define FLV_FLAG_AUDIO      ((uint8)0x04)
 53
 54typedef struct __flv_header {
 55    byte            signature[3]; /* always "FLV" */
 56    uint8           version; /* should be 1 */
 57    uint8_bitmask   flags;
 58    uint32_be       offset; /* always 9 */
 59} flv_header;
 60
 61#define FLV_HEADER_SIZE 9
 62
 63#define flv_header_has_video(header)    ((header).flags & FLV_FLAG_VIDEO)
 64#define flv_header_has_audio(header)    ((header).flags & FLV_FLAG_AUDIO)
 65#define flv_header_get_offset(header)   (swap_uint32((header).offset))
 66
 67/* FLV tag */
 68#define FLV_TAG_TYPE_AUDIO  ((uint8)0x08)
 69#define FLV_TAG_TYPE_VIDEO  ((uint8)0x09)
 70#define FLV_TAG_TYPE_META   ((uint8)0x12)
 71
 72typedef struct __flv_tag {
 73    uint8       type;
 74    uint24_be   body_length; /* in bytes, total tag size minus 11 */
 75    uint24_be   timestamp; /* milli-seconds */
 76    uint8       timestamp_extended; /* timestamp extension */
 77    uint24_be   stream_id; /* reserved, must be "\0\0\0" */
 78    /* body comes next */
 79} flv_tag;
 80
 81#define FLV_TAG_SIZE 11
 82
 83#define flv_tag_get_body_length(tag)    (uint24_be_to_uint32((tag).body_length))
 84#define flv_tag_get_timestamp(tag) \
 85    (uint24_be_to_uint32((tag).timestamp) + ((tag).timestamp_extended << 24))
 86#define flv_tag_get_stream_id(tag)      (uint24_be_to_uint32((tag).stream_id))
 87
 88/* audio tag */
 89#define FLV_AUDIO_TAG_SOUND_TYPE_MONO    0
 90#define FLV_AUDIO_TAG_SOUND_TYPE_STEREO  1
 91
 92#define FLV_AUDIO_TAG_SOUND_SIZE_8       0
 93#define FLV_AUDIO_TAG_SOUND_SIZE_16      1
 94
 95#define FLV_AUDIO_TAG_SOUND_RATE_5_5     0
 96#define FLV_AUDIO_TAG_SOUND_RATE_11      1
 97#define FLV_AUDIO_TAG_SOUND_RATE_22      2
 98#define FLV_AUDIO_TAG_SOUND_RATE_44      3
 99
100#define FLV_AUDIO_TAG_SOUND_FORMAT_LINEAR_PCM          0
101#define FLV_AUDIO_TAG_SOUND_FORMAT_ADPCM               1
102#define FLV_AUDIO_TAG_SOUND_FORMAT_MP3                 2
103#define FLV_AUDIO_TAG_SOUND_FORMAT_LINEAR_PCM_LE       3
104#define FLV_AUDIO_TAG_SOUND_FORMAT_NELLYMOSER_16_MONO  4
105#define FLV_AUDIO_TAG_SOUND_FORMAT_NELLYMOSER_8_MONO   5
106#define FLV_AUDIO_TAG_SOUND_FORMAT_NELLYMOSER          6
107#define FLV_AUDIO_TAG_SOUND_FORMAT_G711_A              7
108#define FLV_AUDIO_TAG_SOUND_FORMAT_G711_MU             8
109#define FLV_AUDIO_TAG_SOUND_FORMAT_RESERVED            9
110#define FLV_AUDIO_TAG_SOUND_FORMAT_AAC                 10
111#define FLV_AUDIO_TAG_SOUND_FORMAT_SPEEX               11
112#define FLV_AUDIO_TAG_SOUND_FORMAT_MP3_8               14
113#define FLV_AUDIO_TAG_SOUND_FORMAT_DEVICE_SPECIFIC     15
114
115typedef byte flv_audio_tag;
116
117#define flv_audio_tag_sound_type(tag)   (((tag) & 0x01) >> 0)
118#define flv_audio_tag_sound_size(tag)   (((tag) & 0x02) >> 1)
119#define flv_audio_tag_sound_rate(tag)   (((tag) & 0x0C) >> 2)
120#define flv_audio_tag_sound_format(tag) (((tag) & 0xF0) >> 4)
121
122/* video tag */
123#define FLV_VIDEO_TAG_CODEC_JPEG            1
124#define FLV_VIDEO_TAG_CODEC_SORENSEN_H263   2
125#define FLV_VIDEO_TAG_CODEC_SCREEN_VIDEO    3
126#define FLV_VIDEO_TAG_CODEC_ON2_VP6         4
127#define FLV_VIDEO_TAG_CODEC_ON2_VP6_ALPHA   5
128#define FLV_VIDEO_TAG_CODEC_SCREEN_VIDEO_V2 6
129#define FLV_VIDEO_TAG_CODEC_AVC             7
130
131#define FLV_VIDEO_TAG_FRAME_TYPE_KEYFRAME               1
132#define FLV_VIDEO_TAG_FRAME_TYPE_INTERFRAME             2
133#define FLV_VIDEO_TAG_FRAME_TYPE_DISPOSABLE_INTERFRAME  3
134#define FLV_VIDEO_TAG_FRAME_TYPE_GENERATED_KEYFRAME     4
135#define FLV_VIDEO_TAG_FRAME_TYPE_COMMAND_FRAME          5
136
137typedef byte flv_video_tag;
138
139#define flv_video_tag_codec_id(tag)     (((tag) & 0x0F) >> 0)
140#define flv_video_tag_frame_type(tag)   (((tag) & 0xF0) >> 4)
141
142#ifdef __cplusplus
143extern "C" {
144#endif /* __cplusplus */
145
146/* FLV helper functions */
147void flv_tag_set_timestamp(flv_tag * tag, uint32 timestamp);
148
149/* FLV stream */
150#define FLV_STREAM_STATE_START          0
151#define FLV_STREAM_STATE_TAG            1
152#define FLV_STREAM_STATE_TAG_BODY       2
153#define FLV_STREAM_STATE_PREV_TAG_SIZE  3
154
155typedef struct __flv_stream {
156    FILE * flvin;
157    uint8 state;
158    flv_tag current_tag;
159    file_offset_t current_tag_offset;
160    uint32 current_tag_body_length;
161    uint32 current_tag_body_overflow;
162} flv_stream;
163
164/* FLV stream functions */
165flv_stream * flv_open(const char * file);
166int flv_read_header(flv_stream * stream, flv_header * header);
167int flv_read_prev_tag_size(flv_stream * stream, uint32 * prev_tag_size);
168int flv_read_tag(flv_stream * stream, flv_tag * tag);
169int flv_read_audio_tag(flv_stream * stream, flv_audio_tag * tag);
170int flv_read_video_tag(flv_stream * stream, flv_video_tag * tag);
171int flv_read_metadata(flv_stream * stream, amf_data ** name, amf_data ** data);
172size_t flv_read_tag_body(flv_stream * stream, void * buffer, size_t buffer_size);
173file_offset_t flv_get_current_tag_offset(flv_stream * stream);
174file_offset_t flv_get_offset(flv_stream * stream);
175void flv_reset(flv_stream * stream);
176void flv_close(flv_stream * stream);
177
178/* FLV stdio writing helper functions */
179size_t flv_write_header(FILE * out, const flv_header * header);
180size_t flv_write_tag(FILE * out, const flv_tag * tag);
181
182/* FLV event based parser */
183typedef struct __flv_parser {
184    flv_stream * stream;
185    void * user_data;
186    int (* on_header)(flv_header * header, struct __flv_parser * parser);
187    int (* on_tag)(flv_tag * tag, struct __flv_parser * parser);
188    int (* on_metadata_tag)(flv_tag * tag, amf_data * name, amf_data * data, struct __flv_parser * parser);
189    int (* on_audio_tag)(flv_tag * tag, flv_audio_tag audio_tag, struct __flv_parser * parser);
190    int (* on_video_tag)(flv_tag * tag, flv_video_tag audio_tag, struct __flv_parser * parser);
191    int (* on_unknown_tag)(flv_tag * tag, struct __flv_parser * parser);
192    int (* on_prev_tag_size)(uint32 size, struct __flv_parser * parser);
193    int (* on_stream_end)(struct __flv_parser * parser);
194} flv_parser;
195
196int flv_parse(const char * file, flv_parser * parser);
197
198#ifdef __cplusplus
199}
200#endif /* __cplusplus */
201
202#endif /* __FLV_H__ */