libavformat/microdvddec.c
Go to the documentation of this file.
00001 /*
00002  * MicroDVD subtitle demuxer
00003  * Copyright (c) 2010  Aurelien Jacobs <aurel@gnuage.org>
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "avformat.h"
00023 #include "internal.h"
00024 #include "libavutil/intreadwrite.h"
00025 
00026 #define MAX_LINESIZE 2048
00027 
00028 
00029 typedef struct {
00030     uint8_t lines[3][MAX_LINESIZE];
00031     int64_t pos[3];
00032 } MicroDVDContext;
00033 
00034 
00035 static int microdvd_probe(AVProbeData *p)
00036 {
00037     unsigned char c, *ptr = p->buf;
00038     int i;
00039 
00040     if (AV_RB24(ptr) == 0xEFBBBF)
00041         ptr += 3;  /* skip UTF-8 BOM */
00042 
00043     for (i=0; i<3; i++) {
00044         if (sscanf(ptr, "{%*d}{}%c",     &c) != 1 &&
00045             sscanf(ptr, "{%*d}{%*d}%c",  &c) != 1 &&
00046             sscanf(ptr, "{DEFAULT}{}%c", &c) != 1)
00047             return 0;
00048         ptr += strcspn(ptr, "\n") + 1;
00049     }
00050     return AVPROBE_SCORE_MAX;
00051 }
00052 
00053 static int microdvd_read_header(AVFormatContext *s, AVFormatParameters *ap)
00054 {
00055     AVRational pts_info = (AVRational){ 2997, 125 };  /* default: 23.976 fps */
00056     MicroDVDContext *microdvd = s->priv_data;
00057     AVStream *st = avformat_new_stream(s, NULL);
00058     int i, frame;
00059     double fps;
00060     char c;
00061 
00062     if (!st)
00063         return -1;
00064     for (i=0; i<FF_ARRAY_ELEMS(microdvd->lines); i++) {
00065         microdvd->pos[i] = avio_tell(s->pb);
00066         ff_get_line(s->pb, microdvd->lines[i], sizeof(microdvd->lines[i]));
00067         if ((sscanf(microdvd->lines[i], "{%d}{}%6lf",    &frame, &fps) == 2 ||
00068              sscanf(microdvd->lines[i], "{%d}{%*d}%6lf", &frame, &fps) == 2)
00069             && frame <= 1 && fps > 3 && fps < 100)
00070             pts_info = av_d2q(fps, 100000);
00071         if (sscanf(microdvd->lines[i], "{DEFAULT}{}%c", &c) == 1) {
00072             st->codec->extradata = av_strdup(microdvd->lines[i] + 11);
00073             st->codec->extradata_size = strlen(st->codec->extradata);
00074             i--;
00075         }
00076     }
00077     avpriv_set_pts_info(st, 64, pts_info.den, pts_info.num);
00078     st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
00079     st->codec->codec_id   = CODEC_ID_MICRODVD;
00080     return 0;
00081 }
00082 
00083 static int64_t get_pts(const char *buf)
00084 {
00085     int frame;
00086     char c;
00087 
00088     if (sscanf(buf, "{%d}{%c", &frame, &c) == 2)
00089         return frame;
00090     return AV_NOPTS_VALUE;
00091 }
00092 
00093 static int microdvd_read_packet(AVFormatContext *s, AVPacket *pkt)
00094 {
00095     MicroDVDContext *microdvd = s->priv_data;
00096     char buffer[MAX_LINESIZE];
00097     int64_t pos = avio_tell(s->pb);
00098     int i, len = 0, res = AVERROR_EOF;
00099 
00100     for (i=0; i<FF_ARRAY_ELEMS(microdvd->lines); i++) {
00101         if (microdvd->lines[i][0]) {
00102             strcpy(buffer, microdvd->lines[i]);
00103             pos = microdvd->pos[i];
00104             len = strlen(buffer);
00105             microdvd->lines[i][0] = 0;
00106             break;
00107         }
00108     }
00109     if (!len)
00110         len = ff_get_line(s->pb, buffer, sizeof(buffer));
00111 
00112     if (buffer[0] && !(res = av_new_packet(pkt, len))) {
00113         memcpy(pkt->data, buffer, len);
00114         pkt->flags |= AV_PKT_FLAG_KEY;
00115         pkt->pos = pos;
00116         pkt->pts = pkt->dts = get_pts(buffer);
00117     }
00118     return res;
00119 }
00120 
00121 AVInputFormat ff_microdvd_demuxer = {
00122     .name           = "microdvd",
00123     .long_name      = NULL_IF_CONFIG_SMALL("MicroDVD subtitle format"),
00124     .priv_data_size = sizeof(MicroDVDContext),
00125     .read_probe     = microdvd_probe,
00126     .read_header    = microdvd_read_header,
00127     .read_packet    = microdvd_read_packet,
00128     .flags          = AVFMT_GENERIC_INDEX,
00129 };