Commit 6cac3a3b authored by Allan Hsu's avatar Allan Hsu Committed by Aurelien Jacobs

Defines various common FLV format values between the FLV muxer and demuxer

in a common flv.h.
patch by Allan Hsu  allan _at_ counterpop.net

Originally committed as revision 7231 to svn://svn.ffmpeg.org/ffmpeg/trunk
parent e48dc873
/**
* @file flv.h
* FLV common header
*
* Copyright (c) 2006 The FFmpeg Project.
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FLV_H
#define FLV_H
/* offsets for packed values */
#define FLV_AUDIO_SAMPLESSIZE_OFFSET 1
#define FLV_AUDIO_SAMPLERATE_OFFSET 2
#define FLV_AUDIO_CODECID_OFFSET 4
#define FLV_VIDEO_FRAMETYPE_OFFSET 4
/* bitmasks to isolate specific values */
#define FLV_AUDIO_CHANNEL_MASK 0x01
#define FLV_AUDIO_SAMPLESIZE_MASK 0x02
#define FLV_AUDIO_SAMPLERATE_MASK 0x0c
#define FLV_AUDIO_CODECID_MASK 0xf0
#define FLV_VIDEO_CODECID_MASK 0x0f
#define FLV_VIDEO_FRAMETYPE_MASK 0xf0
#define AMF_END_OF_OBJECT 0x09
enum {
FLV_HEADER_FLAG_HASVIDEO = 1,
FLV_HEADER_FLAG_HASAUDIO = 4,
};
enum {
FLV_TAG_TYPE_AUDIO = 0x08,
FLV_TAG_TYPE_VIDEO = 0x09,
FLV_TAG_TYPE_META = 0x12,
};
enum {
FLV_MONO = 0,
FLV_STEREO = 1,
};
enum {
FLV_SAMPLESSIZE_8BIT = 0,
FLV_SAMPLESSIZE_16BIT = 1 << FLV_AUDIO_SAMPLESSIZE_OFFSET,
};
enum {
FLV_SAMPLERATE_SPECIAL = 0, /**< signifies 5512Hz and 8000Hz in the case of NELLYMOSER */
FLV_SAMPLERATE_11025HZ = 1 << FLV_AUDIO_SAMPLERATE_OFFSET,
FLV_SAMPLERATE_22050HZ = 2 << FLV_AUDIO_SAMPLERATE_OFFSET,
FLV_SAMPLERATE_44100HZ = 3 << FLV_AUDIO_SAMPLERATE_OFFSET,
};
enum {
FLV_CODECID_PCM_BE = 0,
FLV_CODECID_ADPCM = 1 << FLV_AUDIO_CODECID_OFFSET,
FLV_CODECID_MP3 = 2 << FLV_AUDIO_CODECID_OFFSET,
FLV_CODECID_PCM_LE = 3 << FLV_AUDIO_CODECID_OFFSET,
FLV_CODECID_NELLYMOSER_8HZ_MONO = 5 << FLV_AUDIO_CODECID_OFFSET,
FLV_CODECID_NELLYMOSER = 6 << FLV_AUDIO_CODECID_OFFSET,
};
enum {
FLV_CODECID_H263 = 2,
FLV_CODECID_SCREEN = 3,
FLV_CODECID_VP6 = 4,
};
enum {
FLV_FRAME_KEY = 1 << FLV_VIDEO_FRAMETYPE_OFFSET,
FLV_FRAME_INTER = 2 << FLV_VIDEO_FRAMETYPE_OFFSET,
FLV_FRAME_DISP_INTER = 3 << FLV_VIDEO_FRAMETYPE_OFFSET,
};
typedef enum {
AMF_DATA_TYPE_NUMBER = 0x00,
AMF_DATA_TYPE_BOOL = 0x01,
AMF_DATA_TYPE_STRING = 0x02,
AMF_DATA_TYPE_OBJECT = 0x03,
AMF_DATA_TYPE_NULL = 0x05,
AMF_DATA_TYPE_UNDEFINED = 0x06,
AMF_DATA_TYPE_REFERENCE = 0x07,
AMF_DATA_TYPE_MIXEDARRAY = 0x08,
AMF_DATA_TYPE_ARRAY = 0x0a,
AMF_DATA_TYPE_DATE = 0x0b,
AMF_DATA_TYPE_UNSUPPORTED = 0x0d,
} AMFDataType;
#endif /* FLV_H */
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
* - lower 4bits: difference between encoded height and visible height * - lower 4bits: difference between encoded height and visible height
*/ */
#include "avformat.h" #include "avformat.h"
#include "flv.h"
static int flv_probe(AVProbeData *p) static int flv_probe(AVProbeData *p)
{ {
...@@ -90,13 +91,13 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt) ...@@ -90,13 +91,13 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
next= size + url_ftell(&s->pb); next= size + url_ftell(&s->pb);
if (type == 8) { if (type == FLV_TAG_TYPE_AUDIO) {
is_audio=1; is_audio=1;
flags = get_byte(&s->pb); flags = get_byte(&s->pb);
} else if (type == 9) { } else if (type == FLV_TAG_TYPE_VIDEO) {
is_audio=0; is_audio=0;
flags = get_byte(&s->pb); flags = get_byte(&s->pb);
} else if (type == 18 && size > 13+1+4) { } else if (type == FLV_TAG_TYPE_META && size > 13+1+4) {
url_fskip(&s->pb, 13); //onMetaData blah url_fskip(&s->pb, 13); //onMetaData blah
if(get_byte(&s->pb) == 8){ if(get_byte(&s->pb) == 8){
url_fskip(&s->pb, 4); url_fskip(&s->pb, 4);
...@@ -113,17 +114,17 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt) ...@@ -113,17 +114,17 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
tmp[len]=0; tmp[len]=0;
type= get_byte(&s->pb); type= get_byte(&s->pb);
if(type==0){ if(type == AMF_DATA_TYPE_NUMBER){
d= av_int2dbl(get_be64(&s->pb)); d= av_int2dbl(get_be64(&s->pb));
}else if(type==2){ }else if(type == AMF_DATA_TYPE_STRING){
len= get_be16(&s->pb); len= get_be16(&s->pb);
if(len >= sizeof(tmp)) if(len >= sizeof(tmp))
break; break;
url_fskip(&s->pb, len); url_fskip(&s->pb, len);
}else if(type==8){ }else if(type == AMF_DATA_TYPE_MIXEDARRAY){
//array //array
break; break;
}else if(type==11){ }else if(type == AMF_DATA_TYPE_DATE){
d= av_int2dbl(get_be64(&s->pb)); d= av_int2dbl(get_be64(&s->pb));
get_be16(&s->pb); get_be16(&s->pb);
} }
...@@ -158,14 +159,14 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt) ...@@ -158,14 +159,14 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
st->codec->time_base= (AVRational){1,1000}; st->codec->time_base= (AVRational){1,1000};
} }
// av_log(NULL, AV_LOG_DEBUG, "%d %X %d \n", is_audio, flags, st->discard); // av_log(NULL, AV_LOG_DEBUG, "%d %X %d \n", is_audio, flags, st->discard);
if( (st->discard >= AVDISCARD_NONKEY && !((flags >> 4)==1 || is_audio)) if( (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY || is_audio))
||(st->discard >= AVDISCARD_BIDIR && ((flags >> 4)==3 && !is_audio)) ||(st->discard >= AVDISCARD_BIDIR && ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
|| st->discard >= AVDISCARD_ALL || st->discard >= AVDISCARD_ALL
){ ){
url_fseek(&s->pb, next, SEEK_SET); url_fseek(&s->pb, next, SEEK_SET);
continue; continue;
} }
if ((flags >> 4)==1) if ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY)
av_add_index_entry(st, pos, pts, size, 0, AVINDEX_KEYFRAME); av_add_index_entry(st, pos, pts, size, 0, AVINDEX_KEYFRAME);
break; break;
} }
...@@ -173,31 +174,31 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt) ...@@ -173,31 +174,31 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
if(is_audio){ if(is_audio){
if(st->codec->sample_rate == 0){ if(st->codec->sample_rate == 0){
st->codec->codec_type = CODEC_TYPE_AUDIO; st->codec->codec_type = CODEC_TYPE_AUDIO;
st->codec->channels = (flags&1)+1; st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
if((flags >> 4) == 5) if((flags & FLV_AUDIO_CODECID_MASK) == FLV_CODECID_NELLYMOSER_8HZ_MONO)
st->codec->sample_rate= 8000; st->codec->sample_rate= 8000;
else else
st->codec->sample_rate = (44100<<((flags>>2)&3))>>3; st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
switch(flags >> 4){/* 0: uncompressed 1: ADPCM 2: mp3 5: Nellymoser 8kHz mono 6: Nellymoser*/ switch(flags & FLV_AUDIO_CODECID_MASK) {
case 0: if (flags&2) st->codec->codec_id = CODEC_ID_PCM_S16BE; case FLV_CODECID_PCM_BE: if (flags & FLV_AUDIO_SAMPLESIZE_MASK) st->codec->codec_id = CODEC_ID_PCM_S16BE;
else st->codec->codec_id = CODEC_ID_PCM_S8; break; else st->codec->codec_id = CODEC_ID_PCM_S8; break;
case 1: st->codec->codec_id = CODEC_ID_ADPCM_SWF; break; case FLV_CODECID_ADPCM : st->codec->codec_id = CODEC_ID_ADPCM_SWF; break;
case 2: st->codec->codec_id = CODEC_ID_MP3; st->need_parsing = 1; break; case FLV_CODECID_MP3 : st->codec->codec_id = CODEC_ID_MP3 ; st->need_parsing = 1; break;
// this is not listed at FLV but at SWF, strange... // this is not listed at FLV but at SWF, strange...
case 3: if (flags&2) st->codec->codec_id = CODEC_ID_PCM_S16LE; case FLV_CODECID_PCM_LE: if (flags & FLV_AUDIO_SAMPLESIZE_MASK) st->codec->codec_id = CODEC_ID_PCM_S16LE;
else st->codec->codec_id = CODEC_ID_PCM_S8; break; else st->codec->codec_id = CODEC_ID_PCM_S8; break;
default: default:
av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flags >> 4); av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", flags >> 4);
st->codec->codec_tag= (flags >> 4); st->codec->codec_tag= (flags >> 4);
} }
st->codec->bits_per_sample = (flags & 2) ? 16 : 8; st->codec->bits_per_sample = (flags & FLV_AUDIO_SAMPLESIZE_MASK) ? 16 : 8;
} }
}else{ }else{
st->codec->codec_type = CODEC_TYPE_VIDEO; st->codec->codec_type = CODEC_TYPE_VIDEO;
switch(flags & 0xF){ switch(flags & FLV_VIDEO_CODECID_MASK){
case 2: st->codec->codec_id = CODEC_ID_FLV1; break; case FLV_CODECID_H263 : st->codec->codec_id = CODEC_ID_FLV1 ; break;
case 3: st->codec->codec_id = CODEC_ID_FLASHSV; break; case FLV_CODECID_SCREEN: st->codec->codec_id = CODEC_ID_FLASHSV; break;
case 4: case FLV_CODECID_VP6 :
st->codec->codec_id = CODEC_ID_VP6F; st->codec->codec_id = CODEC_ID_VP6F;
if (st->codec->extradata_size != 1) { if (st->codec->extradata_size != 1) {
st->codec->extradata_size = 1; st->codec->extradata_size = 1;
...@@ -208,8 +209,8 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt) ...@@ -208,8 +209,8 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
size--; size--;
break; break;
default: default:
av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flags & 0xf); av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flags & FLV_VIDEO_CODECID_MASK);
st->codec->codec_tag= flags & 0xF; st->codec->codec_tag = flags & FLV_VIDEO_CODECID_MASK;
} }
} }
...@@ -223,7 +224,7 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt) ...@@ -223,7 +224,7 @@ static int flv_read_packet(AVFormatContext *s, AVPacket *pkt)
pkt->pts = pts; pkt->pts = pts;
pkt->stream_index = st->index; pkt->stream_index = st->index;
if (is_audio || ((flags >> 4)==1)) if (is_audio || ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY))
pkt->flags |= PKT_FLAG_KEY; pkt->flags |= PKT_FLAG_KEY;
return ret; return ret;
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/ */
#include "avformat.h" #include "avformat.h"
#include "flv.h"
#undef NDEBUG #undef NDEBUG
#include <assert.h> #include <assert.h>
...@@ -33,21 +34,21 @@ typedef struct FLVContext { ...@@ -33,21 +34,21 @@ typedef struct FLVContext {
} FLVContext; } FLVContext;
static int get_audio_flags(AVCodecContext *enc){ static int get_audio_flags(AVCodecContext *enc){
int flags = (enc->bits_per_sample == 16) ? 0x2 : 0x0; int flags = (enc->bits_per_sample == 16) ? FLV_SAMPLESSIZE_16BIT : FLV_SAMPLESSIZE_8BIT;
switch (enc->sample_rate) { switch (enc->sample_rate) {
case 44100: case 44100:
flags |= 0x0C; flags |= FLV_SAMPLERATE_44100HZ;
break; break;
case 22050: case 22050:
flags |= 0x08; flags |= FLV_SAMPLERATE_22050HZ;
break; break;
case 11025: case 11025:
flags |= 0x04; flags |= FLV_SAMPLERATE_11025HZ;
break; break;
case 8000: //nellymoser only case 8000: //nellymoser only
case 5512: //not mp3 case 5512: //not mp3
flags |= 0x00; flags |= FLV_SAMPLERATE_SPECIAL;
break; break;
default: default:
av_log(enc, AV_LOG_ERROR, "flv doesnt support that sample rate, choose from (44100, 22050, 11025)\n"); av_log(enc, AV_LOG_ERROR, "flv doesnt support that sample rate, choose from (44100, 22050, 11025)\n");
...@@ -55,23 +56,24 @@ static int get_audio_flags(AVCodecContext *enc){ ...@@ -55,23 +56,24 @@ static int get_audio_flags(AVCodecContext *enc){
} }
if (enc->channels > 1) { if (enc->channels > 1) {
flags |= 0x01; flags |= FLV_STEREO;
} }
switch(enc->codec_id){ switch(enc->codec_id){
case CODEC_ID_MP3: case CODEC_ID_MP3:
flags |= 0x20 | 0x2; flags |= FLV_CODECID_MP3 | FLV_SAMPLESSIZE_16BIT;
break; break;
case CODEC_ID_PCM_S8: case CODEC_ID_PCM_S8:
flags |= FLV_CODECID_PCM_BE | FLV_SAMPLESSIZE_8BIT;
break; break;
case CODEC_ID_PCM_S16BE: case CODEC_ID_PCM_S16BE:
flags |= 0x2; flags |= FLV_CODECID_PCM_BE | FLV_SAMPLESSIZE_16BIT;
break; break;
case CODEC_ID_PCM_S16LE: case CODEC_ID_PCM_S16LE:
flags |= 0x30 | 0x2; flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
break; break;
case CODEC_ID_ADPCM_SWF: case CODEC_ID_ADPCM_SWF:
flags |= 0x10; flags |= FLV_CODECID_ADPCM;
break; break;
case 0: case 0:
flags |= enc->codec_tag<<4; flags |= enc->codec_tag<<4;
...@@ -84,14 +86,6 @@ static int get_audio_flags(AVCodecContext *enc){ ...@@ -84,14 +86,6 @@ static int get_audio_flags(AVCodecContext *enc){
return flags; return flags;
} }
#define AMF_DOUBLE 0
#define AMF_BOOLEAN 1
#define AMF_STRING 2
#define AMF_OBJECT 3
#define AMF_MIXED_ARRAY 8
#define AMF_ARRAY 10
#define AMF_DATE 11
static void put_amf_string(ByteIOContext *pb, const char *str) static void put_amf_string(ByteIOContext *pb, const char *str)
{ {
size_t len = strlen(str); size_t len = strlen(str);
...@@ -101,7 +95,7 @@ static void put_amf_string(ByteIOContext *pb, const char *str) ...@@ -101,7 +95,7 @@ static void put_amf_string(ByteIOContext *pb, const char *str)
static void put_amf_double(ByteIOContext *pb, double d) static void put_amf_double(ByteIOContext *pb, double d)
{ {
put_byte(pb, AMF_DOUBLE); put_byte(pb, AMF_DATA_TYPE_NUMBER);
put_be64(pb, av_dbl2int(d)); put_be64(pb, av_dbl2int(d));
} }
...@@ -160,11 +154,11 @@ static int flv_write_header(AVFormatContext *s) ...@@ -160,11 +154,11 @@ static int flv_write_header(AVFormatContext *s)
/* now data of data_size size */ /* now data of data_size size */
/* first event name as a string */ /* first event name as a string */
put_byte(pb, AMF_STRING); // 1 byte put_byte(pb, AMF_DATA_TYPE_STRING);
put_amf_string(pb, "onMetaData"); // 12 bytes put_amf_string(pb, "onMetaData"); // 12 bytes
/* mixed array (hash) with size and string/type/data tuples */ /* mixed array (hash) with size and string/type/data tuples */
put_byte(pb, AMF_MIXED_ARRAY); put_byte(pb, AMF_DATA_TYPE_MIXEDARRAY);
put_be32(pb, 4*flv->hasVideo + flv->hasAudio + 2); // +2 for duration and file size put_be32(pb, 4*flv->hasVideo + flv->hasAudio + 2); // +2 for duration and file size
put_amf_string(pb, "duration"); put_amf_string(pb, "duration");
...@@ -195,7 +189,7 @@ static int flv_write_header(AVFormatContext *s) ...@@ -195,7 +189,7 @@ static int flv_write_header(AVFormatContext *s)
put_amf_double(pb, 0); // delayed write put_amf_double(pb, 0); // delayed write
put_amf_string(pb, ""); put_amf_string(pb, "");
put_byte(pb, 9); // end marker 1 byte put_byte(pb, AMF_END_OF_OBJECT);
/* write total size of tag */ /* write total size of tag */
data_size= url_ftell(pb) - metadata_size_pos - 10; data_size= url_ftell(pb) - metadata_size_pos - 10;
...@@ -216,8 +210,8 @@ static int flv_write_trailer(AVFormatContext *s) ...@@ -216,8 +210,8 @@ static int flv_write_trailer(AVFormatContext *s)
FLVContext *flv = s->priv_data; FLVContext *flv = s->priv_data;
file_size = url_ftell(pb); file_size = url_ftell(pb);
flags |= flv->hasAudio ? 4 : 0; flags |= flv->hasAudio ? FLV_HEADER_FLAG_HASAUDIO : 0;
flags |= flv->hasVideo ? 1 : 0; flags |= flv->hasVideo ? FLV_HEADER_FLAG_HASVIDEO : 0;
url_fseek(pb, 4, SEEK_SET); url_fseek(pb, 4, SEEK_SET);
put_byte(pb,flags); put_byte(pb,flags);
...@@ -242,16 +236,16 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt) ...@@ -242,16 +236,16 @@ static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
// av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size); // av_log(s, AV_LOG_DEBUG, "type:%d pts: %"PRId64" size:%d\n", enc->codec_type, timestamp, size);
if (enc->codec_type == CODEC_TYPE_VIDEO) { if (enc->codec_type == CODEC_TYPE_VIDEO) {
put_byte(pb, 9); put_byte(pb, FLV_TAG_TYPE_VIDEO);
flags = 2; // choose h263 flags = FLV_CODECID_H263;
flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator flags |= pkt->flags & PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
} else { } else {
assert(enc->codec_type == CODEC_TYPE_AUDIO); assert(enc->codec_type == CODEC_TYPE_AUDIO);
flags = get_audio_flags(enc); flags = get_audio_flags(enc);
assert(size); assert(size);
put_byte(pb, 8); put_byte(pb, FLV_TAG_TYPE_AUDIO);
} }
put_be24(pb,size+1); // include flags put_be24(pb,size+1); // include flags
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment