You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

647 lines
15KB

  1. /*
  2. * Ogg bitstream support
  3. * Luca Barbato <lu_zero@gentoo.org>
  4. * Based on tcvp implementation
  5. *
  6. */
  7. /**
  8. Copyright (C) 2005 Michael Ahlberg, Måns Rullgård
  9. Permission is hereby granted, free of charge, to any person
  10. obtaining a copy of this software and associated documentation
  11. files (the "Software"), to deal in the Software without
  12. restriction, including without limitation the rights to use, copy,
  13. modify, merge, publish, distribute, sublicense, and/or sell copies
  14. of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:
  16. The above copyright notice and this permission notice shall be
  17. included in all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  22. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  23. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. DEALINGS IN THE SOFTWARE.
  26. **/
  27. #include <stdio.h>
  28. #include "oggdec.h"
  29. #include "avformat.h"
  30. #include "vorbiscomment.h"
  31. #define MAX_PAGE_SIZE 65307
  32. #define DECODER_BUFFER_SIZE MAX_PAGE_SIZE
  33. static const struct ogg_codec * const ogg_codecs[] = {
  34. &ff_skeleton_codec,
  35. &ff_dirac_codec,
  36. &ff_speex_codec,
  37. &ff_vorbis_codec,
  38. &ff_theora_codec,
  39. &ff_flac_codec,
  40. &ff_old_dirac_codec,
  41. &ff_old_flac_codec,
  42. &ff_ogm_video_codec,
  43. &ff_ogm_audio_codec,
  44. &ff_ogm_text_codec,
  45. &ff_ogm_old_codec,
  46. NULL
  47. };
  48. //FIXME We could avoid some structure duplication
  49. static int
  50. ogg_save (AVFormatContext * s)
  51. {
  52. struct ogg *ogg = s->priv_data;
  53. struct ogg_state *ost =
  54. av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
  55. int i;
  56. ost->pos = url_ftell (s->pb);
  57. ost->curidx = ogg->curidx;
  58. ost->next = ogg->state;
  59. ost->nstreams = ogg->nstreams;
  60. memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
  61. for (i = 0; i < ogg->nstreams; i++){
  62. struct ogg_stream *os = ogg->streams + i;
  63. os->buf = av_malloc (os->bufsize);
  64. memset (os->buf, 0, os->bufsize);
  65. memcpy (os->buf, ost->streams[i].buf, os->bufpos);
  66. }
  67. ogg->state = ost;
  68. return 0;
  69. }
  70. static int
  71. ogg_restore (AVFormatContext * s, int discard)
  72. {
  73. struct ogg *ogg = s->priv_data;
  74. ByteIOContext *bc = s->pb;
  75. struct ogg_state *ost = ogg->state;
  76. int i;
  77. if (!ost)
  78. return 0;
  79. ogg->state = ost->next;
  80. if (!discard){
  81. for (i = 0; i < ogg->nstreams; i++)
  82. av_free (ogg->streams[i].buf);
  83. url_fseek (bc, ost->pos, SEEK_SET);
  84. ogg->curidx = ost->curidx;
  85. ogg->nstreams = ost->nstreams;
  86. memcpy(ogg->streams, ost->streams,
  87. ost->nstreams * sizeof(*ogg->streams));
  88. }
  89. av_free (ost);
  90. return 0;
  91. }
  92. static int
  93. ogg_reset (struct ogg * ogg)
  94. {
  95. int i;
  96. for (i = 0; i < ogg->nstreams; i++){
  97. struct ogg_stream *os = ogg->streams + i;
  98. os->bufpos = 0;
  99. os->pstart = 0;
  100. os->psize = 0;
  101. os->granule = -1;
  102. os->lastpts = AV_NOPTS_VALUE;
  103. os->lastdts = AV_NOPTS_VALUE;
  104. os->sync_pos = -1;
  105. os->page_pos = 0;
  106. os->nsegs = 0;
  107. os->segp = 0;
  108. os->incomplete = 0;
  109. }
  110. ogg->curidx = -1;
  111. return 0;
  112. }
  113. static const struct ogg_codec *
  114. ogg_find_codec (uint8_t * buf, int size)
  115. {
  116. int i;
  117. for (i = 0; ogg_codecs[i]; i++)
  118. if (size >= ogg_codecs[i]->magicsize &&
  119. !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
  120. return ogg_codecs[i];
  121. return NULL;
  122. }
  123. static int
  124. ogg_new_stream (AVFormatContext * s, uint32_t serial)
  125. {
  126. struct ogg *ogg = s->priv_data;
  127. int idx = ogg->nstreams++;
  128. AVStream *st;
  129. struct ogg_stream *os;
  130. ogg->streams = av_realloc (ogg->streams,
  131. ogg->nstreams * sizeof (*ogg->streams));
  132. memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
  133. os = ogg->streams + idx;
  134. os->serial = serial;
  135. os->bufsize = DECODER_BUFFER_SIZE;
  136. os->buf = av_malloc(os->bufsize);
  137. os->header = -1;
  138. st = av_new_stream (s, idx);
  139. if (!st)
  140. return AVERROR(ENOMEM);
  141. av_set_pts_info(st, 64, 1, 1000000);
  142. return idx;
  143. }
  144. static int
  145. ogg_new_buf(struct ogg *ogg, int idx)
  146. {
  147. struct ogg_stream *os = ogg->streams + idx;
  148. uint8_t *nb = av_malloc(os->bufsize);
  149. int size = os->bufpos - os->pstart;
  150. if(os->buf){
  151. memcpy(nb, os->buf + os->pstart, size);
  152. av_free(os->buf);
  153. }
  154. os->buf = nb;
  155. os->bufpos = size;
  156. os->pstart = 0;
  157. return 0;
  158. }
  159. static int
  160. ogg_read_page (AVFormatContext * s, int *str)
  161. {
  162. ByteIOContext *bc = s->pb;
  163. struct ogg *ogg = s->priv_data;
  164. struct ogg_stream *os;
  165. int i = 0;
  166. int flags, nsegs;
  167. uint64_t gp;
  168. uint32_t serial;
  169. uint32_t seq;
  170. uint32_t crc;
  171. int size, idx;
  172. uint8_t sync[4];
  173. int sp = 0;
  174. if (get_buffer (bc, sync, 4) < 4)
  175. return -1;
  176. do{
  177. int c;
  178. if (sync[sp & 3] == 'O' &&
  179. sync[(sp + 1) & 3] == 'g' &&
  180. sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
  181. break;
  182. c = url_fgetc (bc);
  183. if (c < 0)
  184. return -1;
  185. sync[sp++ & 3] = c;
  186. }while (i++ < MAX_PAGE_SIZE);
  187. if (i >= MAX_PAGE_SIZE){
  188. av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
  189. return -1;
  190. }
  191. if (url_fgetc (bc) != 0) /* version */
  192. return -1;
  193. flags = url_fgetc (bc);
  194. gp = get_le64 (bc);
  195. serial = get_le32 (bc);
  196. seq = get_le32 (bc);
  197. crc = get_le32 (bc);
  198. nsegs = url_fgetc (bc);
  199. idx = ogg_find_stream (ogg, serial);
  200. if (idx < 0){
  201. idx = ogg_new_stream (s, serial);
  202. if (idx < 0)
  203. return -1;
  204. }
  205. os = ogg->streams + idx;
  206. os->page_pos = url_ftell(bc) - 27;
  207. if(os->psize > 0)
  208. ogg_new_buf(ogg, idx);
  209. if (get_buffer (bc, os->segments, nsegs) < nsegs)
  210. return -1;
  211. os->nsegs = nsegs;
  212. os->segp = 0;
  213. size = 0;
  214. for (i = 0; i < nsegs; i++)
  215. size += os->segments[i];
  216. if (flags & OGG_FLAG_CONT || os->incomplete){
  217. if (!os->psize){
  218. while (os->segp < os->nsegs){
  219. int seg = os->segments[os->segp++];
  220. os->pstart += seg;
  221. if (seg < 255)
  222. break;
  223. }
  224. os->sync_pos = os->page_pos;
  225. }
  226. }else{
  227. os->psize = 0;
  228. os->sync_pos = os->page_pos;
  229. }
  230. if (os->bufsize - os->bufpos < size){
  231. uint8_t *nb = av_malloc (os->bufsize *= 2);
  232. memcpy (nb, os->buf, os->bufpos);
  233. av_free (os->buf);
  234. os->buf = nb;
  235. }
  236. if (get_buffer (bc, os->buf + os->bufpos, size) < size)
  237. return -1;
  238. os->bufpos += size;
  239. os->granule = gp;
  240. os->flags = flags;
  241. if (str)
  242. *str = idx;
  243. return 0;
  244. }
  245. static int
  246. ogg_packet (AVFormatContext * s, int *str, int *dstart, int *dsize, int64_t *fpos)
  247. {
  248. struct ogg *ogg = s->priv_data;
  249. int idx, i;
  250. struct ogg_stream *os;
  251. int complete = 0;
  252. int segp = 0, psize = 0;
  253. #if 0
  254. av_log (s, AV_LOG_DEBUG, "ogg_packet: curidx=%i\n", ogg->curidx);
  255. #endif
  256. do{
  257. idx = ogg->curidx;
  258. while (idx < 0){
  259. if (ogg_read_page (s, &idx) < 0)
  260. return -1;
  261. }
  262. os = ogg->streams + idx;
  263. #if 0
  264. av_log (s, AV_LOG_DEBUG,
  265. "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
  266. idx, os->pstart, os->psize, os->segp, os->nsegs);
  267. #endif
  268. if (!os->codec){
  269. if (os->header < 0){
  270. os->codec = ogg_find_codec (os->buf, os->bufpos);
  271. if (!os->codec){
  272. os->header = 0;
  273. return 0;
  274. }
  275. }else{
  276. return 0;
  277. }
  278. }
  279. segp = os->segp;
  280. psize = os->psize;
  281. while (os->segp < os->nsegs){
  282. int ss = os->segments[os->segp++];
  283. os->psize += ss;
  284. if (ss < 255){
  285. complete = 1;
  286. break;
  287. }
  288. }
  289. if (!complete && os->segp == os->nsegs){
  290. ogg->curidx = -1;
  291. os->incomplete = 1;
  292. }
  293. }while (!complete);
  294. #if 0
  295. av_log (s, AV_LOG_DEBUG,
  296. "ogg_packet: idx %i, frame size %i, start %i\n",
  297. idx, os->psize, os->pstart);
  298. #endif
  299. if (os->granule == -1)
  300. av_log(s, AV_LOG_WARNING, "Page at %"PRId64" is missing granule\n", os->page_pos);
  301. ogg->curidx = idx;
  302. os->incomplete = 0;
  303. if (os->header) {
  304. os->header = os->codec->header (s, idx);
  305. if (!os->header){
  306. os->segp = segp;
  307. os->psize = psize;
  308. if (!ogg->headers)
  309. s->data_offset = os->sync_pos;
  310. ogg->headers = 1;
  311. }else{
  312. os->pstart += os->psize;
  313. os->psize = 0;
  314. }
  315. } else {
  316. os->pflags = 0;
  317. os->pduration = 0;
  318. if (os->codec && os->codec->packet)
  319. os->codec->packet (s, idx);
  320. if (str)
  321. *str = idx;
  322. if (dstart)
  323. *dstart = os->pstart;
  324. if (dsize)
  325. *dsize = os->psize;
  326. if (fpos)
  327. *fpos = os->sync_pos;
  328. os->pstart += os->psize;
  329. os->psize = 0;
  330. os->sync_pos = os->page_pos;
  331. }
  332. // determine whether there are more complete packets in this page
  333. // if not, the page's granule will apply to this packet
  334. os->page_end = 1;
  335. for (i = os->segp; i < os->nsegs; i++)
  336. if (os->segments[i] < 255) {
  337. os->page_end = 0;
  338. break;
  339. }
  340. if (os->segp == os->nsegs)
  341. ogg->curidx = -1;
  342. return 0;
  343. }
  344. static int
  345. ogg_get_headers (AVFormatContext * s)
  346. {
  347. struct ogg *ogg = s->priv_data;
  348. do{
  349. if (ogg_packet (s, NULL, NULL, NULL, NULL) < 0)
  350. return -1;
  351. }while (!ogg->headers);
  352. #if 0
  353. av_log (s, AV_LOG_DEBUG, "found headers\n");
  354. #endif
  355. return 0;
  356. }
  357. static int
  358. ogg_get_length (AVFormatContext * s)
  359. {
  360. struct ogg *ogg = s->priv_data;
  361. int i;
  362. int64_t size, end;
  363. if(url_is_streamed(s->pb))
  364. return 0;
  365. // already set
  366. if (s->duration != AV_NOPTS_VALUE)
  367. return 0;
  368. size = url_fsize(s->pb);
  369. if(size < 0)
  370. return 0;
  371. end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: 0;
  372. ogg_save (s);
  373. url_fseek (s->pb, end, SEEK_SET);
  374. while (!ogg_read_page (s, &i)){
  375. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  376. ogg->streams[i].codec) {
  377. s->streams[i]->duration =
  378. ogg_gptopts (s, i, ogg->streams[i].granule, NULL);
  379. if (s->streams[i]->start_time != AV_NOPTS_VALUE)
  380. s->streams[i]->duration -= s->streams[i]->start_time;
  381. }
  382. }
  383. ogg_restore (s, 0);
  384. return 0;
  385. }
  386. static int
  387. ogg_read_header (AVFormatContext * s, AVFormatParameters * ap)
  388. {
  389. struct ogg *ogg = s->priv_data;
  390. int i;
  391. ogg->curidx = -1;
  392. //linear headers seek from start
  393. if (ogg_get_headers (s) < 0){
  394. return -1;
  395. }
  396. for (i = 0; i < ogg->nstreams; i++)
  397. if (ogg->streams[i].header < 0)
  398. ogg->streams[i].codec = NULL;
  399. //linear granulepos seek from end
  400. ogg_get_length (s);
  401. //fill the extradata in the per codec callbacks
  402. return 0;
  403. }
  404. static int64_t ogg_calc_pts(AVFormatContext *s, int idx, int64_t *dts)
  405. {
  406. struct ogg *ogg = s->priv_data;
  407. struct ogg_stream *os = ogg->streams + idx;
  408. int64_t pts = AV_NOPTS_VALUE;
  409. if (dts)
  410. *dts = AV_NOPTS_VALUE;
  411. if (os->lastpts != AV_NOPTS_VALUE) {
  412. pts = os->lastpts;
  413. os->lastpts = AV_NOPTS_VALUE;
  414. }
  415. if (os->lastdts != AV_NOPTS_VALUE) {
  416. if (dts)
  417. *dts = os->lastdts;
  418. os->lastdts = AV_NOPTS_VALUE;
  419. }
  420. if (os->page_end) {
  421. if (os->granule != -1LL) {
  422. if (os->codec && os->codec->granule_is_start)
  423. pts = ogg_gptopts(s, idx, os->granule, dts);
  424. else
  425. os->lastpts = ogg_gptopts(s, idx, os->granule, &os->lastdts);
  426. os->granule = -1LL;
  427. }
  428. }
  429. return pts;
  430. }
  431. static int
  432. ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
  433. {
  434. struct ogg *ogg;
  435. struct ogg_stream *os;
  436. int idx = -1;
  437. int pstart, psize;
  438. int64_t fpos, pts, dts;
  439. //Get an ogg packet
  440. retry:
  441. do{
  442. if (ogg_packet (s, &idx, &pstart, &psize, &fpos) < 0)
  443. return AVERROR(EIO);
  444. }while (idx < 0 || !s->streams[idx]);
  445. ogg = s->priv_data;
  446. os = ogg->streams + idx;
  447. // pflags might not be set until after this
  448. pts = ogg_calc_pts(s, idx, &dts);
  449. if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
  450. goto retry;
  451. os->keyframe_seek = 0;
  452. //Alloc a pkt
  453. if (av_new_packet (pkt, psize) < 0)
  454. return AVERROR(EIO);
  455. pkt->stream_index = idx;
  456. memcpy (pkt->data, os->buf + pstart, psize);
  457. pkt->pts = pts;
  458. pkt->dts = dts;
  459. pkt->flags = os->pflags;
  460. pkt->duration = os->pduration;
  461. pkt->pos = fpos;
  462. return psize;
  463. }
  464. static int
  465. ogg_read_close (AVFormatContext * s)
  466. {
  467. struct ogg *ogg = s->priv_data;
  468. int i;
  469. for (i = 0; i < ogg->nstreams; i++){
  470. av_free (ogg->streams[i].buf);
  471. av_free (ogg->streams[i].private);
  472. }
  473. av_free (ogg->streams);
  474. return 0;
  475. }
  476. static int64_t
  477. ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
  478. int64_t pos_limit)
  479. {
  480. struct ogg *ogg = s->priv_data;
  481. struct ogg_stream *os = ogg->streams + stream_index;
  482. ByteIOContext *bc = s->pb;
  483. int64_t pts = AV_NOPTS_VALUE;
  484. int i;
  485. url_fseek(bc, *pos_arg, SEEK_SET);
  486. ogg_reset(ogg);
  487. while (url_ftell(bc) < pos_limit && !ogg_packet(s, &i, NULL, NULL, pos_arg)) {
  488. if (i == stream_index) {
  489. pts = ogg_calc_pts(s, i, NULL);
  490. if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
  491. pts = AV_NOPTS_VALUE;
  492. }
  493. if (pts != AV_NOPTS_VALUE)
  494. break;
  495. }
  496. ogg_reset(ogg);
  497. return pts;
  498. }
  499. static int ogg_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  500. {
  501. struct ogg *ogg = s->priv_data;
  502. struct ogg_stream *os = ogg->streams + stream_index;
  503. int ret;
  504. // Try seeking to a keyframe first. If this fails (very possible),
  505. // av_seek_frame will fall back to ignoring keyframes
  506. if (s->streams[stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO
  507. && !(flags & AVSEEK_FLAG_ANY))
  508. os->keyframe_seek = 1;
  509. ret = av_seek_frame_binary(s, stream_index, timestamp, flags);
  510. if (ret < 0)
  511. os->keyframe_seek = 0;
  512. return ret;
  513. }
  514. static int ogg_probe(AVProbeData *p)
  515. {
  516. if (p->buf[0] == 'O' && p->buf[1] == 'g' &&
  517. p->buf[2] == 'g' && p->buf[3] == 'S' &&
  518. p->buf[4] == 0x0 && p->buf[5] <= 0x7 )
  519. return AVPROBE_SCORE_MAX;
  520. else
  521. return 0;
  522. }
  523. AVInputFormat ogg_demuxer = {
  524. "ogg",
  525. NULL_IF_CONFIG_SMALL("Ogg"),
  526. sizeof (struct ogg),
  527. ogg_probe,
  528. ogg_read_header,
  529. ogg_read_packet,
  530. ogg_read_close,
  531. ogg_read_seek,
  532. ogg_read_timestamp,
  533. .extensions = "ogg",
  534. .metadata_conv = ff_vorbiscomment_metadata_conv,
  535. .flags = AVFMT_GENERIC_INDEX,
  536. };