On Mon, 25 Jun 2012, Luca Barbato wrote:

---

Yet another one, I do hate this code.


libavformat/flvenc.c |  146 +++++++++++++++++++++++++++-----------------------
1 files changed, 79 insertions(+), 67 deletions(-)

diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index 62756a1..8f99a7e 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -81,16 +81,16 @@ static int get_audio_flags(AVFormatContext *s, 
AVCodecContext *enc)
        }
        return FLV_CODECID_SPEEX | FLV_SAMPLERATE_11025HZ | 
FLV_SAMPLESSIZE_16BIT;
    } else {
-    switch (enc->sample_rate) {
+        switch (enc->sample_rate) {
        case    44100:
            flags |= FLV_SAMPLERATE_44100HZ;
-            break;
+        break;
        case    22050:
            flags |= FLV_SAMPLERATE_22050HZ;
-            break;
+        break;
        case    11025:
            flags |= FLV_SAMPLERATE_11025HZ;
-            break;
+        break;

Why these changes? We keep the break statements indented in our coding style.

        case    16000: //nellymoser only
        case     8000: //nellymoser only
        case     5512: //not mp3
@@ -99,9 +99,11 @@ static int get_audio_flags(AVFormatContext *s, 
AVCodecContext *enc)
                break;
            }
        default:
-            av_log(s, AV_LOG_ERROR, "flv does not support that sample rate, choose 
from (44100, 22050, 11025).\n");
+            av_log(s, AV_LOG_ERROR,
+                   "flv does not support that sample rate, "
+                   "choose from (44100, 22050, 11025).\n");
            return -1;
-    }
+        }
    }

    if (enc->channels > 1) {
@@ -151,16 +153,17 @@ static void put_amf_string(AVIOContext *pb, const char 
*str)
    avio_write(pb, str, len);
}

-static void put_avc_eos_tag(AVIOContext *pb, unsigned ts) {
+static void put_avc_eos_tag(AVIOContext *pb, unsigned ts)
+{
    avio_w8(pb, FLV_TAG_TYPE_VIDEO);
-    avio_wb24(pb, 5);  /* Tag Data Size */
-    avio_wb24(pb, ts);  /* lower 24 bits of timestamp in ms*/
-    avio_w8(pb, (ts >> 24) & 0x7F);  /* MSB of ts in ms*/
-    avio_wb24(pb, 0);  /* StreamId = 0 */
-    avio_w8(pb, 23);  /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
-    avio_w8(pb, 2);  /* AVC end of sequence */
-    avio_wb24(pb, 0);  /* Always 0 for AVC EOS. */
-    avio_wb32(pb, 16);  /* Size of FLV tag */
+    avio_wb24(pb, 5);               /* Tag Data Size */
+    avio_wb24(pb, ts);              /* lower 24 bits of timestamp in ms*/
+    avio_w8(pb, (ts >> 24) & 0x7F); /* MSB of ts in ms*/
+    avio_wb24(pb, 0);               /* StreamId = 0 */
+    avio_w8(pb, 23);                /* ub[4] FrameType = 1, ub[4] CodecId = 7 
*/
+    avio_w8(pb, 2);                 /* AVC end of sequence */
+    avio_wb24(pb, 0);               /* Always 0 for AVC EOS. */
+    avio_wb32(pb, 16);              /* Size of FLV tag */
}

static void put_amf_double(AVIOContext *pb, double d)
@@ -169,7 +172,8 @@ static void put_amf_double(AVIOContext *pb, double d)
    avio_wb64(pb, av_double2int(d));
}

-static void put_amf_bool(AVIOContext *pb, int b) {
+static void put_amf_bool(AVIOContext *pb, int b)
+{
    avio_w8(pb, AMF_DATA_TYPE_BOOL);
    avio_w8(pb, !!b);
}
@@ -184,34 +188,35 @@ static int flv_write_header(AVFormatContext *s)
    int64_t metadata_size_pos, data_size, metadata_count_pos;
    AVDictionaryEntry *tag = NULL;

-    for(i=0; i<s->nb_streams; i++){
+    for (i=0; i<s->nb_streams; i++) {
        AVCodecContext *enc = s->streams[i]->codec;
        FLVStreamContext *sc;
        switch (enc->codec_type) {
        case AVMEDIA_TYPE_VIDEO:
-            if (s->streams[i]->r_frame_rate.den && 
s->streams[i]->r_frame_rate.num) {
+            if (s->streams[i]->r_frame_rate.den &&
+                s->streams[i]->r_frame_rate.num) {
                framerate = av_q2d(s->streams[i]->r_frame_rate);
            } else {
-                framerate = 1/av_q2d(s->streams[i]->codec->time_base);
+                framerate = 1 / av_q2d(s->streams[i]->codec->time_base);
            }
            video_enc = enc;
            if(enc->codec_tag == 0) {
                av_log(s, AV_LOG_ERROR, "video codec not compatible with 
flv\n");
                return -1;
            }
-            break;
+        break;
        case AVMEDIA_TYPE_AUDIO:
            audio_enc = enc;
            if (get_audio_flags(s, enc) < 0)
                return AVERROR_INVALIDDATA;
-            break;
+        break;
        case AVMEDIA_TYPE_DATA:
            if (enc->codec_id != CODEC_ID_TEXT) {
                av_log(s, AV_LOG_ERROR, "codec not compatible with flv\n");
                return AVERROR_INVALIDDATA;
            }
            data_enc = enc;
-            break;
+        break;
        default:

Don't move the break statements, they were correctly placed already.

            av_log(s, AV_LOG_ERROR, "codec not compatible with flv\n");
            return -1;
@@ -224,29 +229,30 @@ static int flv_write_header(AVFormatContext *s)
        s->streams[i]->priv_data = sc;
        sc->last_ts = -1;
    }
+
    flv->delay = AV_NOPTS_VALUE;

    avio_write(pb, "FLV", 3);
-    avio_w8(pb,1);
+    avio_w8(pb, 1);
    avio_w8(pb,   FLV_HEADER_FLAG_HASAUDIO * !!audio_enc
-                 + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
-    avio_wb32(pb,9);
-    avio_wb32(pb,0);
-
-    for(i=0; i<s->nb_streams; i++){
-        if(s->streams[i]->codec->codec_tag == 5){
-            avio_w8(pb,8); // message type
-            avio_wb24(pb,0); // include flags
-            avio_wb24(pb,0); // time stamp
-            avio_wb32(pb,0); // reserved
-            avio_wb32(pb,11); // size
-            flv->reserved=5;
+                + FLV_HEADER_FLAG_HASVIDEO * !!video_enc);
+    avio_wb32(pb, 9);
+    avio_wb32(pb, 0);
+
+    for (i = 0; i < s->nb_streams; i++) {
+        if (s->streams[i]->codec->codec_tag == 5) {
+            avio_w8(pb, 8);    // message type
+            avio_wb24(pb, 0);  // include flags
+            avio_wb24(pb, 0);  // time stamp
+            avio_wb32(pb, 0);  // reserved
+            avio_wb32(pb, 11); // size
+            flv->reserved = 5;
        }
    }

    /* write meta_tag */
    avio_w8(pb, 18);         // tag type META
-    metadata_size_pos= avio_tell(pb);
+    metadata_size_pos = avio_tell(pb);
    avio_wb24(pb, 0);          // size of data part (sum of all parts below)
    avio_wb24(pb, 0);          // time stamp
    avio_wb32(pb, 0);          // reserved
@@ -269,9 +275,11 @@ static int flv_write_header(AVFormatContext *s)

    put_amf_string(pb, "duration");
    flv->duration_offset= avio_tell(pb);
-    put_amf_double(pb, s->duration / AV_TIME_BASE); // fill in the guessed 
duration, it'll be corrected later if incorrect

-    if(video_enc){
+    // fill in the guessed duration, it'll be corrected later if incorrect
+    put_amf_double(pb, s->duration / AV_TIME_BASE);
+
+    if (video_enc) {
        put_amf_string(pb, "width");
        put_amf_double(pb, video_enc->width);

@@ -288,7 +296,7 @@ static int flv_write_header(AVFormatContext *s)
        put_amf_double(pb, video_enc->codec_tag);
    }

-    if(audio_enc){
+    if (audio_enc) {
        put_amf_string(pb, "audiodatarate");
        put_amf_double(pb, audio_enc->bit_rate / 1024.0);

@@ -410,40 +418,40 @@ 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);

-    if(enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
-       enc->codec_id == CODEC_ID_AAC)
-        flags_size= 2;
+    if (enc->codec_id == CODEC_ID_VP6 || enc->codec_id == CODEC_ID_VP6F ||
+        enc->codec_id == CODEC_ID_AAC)
+        flags_size = 2;
    else if(enc->codec_id == CODEC_ID_H264)
-        flags_size= 5;
+        flags_size = 5;
    else
-        flags_size= 1;
+        flags_size = 1;

    switch (enc->codec_type) {
    case AVMEDIA_TYPE_VIDEO:
        avio_w8(pb, FLV_TAG_TYPE_VIDEO);

        flags = enc->codec_tag;
-        if(flags == 0) {
+        if (flags == 0) {
            av_log(s, AV_LOG_ERROR, "video codec %X not compatible with 
flv\n",enc->codec_id);
            return -1;
        }

        flags |= pkt->flags & AV_PKT_FLAG_KEY ? FLV_FRAME_KEY : FLV_FRAME_INTER;
-        break;
+    break;
    case AVMEDIA_TYPE_AUDIO:
        flags = get_audio_flags(s, enc);

        assert(size);

        avio_w8(pb, FLV_TAG_TYPE_AUDIO);
-        break;
-
+    break;
    case AVMEDIA_TYPE_DATA:
        avio_w8(pb, FLV_TAG_TYPE_META);
-        break;
+    break;

Dito

Other than that, the rest looks ok.

// Martin
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to