Advertisement
Guest User

Untitled

a guest
Oct 4th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. commit f1ff6a0db059f577502809e4e3aad224179ed944
  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..ad088cb
  34. --- /dev/null
  35. +++ b/libavformat/stldec.c
  36. @@ -0,0 +1,113 @@
  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=='$')
  50. + ptr+=ff_subtitles_next_line(ptr);
  51. +
  52. + while(ptr[0]=='/' && ptr[1]=='/')
  53. + ptr+=ff_subtitles_next_line(ptr);
  54. +
  55. + if (sscanf(ptr, "%*d:%*d:%*d:%*d , %*d:%*d:%*d:%*d , %c", &c) == 1)
  56. + return AVPROBE_SCORE_MAX;
  57. + return 0;
  58. +
  59. +}
  60. +static int64_t get_pts(char **buf, int *duration)
  61. +{
  62. + int hh1, mm1, ss1, ms1;
  63. + int hh2, mm2, ss2, ms2;
  64. + int len;
  65. + if (sscanf(*buf, "%2d:%2d:%2d:%2d , %2d:%2d:%2d:%2d , %n",
  66. + &hh1, &mm1, &ss1, &ms1,
  67. + &hh2, &mm2, &ss2, &ms2, &len) >= 6) {
  68. +
  69. + int64_t start = (hh1*3600LL + mm1*60LL + ss1) * 100LL + ms1;
  70. + int64_t end = (hh2*3600LL + mm2*60LL + ss2) * 100LL + ms2;
  71. + *duration = end - start;
  72. + *buf+=len;
  73. + return start;
  74. + }
  75. + return AV_NOPTS_VALUE;
  76. +}
  77. +
  78. +static int stl_read_header(AVFormatContext *s)
  79. +{
  80. + STLContext *stl = s->priv_data;
  81. + AVStream *st = avformat_new_stream(s, NULL);
  82. +
  83. + if (!st)
  84. + return AVERROR(ENOMEM);
  85. + avpriv_set_pts_info(st, 64, 1, 100);
  86. + st->codec->codec_type = AVMEDIA_TYPE_SUBTITLE;
  87. + st->codec->codec_id = AV_CODEC_ID_TEXT;
  88. +
  89. + while (!avio_feof(s->pb)) {
  90. + char line[4096];
  91. + char *p = line;
  92. + const int64_t pos = avio_tell(s->pb);
  93. + int len = ff_get_line(s->pb, line, sizeof(line));
  94. + int64_t pts_start;
  95. + int duration;
  96. + if (!len)
  97. + break;
  98. +
  99. + line[strcspn(line, "\r\n")] = 0;
  100. +
  101. + pts_start = get_pts(&p , &duration);
  102. + if (pts_start != AV_NOPTS_VALUE) {
  103. + AVPacket *sub;
  104. +
  105. + sub = ff_subtitles_queue_insert(&stl->q, p, strlen(p), 0);
  106. + if (!sub)
  107. + return AVERROR(ENOMEM);
  108. + sub->pos = pos;
  109. + sub->pts = pts_start;
  110. + sub->duration = duration;
  111. + }
  112. + }
  113. +
  114. + ff_subtitles_queue_finalize(&stl->q);
  115. + return 0;
  116. +
  117. +}
  118. +static int stl_read_packet(AVFormatContext *s, AVPacket *pkt)
  119. +{
  120. + STLContext *stl = s->priv_data;
  121. + return ff_subtitles_queue_read_packet(&stl->q, pkt);
  122. +}
  123. +
  124. +static int stl_read_seek(AVFormatContext *s, int stream_index,
  125. + int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
  126. +{
  127. + STLContext *stl = s->priv_data;
  128. + return ff_subtitles_queue_seek(&stl->q, s, stream_index,
  129. + min_ts, ts, max_ts, flags);
  130. +}
  131. +
  132. +static int stl_read_close(AVFormatContext *s)
  133. +{
  134. + STLContext *stl = s->priv_data;
  135. + ff_subtitles_queue_clean(&stl->q);
  136. + return 0;
  137. +}
  138. +
  139. +AVInputFormat ff_stl_demuxer = {
  140. + .name = "stl",
  141. + .long_name = NULL_IF_CONFIG_SMALL("STL subtitles"),
  142. + .priv_data_size = sizeof(STLContext),
  143. + .read_probe = stl_probe,
  144. + .read_header = stl_read_header,
  145. + .read_packet = stl_read_packet,
  146. + .read_seek2 = stl_read_seek,
  147. + .read_close = stl_read_close,
  148. + .extensions = "stl",
  149. +};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement