[Ffmpeg-cvslog] r7231 - in trunk/libavformat: flv.h flvdec.c flvenc.c

aurel subversion
Wed Dec 6 01:23:05 CET 2006


Author: aurel
Date: Wed Dec  6 01:23:04 2006
New Revision: 7231

Added:
   trunk/libavformat/flv.h
Modified:
   trunk/libavformat/flvdec.c
   trunk/libavformat/flvenc.c

Log:
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


Added: trunk/libavformat/flv.h
==============================================================================
--- (empty file)
+++ trunk/libavformat/flv.h	Wed Dec  6 01:23:04 2006
@@ -0,0 +1,108 @@
+/**
+ * @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 */

Modified: trunk/libavformat/flvdec.c
==============================================================================
--- trunk/libavformat/flvdec.c	(original)
+++ trunk/libavformat/flvdec.c	Wed Dec  6 01:23:04 2006
@@ -25,6 +25,7 @@
  *  - lower 4bits: difference between encoded height and visible height
  */
 #include "avformat.h"
+#include "flv.h"
 
 static int flv_probe(AVProbeData *p)
 {
@@ -90,13 +91,13 @@
 
     next= size + url_ftell(&s->pb);
 
-    if (type == 8) {
+    if (type == FLV_TAG_TYPE_AUDIO) {
         is_audio=1;
         flags = get_byte(&s->pb);
-    } else if (type == 9) {
+    } else if (type == FLV_TAG_TYPE_VIDEO) {
         is_audio=0;
         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
         if(get_byte(&s->pb) == 8){
             url_fskip(&s->pb, 4);
@@ -113,17 +114,17 @@
             tmp[len]=0;
 
             type= get_byte(&s->pb);
-            if(type==0){
+            if(type == AMF_DATA_TYPE_NUMBER){
                 d= av_int2dbl(get_be64(&s->pb));
-            }else if(type==2){
+            }else if(type == AMF_DATA_TYPE_STRING){
                 len= get_be16(&s->pb);
                 if(len >= sizeof(tmp))
                     break;
                 url_fskip(&s->pb, len);
-            }else if(type==8){
+            }else if(type == AMF_DATA_TYPE_MIXEDARRAY){
                 //array
                 break;
-            }else if(type==11){
+            }else if(type == AMF_DATA_TYPE_DATE){
                 d= av_int2dbl(get_be64(&s->pb));
                 get_be16(&s->pb);
             }
@@ -158,14 +159,14 @@
         st->codec->time_base= (AVRational){1,1000};
     }
 //    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))
-       ||(st->discard >= AVDISCARD_BIDIR  &&  ((flags >> 4)==3 && !is_audio))
+    if(  (st->discard >= AVDISCARD_NONKEY && !((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_KEY ||         is_audio))
+       ||(st->discard >= AVDISCARD_BIDIR  &&  ((flags & FLV_VIDEO_FRAMETYPE_MASK) == FLV_FRAME_DISP_INTER && !is_audio))
        || st->discard >= AVDISCARD_ALL
        ){
         url_fseek(&s->pb, next, SEEK_SET);
         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);
     break;
  }
@@ -173,31 +174,31 @@
     if(is_audio){
         if(st->codec->sample_rate == 0){
             st->codec->codec_type = CODEC_TYPE_AUDIO;
-            st->codec->channels = (flags&1)+1;
-            if((flags >> 4) == 5)
+            st->codec->channels = (flags & FLV_AUDIO_CHANNEL_MASK) == FLV_STEREO ? 2 : 1;
+            if((flags & FLV_AUDIO_CODECID_MASK) == FLV_CODECID_NELLYMOSER_8HZ_MONO)
                 st->codec->sample_rate= 8000;
             else
-                st->codec->sample_rate = (44100<<((flags>>2)&3))>>3;
-            switch(flags >> 4){/* 0: uncompressed 1: ADPCM 2: mp3 5: Nellymoser 8kHz mono 6: Nellymoser*/
-            case 0: if (flags&2) st->codec->codec_id = CODEC_ID_PCM_S16BE;
+                st->codec->sample_rate = (44100 << ((flags & FLV_AUDIO_SAMPLERATE_MASK) >> FLV_AUDIO_SAMPLERATE_OFFSET) >> 3);
+            switch(flags & FLV_AUDIO_CODECID_MASK) {
+            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;
-            case 1: 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_ADPCM : st->codec->codec_id = CODEC_ID_ADPCM_SWF;                       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...
-            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;
             default:
                     av_log(s, AV_LOG_INFO, "Unsupported audio codec (%x)\n", 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{
             st->codec->codec_type = CODEC_TYPE_VIDEO;
-            switch(flags & 0xF){
-            case 2: st->codec->codec_id = CODEC_ID_FLV1; break;
-            case 3: st->codec->codec_id = CODEC_ID_FLASHSV; break;
-            case 4:
+            switch(flags & FLV_VIDEO_CODECID_MASK){
+            case FLV_CODECID_H263  : st->codec->codec_id = CODEC_ID_FLV1   ; break;
+            case FLV_CODECID_SCREEN: st->codec->codec_id = CODEC_ID_FLASHSV; break;
+            case FLV_CODECID_VP6   :
                 st->codec->codec_id = CODEC_ID_VP6F;
                 if (st->codec->extradata_size != 1) {
                     st->codec->extradata_size = 1;
@@ -208,8 +209,8 @@
                 size--;
                 break;
             default:
-                    av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flags & 0xf);
-                st->codec->codec_tag= flags & 0xF;
+                av_log(s, AV_LOG_INFO, "Unsupported video codec (%x)\n", flags & FLV_VIDEO_CODECID_MASK);
+                st->codec->codec_tag = flags & FLV_VIDEO_CODECID_MASK;
             }
     }
 
@@ -223,7 +224,7 @@
     pkt->pts = pts;
     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;
 
     return ret;

Modified: trunk/libavformat/flvenc.c
==============================================================================
--- trunk/libavformat/flvenc.c	(original)
+++ trunk/libavformat/flvenc.c	Wed Dec  6 01:23:04 2006
@@ -19,6 +19,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include "avformat.h"
+#include "flv.h"
 
 #undef NDEBUG
 #include <assert.h>
@@ -33,21 +34,21 @@
 } FLVContext;
 
 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) {
         case    44100:
-            flags |= 0x0C;
+            flags |= FLV_SAMPLERATE_44100HZ;
             break;
         case    22050:
-            flags |= 0x08;
+            flags |= FLV_SAMPLERATE_22050HZ;
             break;
         case    11025:
-            flags |= 0x04;
+            flags |= FLV_SAMPLERATE_11025HZ;
             break;
         case     8000: //nellymoser only
         case     5512: //not mp3
-            flags |= 0x00;
+            flags |= FLV_SAMPLERATE_SPECIAL;
             break;
         default:
             av_log(enc, AV_LOG_ERROR, "flv doesnt support that sample rate, choose from (44100, 22050, 11025)\n");
@@ -55,23 +56,24 @@
     }
 
     if (enc->channels > 1) {
-        flags |= 0x01;
+        flags |= FLV_STEREO;
     }
 
     switch(enc->codec_id){
     case CODEC_ID_MP3:
-        flags |= 0x20 | 0x2;
+        flags |= FLV_CODECID_MP3    | FLV_SAMPLESSIZE_16BIT;
         break;
     case CODEC_ID_PCM_S8:
+        flags |= FLV_CODECID_PCM_BE | FLV_SAMPLESSIZE_8BIT;
         break;
     case CODEC_ID_PCM_S16BE:
-        flags |= 0x2;
+        flags |= FLV_CODECID_PCM_BE | FLV_SAMPLESSIZE_16BIT;
         break;
     case CODEC_ID_PCM_S16LE:
-        flags |= 0x30 | 0x2;
+        flags |= FLV_CODECID_PCM_LE | FLV_SAMPLESSIZE_16BIT;
         break;
     case CODEC_ID_ADPCM_SWF:
-        flags |= 0x10;
+        flags |= FLV_CODECID_ADPCM;
         break;
     case 0:
         flags |= enc->codec_tag<<4;
@@ -84,14 +86,6 @@
     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)
 {
     size_t len = strlen(str);
@@ -101,7 +95,7 @@
 
 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));
 }
 
@@ -160,11 +154,11 @@
     /* now data of data_size size */
 
     /* 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
 
     /* 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_amf_string(pb, "duration");
@@ -195,7 +189,7 @@
     put_amf_double(pb, 0); // delayed write
 
     put_amf_string(pb, "");
-    put_byte(pb, 9); // end marker 1 byte
+    put_byte(pb, AMF_END_OF_OBJECT);
 
     /* write total size of tag */
     data_size= url_ftell(pb) - metadata_size_pos - 10;
@@ -216,8 +210,8 @@
     FLVContext *flv = s->priv_data;
 
     file_size = url_ftell(pb);
-    flags |= flv->hasAudio ? 4 : 0;
-    flags |= flv->hasVideo ? 1 : 0;
+    flags |= flv->hasAudio ? FLV_HEADER_FLAG_HASAUDIO : 0;
+    flags |= flv->hasVideo ? FLV_HEADER_FLAG_HASVIDEO : 0;
     url_fseek(pb, 4, SEEK_SET);
     put_byte(pb,flags);
 
@@ -242,16 +236,16 @@
 //    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) {
-        put_byte(pb, 9);
-        flags = 2; // choose h263
-        flags |= pkt->flags & PKT_FLAG_KEY ? 0x10 : 0x20; // add keyframe indicator
+        put_byte(pb, FLV_TAG_TYPE_VIDEO);
+        flags = FLV_CODECID_H263;
+        flags |= pkt->flags & PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
     } else {
         assert(enc->codec_type == CODEC_TYPE_AUDIO);
         flags = get_audio_flags(enc);
 
         assert(size);
 
-        put_byte(pb, 8);
+        put_byte(pb, FLV_TAG_TYPE_AUDIO);
     }
 
     put_be24(pb,size+1); // include flags




More information about the ffmpeg-cvslog mailing list