]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/avienc.c
network: properly declare WSADATA in windows
[ffmpeg] / libavformat / avienc.c
index 61af5113500da9083d44bffb7804acc8bd6ad621..2a23c213b6f84cd209b0e6fc10a7bdf73ffa925c 100644 (file)
@@ -2,26 +2,29 @@
  * AVI muxer
  * Copyright (c) 2000 Fabrice Bellard
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav 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,
+ * Libav 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
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include "avformat.h"
+#include "internal.h"
 #include "avi.h"
+#include "avio_internal.h"
 #include "riff.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/dict.h"
 
 /*
  * TODO:
@@ -77,16 +80,16 @@ static int64_t avi_start_new_riff(AVFormatContext *s, AVIOContext *pb,
     }
 
     avi->riff_start = ff_start_tag(pb, "RIFF");
-    put_tag(pb, riff_tag);
+    ffio_wfourcc(pb, riff_tag);
     loff = ff_start_tag(pb, "LIST");
-    put_tag(pb, list_tag);
+    ffio_wfourcc(pb, list_tag);
     return loff;
 }
 
 static char* avi_stream2fourcc(char* tag, int index, enum AVMediaType type)
 {
-    tag[0] = '0';
-    tag[1] = '0' + index;
+    tag[0] = '0' + index/10;
+    tag[1] = '0' + index%10;
     if (type == AVMEDIA_TYPE_VIDEO) {
         tag[2] = 'd';
         tag[3] = 'c';
@@ -107,7 +110,7 @@ static void avi_write_info_tag(AVIOContext *pb, const char *tag, const char *str
     int len = strlen(str);
     if (len > 0) {
         len++;
-        put_tag(pb, tag);
+        ffio_wfourcc(pb, tag);
         avio_wl32(pb, len);
         avio_put_str(pb, str);
         if (len & 1)
@@ -123,13 +126,13 @@ static int avi_write_counters(AVFormatContext* s, int riff_id)
     int64_t file_size;
     AVCodecContext* stream;
 
-    file_size = url_ftell(pb);
+    file_size = avio_tell(pb);
     for(n = 0; n < s->nb_streams; n++) {
         AVIStream *avist= s->streams[n]->priv_data;
 
         assert(avist->frames_hdr_strm);
         stream = s->streams[n]->codec;
-        url_fseek(pb, avist->frames_hdr_strm, SEEK_SET);
+        avio_seek(pb, avist->frames_hdr_strm, SEEK_SET);
         ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
         if(au_ssize == 0) {
             avio_wl32(pb, avist->packet_count);
@@ -141,10 +144,10 @@ static int avi_write_counters(AVFormatContext* s, int riff_id)
     }
     if(riff_id == 1) {
         assert(avi->frames_hdr_all);
-        url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
+        avio_seek(pb, avi->frames_hdr_all, SEEK_SET);
         avio_wl32(pb, nb_frames);
     }
-    url_fseek(pb, file_size, SEEK_SET);
+    avio_seek(pb, file_size, SEEK_SET);
 
     return 0;
 }
@@ -156,7 +159,13 @@ static int avi_write_header(AVFormatContext *s)
     int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
     AVCodecContext *stream, *video_enc;
     int64_t list1, list2, strh, strf;
-    AVMetadataTag *t = NULL;
+    AVDictionaryEntry *t = NULL;
+
+    if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
+        av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
+               AVI_MAX_STREAM_COUNT);
+        return -1;
+    }
 
     for(n=0;n<s->nb_streams;n++) {
         s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
@@ -169,7 +178,7 @@ static int avi_write_header(AVFormatContext *s)
     list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
 
     /* avi header */
-    put_tag(pb, "avih");
+    ffio_wfourcc(pb, "avih");
     avio_wl32(pb, 14 * 4);
     bitrate = 0;
 
@@ -190,11 +199,11 @@ static int avi_write_header(AVFormatContext *s)
     }
     avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
     avio_wl32(pb, 0); /* padding */
-    if (url_is_streamed(pb))
+    if (!pb->seekable)
         avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
     else
         avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
-    avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
+    avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
     avio_wl32(pb, nb_frames); /* nb frames, filled later */
     avio_wl32(pb, 0); /* initial frame */
     avio_wl32(pb, s->nb_streams); /* nb streams */
@@ -215,7 +224,7 @@ static int avi_write_header(AVFormatContext *s)
     for(i=0;i<n;i++) {
         AVIStream *avist= s->streams[i]->priv_data;
         list2 = ff_start_tag(pb, "LIST");
-        put_tag(pb, "strl");
+        ffio_wfourcc(pb, "strl");
 
         stream = s->streams[i]->codec;
 
@@ -229,10 +238,10 @@ static int avi_write_header(AVFormatContext *s)
                 av_log(s, AV_LOG_ERROR, "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
                 return AVERROR_PATCHWELCOME;
             }
-        case AVMEDIA_TYPE_VIDEO: put_tag(pb, "vids"); break;
-        case AVMEDIA_TYPE_AUDIO: put_tag(pb, "auds"); break;
-//        case AVMEDIA_TYPE_TEXT : put_tag(pb, "txts"); break;
-        case AVMEDIA_TYPE_DATA : put_tag(pb, "dats"); break;
+        case AVMEDIA_TYPE_VIDEO: ffio_wfourcc(pb, "vids"); break;
+        case AVMEDIA_TYPE_AUDIO: ffio_wfourcc(pb, "auds"); break;
+//      case AVMEDIA_TYPE_TEXT : ffio_wfourcc(pb, "txts"); break;
+        case AVMEDIA_TYPE_DATA : ffio_wfourcc(pb, "dats"); break;
         }
         if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
            stream->codec_id == CODEC_ID_XSUB)
@@ -248,11 +257,11 @@ static int avi_write_header(AVFormatContext *s)
 
         avio_wl32(pb, au_scale); /* scale */
         avio_wl32(pb, au_byterate); /* rate */
-        av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
+        avpriv_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
 
         avio_wl32(pb, 0); /* start */
-        avist->frames_hdr_strm = url_ftell(pb); /* remember this offset to fill later */
-        if (url_is_streamed(pb))
+        avist->frames_hdr_strm = avio_tell(pb); /* remember this offset to fill later */
+        if (!pb->seekable)
             avio_wl32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
         else
             avio_wl32(pb, 0); /* length, XXX: filled later */
@@ -290,13 +299,13 @@ static int avi_write_header(AVFormatContext *s)
             return -1;
         }
         ff_end_tag(pb, strf);
-        if ((t = av_metadata_get(s->streams[i]->metadata, "title", NULL, 0))) {
+        if ((t = av_dict_get(s->streams[i]->metadata, "title", NULL, 0))) {
             avi_write_info_tag(s->pb, "strn", t->value);
             t = NULL;
         }
       }
 
-        if (!url_is_streamed(pb)) {
+        if (pb->seekable) {
             unsigned char tag[5];
             int j;
 
@@ -311,7 +320,7 @@ static int avi_write_header(AVFormatContext *s)
             avio_w8(pb, 0);          /* bIndexSubType (0 == frame index) */
             avio_w8(pb, 0);          /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
             avio_wl32(pb, 0);        /* nEntriesInUse (will fill out later on) */
-            put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
+            ffio_wfourcc(pb, avi_stream2fourcc(tag, i, stream->codec_type));
                                     /* dwChunkId */
             avio_wl64(pb, 0);        /* dwReserved[3]
             avio_wl32(pb, 0);           Must be 0.    */
@@ -355,11 +364,11 @@ static int avi_write_header(AVFormatContext *s)
         ff_end_tag(pb, list2);
     }
 
-    if (!url_is_streamed(pb)) {
+    if (pb->seekable) {
         /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
         avi->odml_list = ff_start_tag(pb, "JUNK");
-        put_tag(pb, "odml");
-        put_tag(pb, "dmlh");
+        ffio_wfourcc(pb, "odml");
+        ffio_wfourcc(pb, "dmlh");
         avio_wl32(pb, 248);
         for (i = 0; i < 248; i+= 4)
              avio_wl32(pb, 0);
@@ -369,10 +378,10 @@ static int avi_write_header(AVFormatContext *s)
     ff_end_tag(pb, list1);
 
     list2 = ff_start_tag(pb, "LIST");
-    put_tag(pb, "INFO");
-    ff_metadata_conv(&s->metadata, ff_avi_metadata_conv, NULL);
-    for (i = 0; *ff_avi_tags[i]; i++) {
-        if ((t = av_metadata_get(s->metadata, ff_avi_tags[i], NULL, AV_METADATA_MATCH_CASE)))
+    ffio_wfourcc(pb, "INFO");
+    ff_metadata_conv(&s->metadata, ff_riff_info_conv, NULL);
+    for (i = 0; *ff_riff_tags[i]; i++) {
+        if ((t = av_dict_get(s->metadata, ff_riff_tags[i], NULL, AV_DICT_MATCH_CASE)))
             avi_write_info_tag(s->pb, t->key, t->value);
     }
     ff_end_tag(pb, list2);
@@ -384,9 +393,9 @@ static int avi_write_header(AVFormatContext *s)
     ff_end_tag(pb, list2);
 
     avi->movi_list = ff_start_tag(pb, "LIST");
-    put_tag(pb, "movi");
+    ffio_wfourcc(pb, "movi");
 
-    put_flush_packet(pb);
+    avio_flush(pb);
 
     return 0;
 }
@@ -399,7 +408,7 @@ static int avi_write_ix(AVFormatContext *s)
     char ix_tag[] = "ix00";
     int i, j;
 
-    assert(!url_is_streamed(pb));
+    assert(pb->seekable);
 
     if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
         return -1;
@@ -408,12 +417,12 @@ static int avi_write_ix(AVFormatContext *s)
         AVIStream *avist= s->streams[i]->priv_data;
          int64_t ix, pos;
 
-         avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
+         avi_stream2fourcc(tag, i, s->streams[i]->codec->codec_type);
          ix_tag[3] = '0' + i;
 
          /* Writing AVI OpenDML leaf index chunk */
-         ix = url_ftell(pb);
-         put_tag(pb, &ix_tag[0]);     /* ix?? */
+         ix = avio_tell(pb);
+         ffio_wfourcc(pb, ix_tag);     /* ix?? */
          avio_wl32(pb, avist->indexes.entry * 8 + 24);
                                       /* chunk size */
          avio_wl16(pb, 2);             /* wLongsPerEntry */
@@ -421,7 +430,7 @@ static int avi_write_ix(AVFormatContext *s)
          avio_w8(pb, 1);             /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
          avio_wl32(pb, avist->indexes.entry);
                                       /* nEntriesInUse */
-         put_tag(pb, &tag[0]);        /* dwChunkId */
+         ffio_wfourcc(pb, tag);        /* dwChunkId */
          avio_wl64(pb, avi->movi_list);/* qwBaseOffset */
          avio_wl32(pb, 0);             /* dwReserved_3 (must be 0) */
 
@@ -431,20 +440,20 @@ static int avi_write_ix(AVFormatContext *s)
              avio_wl32(pb, ((uint32_t)ie->len & ~0x80000000) |
                           (ie->flags & 0x10 ? 0 : 0x80000000));
          }
-         put_flush_packet(pb);
-         pos = url_ftell(pb);
+         avio_flush(pb);
+         pos = avio_tell(pb);
 
          /* Updating one entry in the AVI OpenDML master index */
-         url_fseek(pb, avist->indexes.indx_start - 8, SEEK_SET);
-         put_tag(pb, "indx");                 /* enabling this entry */
-         url_fskip(pb, 8);
+         avio_seek(pb, avist->indexes.indx_start - 8, SEEK_SET);
+         ffio_wfourcc(pb, "indx");            /* enabling this entry */
+         avio_skip(pb, 8);
          avio_wl32(pb, avi->riff_id);         /* nEntriesInUse */
-         url_fskip(pb, 16*avi->riff_id);
+         avio_skip(pb, 16*avi->riff_id);
          avio_wl64(pb, ix);                   /* qwOffset */
          avio_wl32(pb, pos - ix);             /* dwSize */
          avio_wl32(pb, avist->indexes.entry); /* dwDuration */
 
-         url_fseek(pb, pos, SEEK_SET);
+         avio_seek(pb, pos, SEEK_SET);
     }
     return 0;
 }
@@ -457,7 +466,7 @@ static int avi_write_idx1(AVFormatContext *s)
     int i;
     char tag[5];
 
-    if (!url_is_streamed(pb)) {
+    if (pb->seekable) {
         AVIStream *avist;
         AVIIentry* ie = 0, *tie;
         int empty, stream_id = -1;
@@ -484,9 +493,9 @@ static int avi_write_idx1(AVFormatContext *s)
             }
             if (!empty) {
                 avist= s->streams[stream_id]->priv_data;
-                avi_stream2fourcc(&tag[0], stream_id,
+                avi_stream2fourcc(tag, stream_id,
                                   s->streams[stream_id]->codec->codec_type);
-                put_tag(pb, &tag[0]);
+                ffio_wfourcc(pb, tag);
                 avio_wl32(pb, ie->flags);
                 avio_wl32(pb, ie->pos);
                 avio_wl32(pb, ie->len);
@@ -525,8 +534,8 @@ static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
     avist->packet_count++;
 
     // Make sure to put an OpenDML chunk when the file size exceeds the limits
-    if (!url_is_streamed(pb) &&
-        (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
+    if (pb->seekable &&
+        (avio_tell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
 
         avi_write_ix(s);
         ff_end_tag(pb, avi->movi_list);
@@ -538,14 +547,14 @@ static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
         avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
     }
 
-    avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
+    avi_stream2fourcc(tag, stream_index, enc->codec_type);
     if(pkt->flags&AV_PKT_FLAG_KEY)
         flags = 0x10;
     if (enc->codec_type == AVMEDIA_TYPE_AUDIO) {
        avist->audio_strm_length += size;
     }
 
-    if (!url_is_streamed(s->pb)) {
+    if (s->pb->seekable) {
         AVIIndex* idx = &avist->indexes;
         int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
         int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
@@ -560,7 +569,7 @@ static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
         }
 
         idx->cluster[cl][id].flags = flags;
-        idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
+        idx->cluster[cl][id].pos = avio_tell(pb) - avi->movi_list;
         idx->cluster[cl][id].len = size;
         idx->entry++;
     }
@@ -571,7 +580,7 @@ static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
     if (size & 1)
         avio_w8(pb, 0);
 
-    put_flush_packet(pb);
+    avio_flush(pb);
     return 0;
 }
 
@@ -583,7 +592,7 @@ static int avi_write_trailer(AVFormatContext *s)
     int i, j, n, nb_frames;
     int64_t file_size;
 
-    if (!url_is_streamed(pb)){
+    if (pb->seekable){
         if (avi->riff_id == 1) {
             ff_end_tag(pb, avi->movi_list);
             res = avi_write_idx1(s);
@@ -593,10 +602,10 @@ static int avi_write_trailer(AVFormatContext *s)
             ff_end_tag(pb, avi->movi_list);
             ff_end_tag(pb, avi->riff_start);
 
-            file_size = url_ftell(pb);
-            url_fseek(pb, avi->odml_list - 8, SEEK_SET);
-            put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
-            url_fskip(pb, 16);
+            file_size = avio_tell(pb);
+            avio_seek(pb, avi->odml_list - 8, SEEK_SET);
+            ffio_wfourcc(pb, "LIST"); /* Making this AVI OpenDML one */
+            avio_skip(pb, 16);
 
             for (n=nb_frames=0;n<s->nb_streams;n++) {
                 AVCodecContext *stream = s->streams[n]->codec;
@@ -612,12 +621,12 @@ static int avi_write_trailer(AVFormatContext *s)
                 }
             }
             avio_wl32(pb, nb_frames);
-            url_fseek(pb, file_size, SEEK_SET);
+            avio_seek(pb, file_size, SEEK_SET);
 
             avi_write_counters(s, avi->riff_id);
         }
     }
-    put_flush_packet(pb);
+    avio_flush(pb);
 
     for (i=0; i<s->nb_streams; i++) {
          AVIStream *avist= s->streams[i]->priv_data;
@@ -631,16 +640,20 @@ static int avi_write_trailer(AVFormatContext *s)
 }
 
 AVOutputFormat ff_avi_muxer = {
-    "avi",
-    NULL_IF_CONFIG_SMALL("AVI format"),
-    "video/x-msvideo",
-    "avi",
-    sizeof(AVIContext),
-    CODEC_ID_MP2,
-    CODEC_ID_MPEG4,
-    avi_write_header,
-    avi_write_packet,
-    avi_write_trailer,
+    .name              = "avi",
+    .long_name         = NULL_IF_CONFIG_SMALL("AVI format"),
+    .mime_type         = "video/x-msvideo",
+    .extensions        = "avi",
+    .priv_data_size    = sizeof(AVIContext),
+#if CONFIG_LIBMP3LAME_ENCODER
+    .audio_codec       = CODEC_ID_MP3,
+#else
+    .audio_codec       = CODEC_ID_AC3,
+#endif
+    .video_codec       = CODEC_ID_MPEG4,
+    .write_header      = avi_write_header,
+    .write_packet      = avi_write_packet,
+    .write_trailer     = avi_write_trailer,
     .codec_tag= (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
     .flags= AVFMT_VARIABLE_FPS,
 };