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.

670 lines
17KB

  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_celt_codec,
  41. &ff_old_dirac_codec,
  42. &ff_old_flac_codec,
  43. &ff_ogm_video_codec,
  44. &ff_ogm_audio_codec,
  45. &ff_ogm_text_codec,
  46. &ff_ogm_old_codec,
  47. NULL
  48. };
  49. //FIXME We could avoid some structure duplication
  50. static int 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 = avio_tell (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 ogg_restore(AVFormatContext *s, int discard)
  71. {
  72. struct ogg *ogg = s->priv_data;
  73. AVIOContext *bc = s->pb;
  74. struct ogg_state *ost = ogg->state;
  75. int i;
  76. if (!ost)
  77. return 0;
  78. ogg->state = ost->next;
  79. if (!discard){
  80. for (i = 0; i < ogg->nstreams; i++)
  81. av_free (ogg->streams[i].buf);
  82. avio_seek (bc, ost->pos, SEEK_SET);
  83. ogg->curidx = ost->curidx;
  84. ogg->nstreams = ost->nstreams;
  85. memcpy(ogg->streams, ost->streams,
  86. ost->nstreams * sizeof(*ogg->streams));
  87. }
  88. av_free (ost);
  89. return 0;
  90. }
  91. static int ogg_reset(struct ogg *ogg)
  92. {
  93. int i;
  94. for (i = 0; i < ogg->nstreams; i++){
  95. struct ogg_stream *os = ogg->streams + i;
  96. os->bufpos = 0;
  97. os->pstart = 0;
  98. os->psize = 0;
  99. os->granule = -1;
  100. os->lastpts = AV_NOPTS_VALUE;
  101. os->lastdts = AV_NOPTS_VALUE;
  102. os->sync_pos = -1;
  103. os->page_pos = 0;
  104. os->nsegs = 0;
  105. os->segp = 0;
  106. os->incomplete = 0;
  107. }
  108. ogg->curidx = -1;
  109. return 0;
  110. }
  111. static const struct ogg_codec *ogg_find_codec(uint8_t *buf, int size)
  112. {
  113. int i;
  114. for (i = 0; ogg_codecs[i]; i++)
  115. if (size >= ogg_codecs[i]->magicsize &&
  116. !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
  117. return ogg_codecs[i];
  118. return NULL;
  119. }
  120. static int ogg_new_stream(AVFormatContext *s, uint32_t serial, int new_avstream)
  121. {
  122. struct ogg *ogg = s->priv_data;
  123. int idx = ogg->nstreams++;
  124. AVStream *st;
  125. struct ogg_stream *os;
  126. ogg->streams = av_realloc (ogg->streams,
  127. ogg->nstreams * sizeof (*ogg->streams));
  128. memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
  129. os = ogg->streams + idx;
  130. os->serial = serial;
  131. os->bufsize = DECODER_BUFFER_SIZE;
  132. os->buf = av_malloc(os->bufsize);
  133. os->header = -1;
  134. if (new_avstream) {
  135. st = av_new_stream(s, idx);
  136. if (!st)
  137. return AVERROR(ENOMEM);
  138. av_set_pts_info(st, 64, 1, 1000000);
  139. }
  140. return idx;
  141. }
  142. static int ogg_new_buf(struct ogg *ogg, int idx)
  143. {
  144. struct ogg_stream *os = ogg->streams + idx;
  145. uint8_t *nb = av_malloc(os->bufsize);
  146. int size = os->bufpos - os->pstart;
  147. if(os->buf){
  148. memcpy(nb, os->buf + os->pstart, size);
  149. av_free(os->buf);
  150. }
  151. os->buf = nb;
  152. os->bufpos = size;
  153. os->pstart = 0;
  154. return 0;
  155. }
  156. static int ogg_read_page(AVFormatContext *s, int *str)
  157. {
  158. AVIOContext *bc = s->pb;
  159. struct ogg *ogg = s->priv_data;
  160. struct ogg_stream *os;
  161. int i = 0;
  162. int flags, nsegs;
  163. uint64_t gp;
  164. uint32_t serial;
  165. uint32_t seq;
  166. uint32_t crc;
  167. int size, idx;
  168. uint8_t sync[4];
  169. int sp = 0;
  170. if (avio_read (bc, sync, 4) < 4)
  171. return -1;
  172. do{
  173. int c;
  174. if (sync[sp & 3] == 'O' &&
  175. sync[(sp + 1) & 3] == 'g' &&
  176. sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
  177. break;
  178. c = avio_r8(bc);
  179. if (url_feof(bc))
  180. return -1;
  181. sync[sp++ & 3] = c;
  182. }while (i++ < MAX_PAGE_SIZE);
  183. if (i >= MAX_PAGE_SIZE){
  184. av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
  185. return -1;
  186. }
  187. if (avio_r8(bc) != 0) /* version */
  188. return -1;
  189. flags = avio_r8(bc);
  190. gp = avio_rl64 (bc);
  191. serial = avio_rl32 (bc);
  192. seq = avio_rl32 (bc);
  193. crc = avio_rl32 (bc);
  194. nsegs = avio_r8(bc);
  195. idx = ogg_find_stream (ogg, serial);
  196. if (idx < 0){
  197. if (ogg->headers) {
  198. int n;
  199. for (n = 0; n < ogg->nstreams; n++) {
  200. av_freep(&ogg->streams[n].buf);
  201. av_freep(&ogg->streams[n].private);
  202. }
  203. ogg->curidx = -1;
  204. ogg->nstreams = 0;
  205. idx = ogg_new_stream(s, serial, 0);
  206. } else {
  207. idx = ogg_new_stream(s, serial, 1);
  208. }
  209. if (idx < 0)
  210. return -1;
  211. }
  212. os = ogg->streams + idx;
  213. os->page_pos = avio_tell(bc) - 27;
  214. if(os->psize > 0)
  215. ogg_new_buf(ogg, idx);
  216. if (avio_read (bc, os->segments, nsegs) < nsegs)
  217. return -1;
  218. os->nsegs = nsegs;
  219. os->segp = 0;
  220. size = 0;
  221. for (i = 0; i < nsegs; i++)
  222. size += os->segments[i];
  223. if (flags & OGG_FLAG_CONT || os->incomplete){
  224. if (!os->psize){
  225. while (os->segp < os->nsegs){
  226. int seg = os->segments[os->segp++];
  227. os->pstart += seg;
  228. if (seg < 255)
  229. break;
  230. }
  231. os->sync_pos = os->page_pos;
  232. }
  233. }else{
  234. os->psize = 0;
  235. os->sync_pos = os->page_pos;
  236. }
  237. if (os->bufsize - os->bufpos < size){
  238. uint8_t *nb = av_malloc (os->bufsize *= 2);
  239. memcpy (nb, os->buf, os->bufpos);
  240. av_free (os->buf);
  241. os->buf = nb;
  242. }
  243. if (avio_read (bc, os->buf + os->bufpos, size) < size)
  244. return -1;
  245. os->bufpos += size;
  246. os->granule = gp;
  247. os->flags = flags;
  248. if (str)
  249. *str = idx;
  250. return 0;
  251. }
  252. static int ogg_packet(AVFormatContext *s, int *str, int *dstart, int *dsize,
  253. int64_t *fpos)
  254. {
  255. struct ogg *ogg = s->priv_data;
  256. int idx, i;
  257. struct ogg_stream *os;
  258. int complete = 0;
  259. int segp = 0, psize = 0;
  260. #if 0
  261. av_log (s, AV_LOG_DEBUG, "ogg_packet: curidx=%i\n", ogg->curidx);
  262. #endif
  263. do{
  264. idx = ogg->curidx;
  265. while (idx < 0){
  266. if (ogg_read_page (s, &idx) < 0)
  267. return -1;
  268. }
  269. os = ogg->streams + idx;
  270. #if 0
  271. av_log (s, AV_LOG_DEBUG,
  272. "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
  273. idx, os->pstart, os->psize, os->segp, os->nsegs);
  274. #endif
  275. if (!os->codec){
  276. if (os->header < 0){
  277. os->codec = ogg_find_codec (os->buf, os->bufpos);
  278. if (!os->codec){
  279. os->header = 0;
  280. return 0;
  281. }
  282. }else{
  283. return 0;
  284. }
  285. }
  286. segp = os->segp;
  287. psize = os->psize;
  288. while (os->segp < os->nsegs){
  289. int ss = os->segments[os->segp++];
  290. os->psize += ss;
  291. if (ss < 255){
  292. complete = 1;
  293. break;
  294. }
  295. }
  296. if (!complete && os->segp == os->nsegs){
  297. ogg->curidx = -1;
  298. os->incomplete = 1;
  299. }
  300. }while (!complete);
  301. #if 0
  302. av_log (s, AV_LOG_DEBUG,
  303. "ogg_packet: idx %i, frame size %i, start %i\n",
  304. idx, os->psize, os->pstart);
  305. #endif
  306. if (os->granule == -1)
  307. av_log(s, AV_LOG_WARNING, "Page at %"PRId64" is missing granule\n", os->page_pos);
  308. ogg->curidx = idx;
  309. os->incomplete = 0;
  310. if (os->header) {
  311. os->header = os->codec->header (s, idx);
  312. if (!os->header){
  313. os->segp = segp;
  314. os->psize = psize;
  315. // We have reached the first non-header packet in this stream.
  316. // Unfortunately more header packets may still follow for others,
  317. // but if we continue with header parsing we may lose data packets.
  318. ogg->headers = 1;
  319. // Update the header state for all streams and
  320. // compute the data_offset.
  321. if (!s->data_offset)
  322. s->data_offset = os->sync_pos;
  323. for (i = 0; i < ogg->nstreams; i++) {
  324. struct ogg_stream *cur_os = ogg->streams + i;
  325. // if we have a partial non-header packet, its start is
  326. // obviously at or after the data start
  327. if (cur_os->incomplete)
  328. s->data_offset = FFMIN(s->data_offset, cur_os->sync_pos);
  329. }
  330. }else{
  331. os->pstart += os->psize;
  332. os->psize = 0;
  333. }
  334. } else {
  335. os->pflags = 0;
  336. os->pduration = 0;
  337. if (os->codec && os->codec->packet)
  338. os->codec->packet (s, idx);
  339. if (str)
  340. *str = idx;
  341. if (dstart)
  342. *dstart = os->pstart;
  343. if (dsize)
  344. *dsize = os->psize;
  345. if (fpos)
  346. *fpos = os->sync_pos;
  347. os->pstart += os->psize;
  348. os->psize = 0;
  349. os->sync_pos = os->page_pos;
  350. }
  351. // determine whether there are more complete packets in this page
  352. // if not, the page's granule will apply to this packet
  353. os->page_end = 1;
  354. for (i = os->segp; i < os->nsegs; i++)
  355. if (os->segments[i] < 255) {
  356. os->page_end = 0;
  357. break;
  358. }
  359. if (os->segp == os->nsegs)
  360. ogg->curidx = -1;
  361. return 0;
  362. }
  363. static int ogg_get_headers(AVFormatContext *s)
  364. {
  365. struct ogg *ogg = s->priv_data;
  366. do{
  367. if (ogg_packet (s, NULL, NULL, NULL, NULL) < 0)
  368. return -1;
  369. }while (!ogg->headers);
  370. #if 0
  371. av_log (s, AV_LOG_DEBUG, "found headers\n");
  372. #endif
  373. return 0;
  374. }
  375. static int ogg_get_length(AVFormatContext *s)
  376. {
  377. struct ogg *ogg = s->priv_data;
  378. int i;
  379. int64_t size, end;
  380. if(!s->pb->seekable)
  381. return 0;
  382. // already set
  383. if (s->duration != AV_NOPTS_VALUE)
  384. return 0;
  385. size = avio_size(s->pb);
  386. if(size < 0)
  387. return 0;
  388. end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: 0;
  389. ogg_save (s);
  390. avio_seek (s->pb, end, SEEK_SET);
  391. while (!ogg_read_page (s, &i)){
  392. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  393. ogg->streams[i].codec) {
  394. s->streams[i]->duration =
  395. ogg_gptopts (s, i, ogg->streams[i].granule, NULL);
  396. if (s->streams[i]->start_time != AV_NOPTS_VALUE)
  397. s->streams[i]->duration -= s->streams[i]->start_time;
  398. }
  399. }
  400. ogg_restore (s, 0);
  401. ogg_save (s);
  402. avio_seek (s->pb, 0, SEEK_SET);
  403. while (!ogg_read_page (s, &i)){
  404. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  405. ogg->streams[i].codec) {
  406. s->streams[i]->duration -=
  407. ogg_gptopts (s, i, ogg->streams[i].granule, NULL);
  408. break;
  409. }
  410. }
  411. ogg_restore (s, 0);
  412. return 0;
  413. }
  414. static int ogg_read_header(AVFormatContext *s, AVFormatParameters *ap)
  415. {
  416. struct ogg *ogg = s->priv_data;
  417. int i;
  418. ogg->curidx = -1;
  419. //linear headers seek from start
  420. if (ogg_get_headers (s) < 0){
  421. return -1;
  422. }
  423. for (i = 0; i < ogg->nstreams; i++)
  424. if (ogg->streams[i].header < 0)
  425. ogg->streams[i].codec = NULL;
  426. //linear granulepos seek from end
  427. ogg_get_length (s);
  428. //fill the extradata in the per codec callbacks
  429. return 0;
  430. }
  431. static int64_t ogg_calc_pts(AVFormatContext *s, int idx, int64_t *dts)
  432. {
  433. struct ogg *ogg = s->priv_data;
  434. struct ogg_stream *os = ogg->streams + idx;
  435. int64_t pts = AV_NOPTS_VALUE;
  436. if (dts)
  437. *dts = AV_NOPTS_VALUE;
  438. if (os->lastpts != AV_NOPTS_VALUE) {
  439. pts = os->lastpts;
  440. os->lastpts = AV_NOPTS_VALUE;
  441. }
  442. if (os->lastdts != AV_NOPTS_VALUE) {
  443. if (dts)
  444. *dts = os->lastdts;
  445. os->lastdts = AV_NOPTS_VALUE;
  446. }
  447. if (os->page_end) {
  448. if (os->granule != -1LL) {
  449. if (os->codec && os->codec->granule_is_start)
  450. pts = ogg_gptopts(s, idx, os->granule, dts);
  451. else
  452. os->lastpts = ogg_gptopts(s, idx, os->granule, &os->lastdts);
  453. os->granule = -1LL;
  454. }
  455. }
  456. return pts;
  457. }
  458. static int ogg_read_packet(AVFormatContext *s, AVPacket *pkt)
  459. {
  460. struct ogg *ogg;
  461. struct ogg_stream *os;
  462. int idx = -1;
  463. int pstart, psize;
  464. int64_t fpos, pts, dts;
  465. //Get an ogg packet
  466. retry:
  467. do{
  468. if (ogg_packet (s, &idx, &pstart, &psize, &fpos) < 0)
  469. return AVERROR(EIO);
  470. }while (idx < 0 || !s->streams[idx]);
  471. ogg = s->priv_data;
  472. os = ogg->streams + idx;
  473. // pflags might not be set until after this
  474. pts = ogg_calc_pts(s, idx, &dts);
  475. if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
  476. goto retry;
  477. os->keyframe_seek = 0;
  478. //Alloc a pkt
  479. if (av_new_packet (pkt, psize) < 0)
  480. return AVERROR(EIO);
  481. pkt->stream_index = idx;
  482. memcpy (pkt->data, os->buf + pstart, psize);
  483. pkt->pts = pts;
  484. pkt->dts = dts;
  485. pkt->flags = os->pflags;
  486. pkt->duration = os->pduration;
  487. pkt->pos = fpos;
  488. return psize;
  489. }
  490. static int ogg_read_close(AVFormatContext *s)
  491. {
  492. struct ogg *ogg = s->priv_data;
  493. int i;
  494. for (i = 0; i < ogg->nstreams; i++){
  495. av_free (ogg->streams[i].buf);
  496. av_free (ogg->streams[i].private);
  497. }
  498. av_free (ogg->streams);
  499. return 0;
  500. }
  501. static int64_t ogg_read_timestamp(AVFormatContext *s, int stream_index,
  502. int64_t *pos_arg, int64_t pos_limit)
  503. {
  504. struct ogg *ogg = s->priv_data;
  505. struct ogg_stream *os = ogg->streams + stream_index;
  506. AVIOContext *bc = s->pb;
  507. int64_t pts = AV_NOPTS_VALUE;
  508. int i;
  509. avio_seek(bc, *pos_arg, SEEK_SET);
  510. ogg_reset(ogg);
  511. while (avio_tell(bc) < pos_limit && !ogg_packet(s, &i, NULL, NULL, pos_arg)) {
  512. if (i == stream_index) {
  513. pts = ogg_calc_pts(s, i, NULL);
  514. if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
  515. pts = AV_NOPTS_VALUE;
  516. }
  517. if (pts != AV_NOPTS_VALUE)
  518. break;
  519. }
  520. ogg_reset(ogg);
  521. return pts;
  522. }
  523. static int ogg_read_seek(AVFormatContext *s, int stream_index,
  524. int64_t timestamp, int flags)
  525. {
  526. struct ogg *ogg = s->priv_data;
  527. struct ogg_stream *os = ogg->streams + stream_index;
  528. int ret;
  529. // Try seeking to a keyframe first. If this fails (very possible),
  530. // av_seek_frame will fall back to ignoring keyframes
  531. if (s->streams[stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO
  532. && !(flags & AVSEEK_FLAG_ANY))
  533. os->keyframe_seek = 1;
  534. ret = av_seek_frame_binary(s, stream_index, timestamp, flags);
  535. if (ret < 0)
  536. os->keyframe_seek = 0;
  537. return ret;
  538. }
  539. static int ogg_probe(AVProbeData *p)
  540. {
  541. if (!memcmp("OggS", p->buf, 5) && p->buf[5] <= 0x7)
  542. return AVPROBE_SCORE_MAX;
  543. return 0;
  544. }
  545. AVInputFormat ff_ogg_demuxer = {
  546. .name = "ogg",
  547. .long_name = NULL_IF_CONFIG_SMALL("Ogg"),
  548. .priv_data_size = sizeof(struct ogg),
  549. .read_probe = ogg_probe,
  550. .read_header = ogg_read_header,
  551. .read_packet = ogg_read_packet,
  552. .read_close = ogg_read_close,
  553. .read_seek = ogg_read_seek,
  554. .read_timestamp = ogg_read_timestamp,
  555. .extensions = "ogg",
  556. .flags = AVFMT_GENERIC_INDEX,
  557. };