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.

666 lines
16KB

  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 = 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
  71. ogg_restore (AVFormatContext * s, int discard)
  72. {
  73. struct ogg *ogg = s->priv_data;
  74. AVIOContext *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. avio_seek (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. AVIOContext *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 (avio_read (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 = avio_r8(bc);
  183. if (bc->eof_reached)
  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 (avio_r8(bc) != 0) /* version */
  192. return -1;
  193. flags = avio_r8(bc);
  194. gp = avio_rl64 (bc);
  195. serial = avio_rl32 (bc);
  196. seq = avio_rl32 (bc);
  197. crc = avio_rl32 (bc);
  198. nsegs = avio_r8(bc);
  199. idx = ogg_find_stream (ogg, serial);
  200. if (idx < 0){
  201. if (ogg->headers) {
  202. int n;
  203. for (n = 0; n < ogg->nstreams; n++) {
  204. av_freep(&ogg->streams[n].buf);
  205. av_freep(&ogg->streams[n].private);
  206. }
  207. ogg->curidx = -1;
  208. ogg->nstreams = 0;
  209. }
  210. idx = ogg_new_stream (s, serial);
  211. if (idx < 0)
  212. return -1;
  213. }
  214. os = ogg->streams + idx;
  215. os->page_pos = avio_tell(bc) - 27;
  216. if(os->psize > 0)
  217. ogg_new_buf(ogg, idx);
  218. if (avio_read (bc, os->segments, nsegs) < nsegs)
  219. return -1;
  220. os->nsegs = nsegs;
  221. os->segp = 0;
  222. size = 0;
  223. for (i = 0; i < nsegs; i++)
  224. size += os->segments[i];
  225. if (flags & OGG_FLAG_CONT || os->incomplete){
  226. if (!os->psize){
  227. while (os->segp < os->nsegs){
  228. int seg = os->segments[os->segp++];
  229. os->pstart += seg;
  230. if (seg < 255)
  231. break;
  232. }
  233. os->sync_pos = os->page_pos;
  234. }
  235. }else{
  236. os->psize = 0;
  237. os->sync_pos = os->page_pos;
  238. }
  239. if (os->bufsize - os->bufpos < size){
  240. uint8_t *nb = av_malloc (os->bufsize *= 2);
  241. memcpy (nb, os->buf, os->bufpos);
  242. av_free (os->buf);
  243. os->buf = nb;
  244. }
  245. if (avio_read (bc, os->buf + os->bufpos, size) < size)
  246. return -1;
  247. os->bufpos += size;
  248. os->granule = gp;
  249. os->flags = flags;
  250. if (str)
  251. *str = idx;
  252. return 0;
  253. }
  254. static int
  255. ogg_packet (AVFormatContext * s, int *str, int *dstart, int *dsize, int64_t *fpos)
  256. {
  257. struct ogg *ogg = s->priv_data;
  258. int idx, i;
  259. struct ogg_stream *os;
  260. int complete = 0;
  261. int segp = 0, psize = 0;
  262. av_dlog(s, AV_LOG_DEBUG, "ogg_packet: curidx=%i\n", ogg->curidx);
  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. av_dlog(s, AV_LOG_DEBUG,
  271. "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
  272. idx, os->pstart, os->psize, os->segp, os->nsegs);
  273. if (!os->codec){
  274. if (os->header < 0){
  275. os->codec = ogg_find_codec (os->buf, os->bufpos);
  276. if (!os->codec){
  277. os->header = 0;
  278. return 0;
  279. }
  280. }else{
  281. return 0;
  282. }
  283. }
  284. segp = os->segp;
  285. psize = os->psize;
  286. while (os->segp < os->nsegs){
  287. int ss = os->segments[os->segp++];
  288. os->psize += ss;
  289. if (ss < 255){
  290. complete = 1;
  291. break;
  292. }
  293. }
  294. if (!complete && os->segp == os->nsegs){
  295. ogg->curidx = -1;
  296. os->incomplete = 1;
  297. }
  298. }while (!complete);
  299. av_dlog(s, AV_LOG_DEBUG,
  300. "ogg_packet: idx %i, frame size %i, start %i\n",
  301. idx, os->psize, os->pstart);
  302. if (os->granule == -1)
  303. av_log(s, AV_LOG_WARNING, "Page at %"PRId64" is missing granule\n", os->page_pos);
  304. ogg->curidx = idx;
  305. os->incomplete = 0;
  306. if (os->header) {
  307. os->header = os->codec->header (s, idx);
  308. if (!os->header){
  309. os->segp = segp;
  310. os->psize = psize;
  311. // We have reached the first non-header packet in this stream.
  312. // Unfortunately more header packets may still follow for others,
  313. // so we reset this later unless we are done with the headers
  314. // for all streams.
  315. ogg->headers = 1;
  316. // Update the header state for all streams and
  317. // compute the data_offset.
  318. if (!s->data_offset)
  319. s->data_offset = os->sync_pos;
  320. for (i = 0; i < ogg->nstreams; i++) {
  321. struct ogg_stream *cur_os = ogg->streams + i;
  322. if (cur_os->header > 0)
  323. ogg->headers = 0;
  324. // if we have a partial non-header packet, its start is
  325. // obviously at or after the data start
  326. if (cur_os->incomplete)
  327. s->data_offset = FFMIN(s->data_offset, cur_os->sync_pos);
  328. }
  329. }else{
  330. os->pstart += os->psize;
  331. os->psize = 0;
  332. }
  333. } else {
  334. os->pflags = 0;
  335. os->pduration = 0;
  336. if (os->codec && os->codec->packet)
  337. os->codec->packet (s, idx);
  338. if (str)
  339. *str = idx;
  340. if (dstart)
  341. *dstart = os->pstart;
  342. if (dsize)
  343. *dsize = os->psize;
  344. if (fpos)
  345. *fpos = os->sync_pos;
  346. os->pstart += os->psize;
  347. os->psize = 0;
  348. os->sync_pos = os->page_pos;
  349. }
  350. // determine whether there are more complete packets in this page
  351. // if not, the page's granule will apply to this packet
  352. os->page_end = 1;
  353. for (i = os->segp; i < os->nsegs; i++)
  354. if (os->segments[i] < 255) {
  355. os->page_end = 0;
  356. break;
  357. }
  358. if (os->segp == os->nsegs)
  359. ogg->curidx = -1;
  360. return 0;
  361. }
  362. static int
  363. 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. av_dlog(s, AV_LOG_DEBUG, "found headers\n");
  371. return 0;
  372. }
  373. static int
  374. ogg_get_length (AVFormatContext * s)
  375. {
  376. struct ogg *ogg = s->priv_data;
  377. int i;
  378. int64_t size, end;
  379. if(!s->pb->seekable)
  380. return 0;
  381. // already set
  382. if (s->duration != AV_NOPTS_VALUE)
  383. return 0;
  384. size = avio_size(s->pb);
  385. if(size < 0)
  386. return 0;
  387. end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: 0;
  388. ogg_save (s);
  389. avio_seek (s->pb, end, SEEK_SET);
  390. while (!ogg_read_page (s, &i)){
  391. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  392. ogg->streams[i].codec) {
  393. s->streams[i]->duration =
  394. ogg_gptopts (s, i, ogg->streams[i].granule, NULL);
  395. if (s->streams[i]->start_time != AV_NOPTS_VALUE)
  396. s->streams[i]->duration -= s->streams[i]->start_time;
  397. }
  398. }
  399. ogg_restore (s, 0);
  400. return 0;
  401. }
  402. static int
  403. ogg_read_header (AVFormatContext * s, AVFormatParameters * ap)
  404. {
  405. struct ogg *ogg = s->priv_data;
  406. int i;
  407. ogg->curidx = -1;
  408. //linear headers seek from start
  409. if (ogg_get_headers (s) < 0){
  410. return -1;
  411. }
  412. for (i = 0; i < ogg->nstreams; i++)
  413. if (ogg->streams[i].header < 0)
  414. ogg->streams[i].codec = NULL;
  415. //linear granulepos seek from end
  416. ogg_get_length (s);
  417. //fill the extradata in the per codec callbacks
  418. return 0;
  419. }
  420. static int64_t ogg_calc_pts(AVFormatContext *s, int idx, int64_t *dts)
  421. {
  422. struct ogg *ogg = s->priv_data;
  423. struct ogg_stream *os = ogg->streams + idx;
  424. int64_t pts = AV_NOPTS_VALUE;
  425. if (dts)
  426. *dts = AV_NOPTS_VALUE;
  427. if (os->lastpts != AV_NOPTS_VALUE) {
  428. pts = os->lastpts;
  429. os->lastpts = AV_NOPTS_VALUE;
  430. }
  431. if (os->lastdts != AV_NOPTS_VALUE) {
  432. if (dts)
  433. *dts = os->lastdts;
  434. os->lastdts = AV_NOPTS_VALUE;
  435. }
  436. if (os->page_end) {
  437. if (os->granule != -1LL) {
  438. if (os->codec && os->codec->granule_is_start)
  439. pts = ogg_gptopts(s, idx, os->granule, dts);
  440. else
  441. os->lastpts = ogg_gptopts(s, idx, os->granule, &os->lastdts);
  442. os->granule = -1LL;
  443. }
  444. }
  445. return pts;
  446. }
  447. static int
  448. ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
  449. {
  450. struct ogg *ogg;
  451. struct ogg_stream *os;
  452. int idx = -1;
  453. int pstart, psize;
  454. int64_t fpos, pts, dts;
  455. //Get an ogg packet
  456. retry:
  457. do{
  458. if (ogg_packet (s, &idx, &pstart, &psize, &fpos) < 0)
  459. return AVERROR(EIO);
  460. }while (idx < 0 || !s->streams[idx]);
  461. ogg = s->priv_data;
  462. os = ogg->streams + idx;
  463. // pflags might not be set until after this
  464. pts = ogg_calc_pts(s, idx, &dts);
  465. if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
  466. goto retry;
  467. os->keyframe_seek = 0;
  468. //Alloc a pkt
  469. if (av_new_packet (pkt, psize) < 0)
  470. return AVERROR(EIO);
  471. pkt->stream_index = idx;
  472. memcpy (pkt->data, os->buf + pstart, psize);
  473. pkt->pts = pts;
  474. pkt->dts = dts;
  475. pkt->flags = os->pflags;
  476. pkt->duration = os->pduration;
  477. pkt->pos = fpos;
  478. return psize;
  479. }
  480. static int
  481. ogg_read_close (AVFormatContext * s)
  482. {
  483. struct ogg *ogg = s->priv_data;
  484. int i;
  485. for (i = 0; i < ogg->nstreams; i++){
  486. av_free (ogg->streams[i].buf);
  487. av_free (ogg->streams[i].private);
  488. }
  489. av_free (ogg->streams);
  490. return 0;
  491. }
  492. static int64_t
  493. ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
  494. int64_t pos_limit)
  495. {
  496. struct ogg *ogg = s->priv_data;
  497. struct ogg_stream *os = ogg->streams + stream_index;
  498. AVIOContext *bc = s->pb;
  499. int64_t pts = AV_NOPTS_VALUE;
  500. int i;
  501. avio_seek(bc, *pos_arg, SEEK_SET);
  502. ogg_reset(ogg);
  503. while (avio_tell(bc) < pos_limit && !ogg_packet(s, &i, NULL, NULL, pos_arg)) {
  504. if (i == stream_index) {
  505. pts = ogg_calc_pts(s, i, NULL);
  506. if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
  507. pts = AV_NOPTS_VALUE;
  508. }
  509. if (pts != AV_NOPTS_VALUE)
  510. break;
  511. }
  512. ogg_reset(ogg);
  513. return pts;
  514. }
  515. static int ogg_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  516. {
  517. struct ogg *ogg = s->priv_data;
  518. struct ogg_stream *os = ogg->streams + stream_index;
  519. int ret;
  520. // Try seeking to a keyframe first. If this fails (very possible),
  521. // av_seek_frame will fall back to ignoring keyframes
  522. if (s->streams[stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO
  523. && !(flags & AVSEEK_FLAG_ANY))
  524. os->keyframe_seek = 1;
  525. ret = av_seek_frame_binary(s, stream_index, timestamp, flags);
  526. if (ret < 0)
  527. os->keyframe_seek = 0;
  528. return ret;
  529. }
  530. static int ogg_probe(AVProbeData *p)
  531. {
  532. if (p->buf[0] == 'O' && p->buf[1] == 'g' &&
  533. p->buf[2] == 'g' && p->buf[3] == 'S' &&
  534. p->buf[4] == 0x0 && p->buf[5] <= 0x7 )
  535. return AVPROBE_SCORE_MAX;
  536. else
  537. return 0;
  538. }
  539. AVInputFormat ff_ogg_demuxer = {
  540. "ogg",
  541. NULL_IF_CONFIG_SMALL("Ogg"),
  542. sizeof (struct ogg),
  543. ogg_probe,
  544. ogg_read_header,
  545. ogg_read_packet,
  546. ogg_read_close,
  547. ogg_read_seek,
  548. ogg_read_timestamp,
  549. .extensions = "ogg",
  550. .flags = AVFMT_GENERIC_INDEX,
  551. };