Advertisement
Guest User

Untitled

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