avformat/mxfdec: compute both essence_offset and essence_length in mxf_compute_essenc...
[ffmpeg.git] / libavformat / srtenc.c
index b8fc8fa..d811a4d 100644 (file)
@@ -22,6 +22,7 @@
 #include "avformat.h"
 #include "internal.h"
 #include "libavutil/log.h"
+#include "libavutil/intreadwrite.h"
 
 /* TODO: add options for:
    - character encoding;
@@ -35,65 +36,80 @@ typedef struct SRTContext{
 
 static int srt_write_header(AVFormatContext *avf)
 {
+    SRTContext *srt = avf->priv_data;
+
     if (avf->nb_streams != 1 ||
-        avf->streams[0]->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) {
+        avf->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
         av_log(avf, AV_LOG_ERROR,
                "SRT supports only a single subtitles stream.\n");
         return AVERROR(EINVAL);
     }
-    if (avf->streams[0]->codec->codec_id != CODEC_ID_TEXT &&
-        avf->streams[0]->codec->codec_id != CODEC_ID_SRT) {
+    if (avf->streams[0]->codecpar->codec_id != AV_CODEC_ID_TEXT &&
+        avf->streams[0]->codecpar->codec_id != AV_CODEC_ID_SUBRIP) {
         av_log(avf, AV_LOG_ERROR,
                "Unsupported subtitles codec: %s\n",
-               avcodec_get_name(avf->streams[0]->codec->codec_id));
+               avcodec_get_name(avf->streams[0]->codecpar->codec_id));
         return AVERROR(EINVAL);
     }
     avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
+    srt->index = 1;
     return 0;
 }
 
 static int srt_write_packet(AVFormatContext *avf, AVPacket *pkt)
 {
     SRTContext *srt = avf->priv_data;
-    int write_ts = avf->streams[0]->codec->codec_id != CODEC_ID_SRT;
 
-    srt->index++;
-    if (write_ts) {
-        char buf[64];
-        int64_t s = pkt->pts, e, d = pkt->duration;
-        int len;
+    int64_t s = pkt->pts, e, d = pkt->duration;
+    int size, x1 = -1, y1 = -1, x2 = -1, y2 = -1;
+    const uint8_t *p;
 
-        if (d <= 0)
-            d = pkt->convergence_duration;
-        if (s == AV_NOPTS_VALUE || d <= 0) {
-            av_log(avf, AV_LOG_ERROR, "Insufficient timestamps.\n");
-            return AVERROR(EINVAL);
-        }
-        e = s + d;
-        len = snprintf(buf, sizeof(buf),
-                       "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d\n",
-                       srt->index,
-                       (int)(s / 3600000),      (int)(s / 60000) % 60,
-                       (int)(s /    1000) % 60, (int)(s %  1000),
-                       (int)(e / 3600000),      (int)(e / 60000) % 60,
-                       (int)(e /    1000) % 60, (int)(e %  1000));
-        avio_write(avf->pb, buf, len);
+    p = av_packet_get_side_data(pkt, AV_PKT_DATA_SUBTITLE_POSITION, &size);
+    if (p && size == 16) {
+        x1 = AV_RL32(p     );
+        y1 = AV_RL32(p +  4);
+        x2 = AV_RL32(p +  8);
+        y2 = AV_RL32(p + 12);
     }
+
+#if FF_API_CONVERGENCE_DURATION
+FF_DISABLE_DEPRECATION_WARNINGS
+    if (d <= 0)
+        /* For backward compatibility, fallback to convergence_duration. */
+        d = pkt->convergence_duration;
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
+    if (s == AV_NOPTS_VALUE || d < 0) {
+        av_log(avf, AV_LOG_WARNING,
+               "Insufficient timestamps in event number %d.\n", srt->index);
+        return 0;
+    }
+    e = s + d;
+    avio_printf(avf->pb, "%d\n%02d:%02d:%02d,%03d --> %02d:%02d:%02d,%03d",
+                   srt->index,
+                   (int)(s / 3600000),      (int)(s / 60000) % 60,
+                   (int)(s /    1000) % 60, (int)(s %  1000),
+                   (int)(e / 3600000),      (int)(e / 60000) % 60,
+                   (int)(e /    1000) % 60, (int)(e %  1000));
+    if (p)
+        avio_printf(avf->pb, "  X1:%03d X2:%03d Y1:%03d Y2:%03d",
+                    x1, x2, y1, y2);
+    avio_printf(avf->pb, "\n");
+
     avio_write(avf->pb, pkt->data, pkt->size);
-    if (write_ts)
-        avio_write(avf->pb, "\n\n", 2);
-    avio_flush(avf->pb);
+    avio_write(avf->pb, "\n\n", 2);
+    srt->index++;
     return 0;
 }
 
 AVOutputFormat ff_srt_muxer = {
     .name           = "srt",
-    .long_name      = NULL_IF_CONFIG_SMALL("SubRip subtitle format"),
+    .long_name      = NULL_IF_CONFIG_SMALL("SubRip subtitle"),
     .mime_type      = "application/x-subrip",
     .extensions     = "srt",
     .priv_data_size = sizeof(SRTContext),
     .write_header   = srt_write_header,
     .write_packet   = srt_write_packet,
-    .flags          = AVFMT_VARIABLE_FPS,
-    .subtitle_codec = CODEC_ID_TEXT,
+    .flags          = AVFMT_VARIABLE_FPS | AVFMT_TS_NONSTRICT,
+    .subtitle_codec = AV_CODEC_ID_SUBRIP,
 };