]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/gxfenc.c
Support picture output without -f image2 for most codecs.
[ffmpeg] / libavformat / gxfenc.c
index 6fa7e96c45867452eb9ecf4a6d9804a0fdae7f6d..72a35c35c37b59434492333f0554e8476510657d 100644 (file)
@@ -2,25 +2,27 @@
  * GXF muxer.
  * Copyright (c) 2006 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>.
  *
- * This library is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This file is part of FFmpeg.
  *
- * This library is distributed in the hope that it will be useful,
+ * 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 General Public License for more details.
+ * 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 General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ * 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
  */
 
+#include "libavutil/fifo.h"
 #include "avformat.h"
 #include "gxf.h"
 #include "riff.h"
-#include "fifo.h"
 
 #define GXF_AUDIO_PACKET_SIZE 65536
 
@@ -43,6 +45,7 @@ typedef struct GXFStreamContext {
     int b_per_gop;
     int first_gop_closed;
     int64_t current_dts;
+    int dts_delay;
 } GXFStreamContext;
 
 typedef struct GXFContext {
@@ -82,7 +85,7 @@ static const GXF_Lines gxf_lines_tab[] = {
     { 720,  6 },
 };
 
-static const CodecTag gxf_media_types[] = {
+static const AVCodecTag gxf_media_types[] = {
     { CODEC_ID_MJPEG     ,   3 }, /* NTSC */
     { CODEC_ID_MJPEG     ,   4 }, /* PAL */
     { CODEC_ID_PCM_S24LE ,   9 },
@@ -117,16 +120,16 @@ static int gxf_find_lines_index(GXFStreamContext *ctx)
     return -1;
 }
 
-static void gxf_write_padding(ByteIOContext *pb, offset_t to_pad)
+static void gxf_write_padding(ByteIOContext *pb, int64_t to_pad)
 {
     for (; to_pad > 0; to_pad--) {
         put_byte(pb, 0);
     }
 }
 
-static offset_t updatePacketSize(ByteIOContext *pb, offset_t pos)
+static int64_t updatePacketSize(ByteIOContext *pb, int64_t pos)
 {
-    offset_t curpos;
+    int64_t curpos;
     int size;
 
     size = url_ftell(pb) - pos;
@@ -141,9 +144,9 @@ static offset_t updatePacketSize(ByteIOContext *pb, offset_t pos)
     return curpos - pos;
 }
 
-static offset_t updateSize(ByteIOContext *pb, offset_t pos)
+static int64_t updateSize(ByteIOContext *pb, int64_t pos)
 {
-    offset_t curpos;
+    int64_t curpos;
 
     curpos = url_ftell(pb);
     url_fseek(pb, pos, SEEK_SET);
@@ -184,7 +187,7 @@ static int gxf_write_mpeg_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx)
                     (float)ctx->codec->bit_rate, ctx->p_per_gop, ctx->b_per_gop,
                     ctx->codec->pix_fmt == PIX_FMT_YUV422P ? 2 : 1, ctx->first_gop_closed == 1,
                     ctx->codec->height / 16);
-    put_byte(pb, 0x4F);
+    put_byte(pb, TRACK_MPG_AUX);
     put_byte(pb, size + 1);
     put_buffer(pb, (uint8_t *)buffer, size + 1);
     return size + 3;
@@ -204,7 +207,7 @@ static int gxf_write_timecode_auxiliary(ByteIOContext *pb, GXFStreamContext *ctx
 
 static int gxf_write_track_description(ByteIOContext *pb, GXFStreamContext *stream)
 {
-    offset_t pos;
+    int64_t pos;
 
     /* track description section */
     put_byte(pb, stream->media_type + 0x80);
@@ -214,7 +217,7 @@ static int gxf_write_track_description(ByteIOContext *pb, GXFStreamContext *stre
     put_be16(pb, 0); /* size */
 
     /* media file name */
-    put_byte(pb, 0x4C);
+    put_byte(pb, TRACK_NAME);
     put_byte(pb, strlen(ES_NAME_PATTERN) + 3);
     put_tag(pb, ES_NAME_PATTERN);
     put_be16(pb, stream->media_info);
@@ -222,7 +225,7 @@ static int gxf_write_track_description(ByteIOContext *pb, GXFStreamContext *stre
 
     if (stream->codec->codec_id != CODEC_ID_MPEG2VIDEO) {
         /* auxiliary information */
-        put_byte(pb, 0x4D);
+        put_byte(pb, TRACK_AUX);
         put_byte(pb, 8);
         if (stream->codec->codec_id == CODEC_ID_NONE)
             gxf_write_timecode_auxiliary(pb, stream);
@@ -231,7 +234,7 @@ static int gxf_write_track_description(ByteIOContext *pb, GXFStreamContext *stre
     }
 
     /* file system version */
-    put_byte(pb, 0x4E);
+    put_byte(pb, TRACK_VER);
     put_byte(pb, 4);
     put_be32(pb, 0);
 
@@ -239,17 +242,17 @@ static int gxf_write_track_description(ByteIOContext *pb, GXFStreamContext *stre
         gxf_write_mpeg_auxiliary(pb, stream);
 
     /* frame rate */
-    put_byte(pb, 0x50);
+    put_byte(pb, TRACK_FPS);
     put_byte(pb, 4);
     put_be32(pb, stream->frame_rate_index);
 
     /* lines per frame */
-    put_byte(pb, 0x51);
+    put_byte(pb, TRACK_LINES);
     put_byte(pb, 4);
     put_be32(pb, stream->lines_index);
 
     /* fields per frame */
-    put_byte(pb, 0x52);
+    put_byte(pb, TRACK_FPF);
     put_byte(pb, 4);
     put_be32(pb, stream->fields);
 
@@ -258,7 +261,7 @@ static int gxf_write_track_description(ByteIOContext *pb, GXFStreamContext *stre
 
 static int gxf_write_material_data_section(ByteIOContext *pb, GXFContext *ctx)
 {
-    offset_t pos;
+    int64_t pos;
     const char *filename = strrchr(ctx->fc->filename, '/');
 
     pos = url_ftell(pb);
@@ -269,33 +272,33 @@ static int gxf_write_material_data_section(ByteIOContext *pb, GXFContext *ctx)
         filename++;
     else
         filename = ctx->fc->filename;
-    put_byte(pb, 0x40);
+    put_byte(pb, MAT_NAME);
     put_byte(pb, strlen(SERVER_PATH) + strlen(filename) + 1);
     put_tag(pb, SERVER_PATH);
     put_tag(pb, filename);
     put_byte(pb, 0);
 
     /* first field */
-    put_byte(pb, 0x41);
+    put_byte(pb, MAT_FIRST_FIELD);
     put_byte(pb, 4);
     put_be32(pb, 0);
 
     /* last field */
-    put_byte(pb, 0x42);
+    put_byte(pb, MAT_LAST_FIELD);
     put_byte(pb, 4);
     put_be32(pb, ctx->nb_frames);
 
     /* reserved */
-    put_byte(pb, 0x43);
+    put_byte(pb, MAT_MARK_IN);
     put_byte(pb, 4);
     put_be32(pb, 0);
 
-    put_byte(pb, 0x44);
+    put_byte(pb, MAT_MARK_OUT);
     put_byte(pb, 4);
     put_be32(pb, ctx->nb_frames);
 
     /* estimated size */
-    put_byte(pb, 0x45);
+    put_byte(pb, MAT_SIZE);
     put_byte(pb, 4);
     put_be32(pb, url_fsize(pb) / 1024);
 
@@ -304,7 +307,7 @@ static int gxf_write_material_data_section(ByteIOContext *pb, GXFContext *ctx)
 
 static int gxf_write_track_description_section(ByteIOContext *pb, GXFContext *ctx)
 {
-    offset_t pos;
+    int64_t pos;
     int i;
 
     pos = url_ftell(pb);
@@ -316,7 +319,7 @@ static int gxf_write_track_description_section(ByteIOContext *pb, GXFContext *ct
 
 static int gxf_write_map_packet(ByteIOContext *pb, GXFContext *ctx)
 {
-    offset_t pos = url_ftell(pb);
+    int64_t pos = url_ftell(pb);
 
     gxf_write_packet_header(pb, PKT_MAP);
 
@@ -333,7 +336,7 @@ static int gxf_write_map_packet(ByteIOContext *pb, GXFContext *ctx)
 #if 0
 static int gxf_write_flt_packet(ByteIOContext *pb, GXFContext *ctx)
 {
-    offset_t pos = url_ftell(pb);
+    int64_t pos = url_ftell(pb);
     int i;
 
     gxf_write_packet_header(pb, PKT_FLT);
@@ -387,7 +390,7 @@ static int gxf_write_umf_payload(ByteIOContext *pb, GXFContext *ctx)
 
 static int gxf_write_umf_track_description(ByteIOContext *pb, GXFContext *ctx)
 {
-    offset_t pos = url_ftell(pb);
+    int64_t pos = url_ftell(pb);
     int tracks[255]={0};
     int i;
 
@@ -403,6 +406,7 @@ static int gxf_write_umf_track_description(ByteIOContext *pb, GXFContext *ctx)
         case CODEC_ID_PCM_S16LE:  id= 'A'; break;
         case CODEC_ID_DVVIDEO:    id= sc->track_type == 6 ? 'E' : 'D'; break;
         case CODEC_ID_MJPEG:      id= 'V'; break;
+        default:                  break;
         }
         sc->media_info= id << 8;
         /* FIXME first 10 audio tracks are 0 to 9 next 22 are A to V */
@@ -482,7 +486,7 @@ static int gxf_write_umf_media_mjpeg(ByteIOContext *pb, GXFStreamContext *track)
 
 static int gxf_write_umf_media_description(ByteIOContext *pb, GXFContext *ctx)
 {
-    offset_t pos;
+    int64_t pos;
     int i;
 
     pos = url_ftell(pb);
@@ -490,7 +494,7 @@ static int gxf_write_umf_media_description(ByteIOContext *pb, GXFContext *ctx)
     for (i = 0; i < ctx->fc->nb_streams; ++i) {
         GXFStreamContext *sc = &ctx->streams[i];
         char buffer[88];
-        offset_t startpos, curpos;
+        int64_t startpos, curpos;
         int path_size = strlen(ES_NAME_PATTERN);
 
         memset(buffer, 0, 88);
@@ -534,7 +538,7 @@ static int gxf_write_umf_media_description(ByteIOContext *pb, GXFContext *ctx)
 
 static int gxf_write_umf_user_data(ByteIOContext *pb, GXFContext *ctx)
 {
-    offset_t pos = url_ftell(pb);
+    int64_t pos = url_ftell(pb);
     ctx->umf_user_data_offset = pos - ctx->umf_start_offset;
     put_le32(pb, 20);
     put_le32(pb,  0);
@@ -550,7 +554,7 @@ static int gxf_write_umf_user_data(ByteIOContext *pb, GXFContext *ctx)
 
 static int gxf_write_umf_packet(ByteIOContext *pb, GXFContext *ctx)
 {
-    offset_t pos = url_ftell(pb);
+    int64_t pos = url_ftell(pb);
 
     gxf_write_packet_header(pb, PKT_UMF);
 
@@ -568,9 +572,11 @@ static int gxf_write_umf_packet(ByteIOContext *pb, GXFContext *ctx)
     return updatePacketSize(pb, pos);
 }
 
+#define GXF_NODELAY -5000
+
 static int gxf_write_header(AVFormatContext *s)
 {
-    ByteIOContext *pb = &s->pb;
+    ByteIOContext *pb = s->pb;
     GXFContext *gxf = s->priv_data;
     int i;
 
@@ -619,7 +625,8 @@ static int gxf_write_header(AVFormatContext *s)
                 gxf->flags |= 0x00000040;
             }
             gxf->sample_rate = sc->sample_rate;
-            av_set_pts_info(st, 64, 1, sc->sample_rate);
+            av_set_pts_info(st, 64, 1, st->codec->time_base.den);
+            sc->dts_delay = GXF_NODELAY;
             if (gxf_find_lines_index(sc) < 0)
                 sc->lines_index = -1;
             sc->sample_size = st->codec->bit_rate;
@@ -642,7 +649,7 @@ static int gxf_write_header(AVFormatContext *s)
                 }
                 break;
             default:
-                av_log(NULL, AV_LOG_ERROR, "video codec not supported\n");
+                av_log(s, AV_LOG_ERROR, "video codec not supported\n");
                 return -1;
             }
         }
@@ -656,7 +663,7 @@ static int gxf_write_header(AVFormatContext *s)
 
 static int gxf_write_eos_packet(ByteIOContext *pb, GXFContext *ctx)
 {
-    offset_t pos = url_ftell(pb);
+    int64_t pos = url_ftell(pb);
 
     gxf_write_packet_header(pb, PKT_EOS);
     return updatePacketSize(pb, pos);
@@ -664,17 +671,17 @@ static int gxf_write_eos_packet(ByteIOContext *pb, GXFContext *ctx)
 
 static int gxf_write_trailer(AVFormatContext *s)
 {
-    ByteIOContext *pb = &s->pb;
+    ByteIOContext *pb = s->pb;
     GXFContext *gxf = s->priv_data;
-    offset_t end;
+    int64_t end;
     int i;
 
     for (i = 0; i < s->nb_streams; ++i) {
         if (s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
             av_fifo_free(&gxf->streams[i].audio_buffer);
-        }
-        if (s->streams[i]->codec->frame_number > gxf->nb_frames)
+        } else if (s->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
             gxf->nb_frames = 2 * s->streams[i]->codec->frame_number;
+        }
     }
 
     gxf_write_eos_packet(pb, gxf);
@@ -703,7 +710,7 @@ static int gxf_parse_mpeg_frame(GXFStreamContext *sc, const uint8_t *buf, int si
 static int gxf_write_media_preamble(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt, int size)
 {
     GXFStreamContext *sc = &ctx->streams[pkt->stream_index];
-    int64_t dts = av_rescale(pkt->dts, ctx->sample_rate, sc->sample_rate);
+    int64_t dts = av_rescale_rnd(pkt->dts, ctx->sample_rate, sc->codec->time_base.den, AV_ROUND_UP);
 
     put_byte(pb, sc->media_type);
     put_byte(pb, sc->index);
@@ -738,7 +745,7 @@ static int gxf_write_media_preamble(ByteIOContext *pb, GXFContext *ctx, AVPacket
 static int gxf_write_media_packet(ByteIOContext *pb, GXFContext *ctx, AVPacket *pkt)
 {
     GXFStreamContext *sc = &ctx->streams[pkt->stream_index];
-    offset_t pos = url_ftell(pb);
+    int64_t pos = url_ftell(pb);
     int padding = 0;
 
     gxf_write_packet_header(pb, PKT_MEDIA);
@@ -756,8 +763,8 @@ static int gxf_write_packet(AVFormatContext *s, AVPacket *pkt)
 {
     GXFContext *gxf = s->priv_data;
 
-    gxf_write_media_packet(&s->pb, gxf, pkt);
-    put_flush_packet(&s->pb);
+    gxf_write_media_packet(s->pb, gxf, pkt);
+    put_flush_packet(s->pb);
     return 0;
 }
 
@@ -782,18 +789,23 @@ static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pk
     int i;
 
     for (i = 0; i < s->nb_streams; i++) {
-        if (s->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
-            GXFStreamContext *sc = &gxf->streams[i];
+        AVStream *st = s->streams[i];
+        GXFStreamContext *sc = &gxf->streams[i];
+        if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
             if (pkt && pkt->stream_index == i) {
-                av_fifo_write(&sc->audio_buffer, pkt->data, pkt->size);
+                av_fifo_generic_write(&sc->audio_buffer, pkt->data, pkt->size, NULL);
                 pkt = NULL;
             }
             if (flush || av_fifo_size(&sc->audio_buffer) >= GXF_AUDIO_PACKET_SIZE) {
-                if (gxf_new_audio_packet(gxf, sc, &new_pkt, flush) > 0) {
+                if (!pkt && gxf_new_audio_packet(gxf, sc, &new_pkt, flush) > 0) {
                     pkt = &new_pkt;
                     break; /* add pkt right now into list */
                 }
             }
+        } else if (pkt && pkt->stream_index == i) {
+            if (sc->dts_delay == GXF_NODELAY) /* adjust dts if needed */
+                sc->dts_delay = pkt->dts;
+            pkt->dts -= sc->dts_delay;
         }
     }
     return av_interleave_packet_per_dts(s, out, pkt, flush);
@@ -801,7 +813,7 @@ static int gxf_interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *pk
 
 AVOutputFormat gxf_muxer = {
     "gxf",
-    "GXF format",
+    NULL_IF_CONFIG_SMALL("GXF format"),
     NULL,
     "gxf",
     sizeof(GXFContext),