Advertisement
Guest User

Untitled

a guest
Oct 4th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. commit f4bd048d4121ce017c7a9427f647566083cccbd1
  2. Author: Eejya <singh.eejya@gmail.com>
  3. Date: Fri Oct 3 01:43:20 2014 +0530
  4.  
  5. Added STL demuxer for supporting subtitles with .stl file format.
  6.  
  7. diff --git a/libavformat/Makefile b/libavformat/Makefile
  8. index 86064ea..60e056d 100644
  9. --- a/libavformat/Makefile
  10. +++ b/libavformat/Makefile
  11. @@ -433,6 +433,7 @@ OBJS-$(CONFIG_VOBSUB_DEMUXER) += subtitles.o # mpeg demuxer is in the
  12. OBJS-$(CONFIG_VOC_DEMUXER) += vocdec.o voc.o
  13. OBJS-$(CONFIG_VOC_MUXER) += vocenc.o voc.o
  14. OBJS-$(CONFIG_VPLAYER_DEMUXER) += vplayerdec.o subtitles.o
  15. +OBJS-$(CONFIG_STL_DEMUXER) += stldec.o subtitles.o
  16. OBJS-$(CONFIG_VQF_DEMUXER) += vqf.o
  17. OBJS-$(CONFIG_W64_DEMUXER) += wavdec.o w64.o pcm.o
  18. OBJS-$(CONFIG_W64_MUXER) += wavenc.o w64.o
  19. diff --git a/libavformat/allformats.c b/libavformat/allformats.c
  20. index d54ed9b..c1a3a4e 100644
  21. --- a/libavformat/allformats.c
  22. +++ b/libavformat/allformats.c
  23. @@ -258,6 +258,7 @@ void av_register_all(void)
  24. REGISTER_DEMUXER (SBG, sbg);
  25. REGISTER_DEMUXER (SDP, sdp);
  26. REGISTER_DEMUXER (SDR2, sdr2);
  27. + REGISTER_DEMUXER (STL, stl);
  28. #if CONFIG_RTPDEC
  29. ff_register_rtp_dynamic_payload_handlers();
  30. ff_register_rdt_dynamic_payload_handlers();
  31. diff --git a/libavformat/stldec.c b/libavformat/stldec.c
  32. new file mode 100644
  33. index 0000000..a428de0
  34. --- /dev/null
  35. +++ b/libavformat/stldec.c
  36. @@ -0,0 +1,110 @@
  37. +#include "avformat.h"
  38. +#include "internal.h"
  39. +#include "subtitles.h"
  40. +#include "libavutil/intreadwrite.h"
  41. +typedef struct {
  42. + FFDemuxSubtitlesQueue q;
  43. +} STLContext;
  44. +
  45. +static int stl_probe(AVProbeData *p)
  46. +{
  47. + char c;
  48. + const unsigned char *ptr = p->buf;
  49. + while(*ptr=='\r' || *ptr=='\n' || *ptr=='$' || (ptr[0]=='/' && ptr[1]=='/'))
  50. + ptr+=ff_subtitles_next_line(ptr);
  51. +
  52. + if (sscanf(ptr, "%*d:%*d:%*d:%*d , %*d:%*d:%*d:%*d , %c", &c) == 1)
  53. + return AVPROBE_SCORE_MAX;
  54. + return 0;
  55. +
  56. +}
  57. +static int64_t get_pts(char **buf, int *duration)
  58. +{
  59. + int hh1, mm1, ss1, ms1;
  60. + int hh2, mm2, ss2, ms2;
  61. + int len;
  62. + if (sscanf(*buf, "%2d:%2d:%2d:%2d , %2d:%2d:%2d:%2d , %n",
  63. + &hh1, &mm1, &ss1, &ms1,
  64. + &hh2, &mm2, &ss2, &ms2, &len) >= 6) {
  65. +
  66. + int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
  67. + int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
  68. + *duration = end - start;
  69. + *buf+=len;
  70. + return start;
  71. + }
  72. + return AV_NOPTS_VALUE;
  73. +}
  74. +
  75. +static int stl_read_header(AVFormatContext *s)
  76. +{
  77. + STLContext *stl = s->priv_data;
  78. + AVStream *st = avformat_new_stream(s, NULL);
  79. +
  80. + if (!st)
  81. + return AVERROR(ENOMEM);
  82. + avpriv_set_pts_info(st, 64, 1, 100);
  83. + st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  84. + st->codec->codec_id = AV_CODEC_ID_TEXT;
  85. +
  86. + while (!avio_feof(s->pb)) {
  87. + char line[4096];
  88. + char *p = line;
  89. + const int64_t pos = avio_tell(s->pb);
  90. + int len = ff_get_line(s->pb, line, sizeof(line));
  91. + int64_t pts_start;
  92. + int duration;
  93. + if (!len)
  94. + break;
  95. +
  96. + line[strcspn(line, "\r\n")] = 0;
  97. +
  98. + pts_start = get_pts(&p , &duration);
  99. + if (pts_start != AV_NOPTS_VALUE) {
  100. + AVPacket *sub;
  101. +
  102. + sub = ff_subtitles_queue_insert(&stl->q, p, strlen(p), 0);
  103. + if (!sub)
  104. + return AVERROR(ENOMEM);
  105. + sub->pos = pos;
  106. + sub->pts = pts_start;
  107. + sub->duration = duration;
  108. + }
  109. + }
  110. +
  111. + ff_subtitles_queue_finalize(&stl->q);
  112. + return 0;
  113. +
  114. +}
  115. +static int stl_read_packet(AVFormatContext *s, AVPacket *pkt)
  116. +{
  117. + STLContext *stl = s->priv_data;
  118. + return ff_subtitles_queue_read_packet(&stl->q, pkt);
  119. +}
  120. +
  121. +static int stl_read_seek(AVFormatContext *s, int stream_index,
  122. + int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  123. +{
  124. + STLContext *stl = s->priv_data;
  125. + return ff_subtitles_queue_seek(&stl->q, s, stream_index,
  126. + min_ts, ts, max_ts, flags);
  127. +}
  128. +
  129. +static int stl_read_close(AVFormatContext *s)
  130. +{
  131. + STLContext *stl = s->priv_data;
  132. + ff_subtitles_queue_clean(&stl->q);
  133. + return 0;
  134. +}
  135. +
  136. +AVInputFormat ff_stl_demuxer = {
  137. + .name = "stl",
  138. + .long_name = NULL_IF_CONFIG_SMALL("STL subtitles"),
  139. + .priv_data_size = sizeof(STLContext),
  140. + .read_probe = stl_probe,
  141. + .read_header = stl_read_header,
  142. + .read_packet = stl_read_packet,
  143. + .read_seek2 = stl_read_seek,
  144. + .read_close = stl_read_close,
  145. + .extensions = "stl",
  146. +};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement