oggdec.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. idx = ogg_new_stream (s, serial);
  202. if (idx < 0)
  203. return -1;
  204. }
  205. os = ogg->streams + idx;
  206. os->page_pos = avio_tell(bc) - 27;
  207. if(os->psize > 0)
  208. ogg_new_buf(ogg, idx);
  209. if (avio_read (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 (avio_read (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. // We have reached the first non-header packet in this stream.
  309. // Unfortunately more header packets may still follow for others,
  310. // so we reset this later unless we are done with the headers
  311. // for all streams.
  312. ogg->headers = 1;
  313. // Update the header state for all streams and
  314. // compute the data_offset.
  315. if (!s->data_offset)
  316. s->data_offset = os->sync_pos;
  317. for (i = 0; i < ogg->nstreams; i++) {
  318. struct ogg_stream *cur_os = ogg->streams + i;
  319. if (cur_os->header > 0)
  320. ogg->headers = 0;
  321. // if we have a partial non-header packet, its start is
  322. // obviously at or after the data start
  323. if (cur_os->incomplete)
  324. s->data_offset = FFMIN(s->data_offset, cur_os->sync_pos);
  325. }
  326. }else{
  327. os->pstart += os->psize;
  328. os->psize = 0;
  329. }
  330. } else {
  331. os->pflags = 0;
  332. os->pduration = 0;
  333. if (os->codec && os->codec->packet)
  334. os->codec->packet (s, idx);
  335. if (str)
  336. *str = idx;
  337. if (dstart)
  338. *dstart = os->pstart;
  339. if (dsize)
  340. *dsize = os->psize;
  341. if (fpos)
  342. *fpos = os->sync_pos;
  343. os->pstart += os->psize;
  344. os->psize = 0;
  345. os->sync_pos = os->page_pos;
  346. }
  347. // determine whether there are more complete packets in this page
  348. // if not, the page's granule will apply to this packet
  349. os->page_end = 1;
  350. for (i = os->segp; i < os->nsegs; i++)
  351. if (os->segments[i] < 255) {
  352. os->page_end = 0;
  353. break;
  354. }
  355. if (os->segp == os->nsegs)
  356. ogg->curidx = -1;
  357. return 0;
  358. }
  359. static int
  360. ogg_get_headers (AVFormatContext * s)
  361. {
  362. struct ogg *ogg = s->priv_data;
  363. do{
  364. if (ogg_packet (s, NULL, NULL, NULL, NULL) < 0)
  365. return -1;
  366. }while (!ogg->headers);
  367. #if 0
  368. av_log (s, AV_LOG_DEBUG, "found headers\n");
  369. #endif
  370. return 0;
  371. }
  372. static int
  373. ogg_get_length (AVFormatContext * s)
  374. {
  375. struct ogg *ogg = s->priv_data;
  376. int i;
  377. int64_t size, end;
  378. if(url_is_streamed(s->pb))
  379. return 0;
  380. // already set
  381. if (s->duration != AV_NOPTS_VALUE)
  382. return 0;
  383. size = avio_size(s->pb);
  384. if(size < 0)
  385. return 0;
  386. end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: 0;
  387. ogg_save (s);
  388. avio_seek (s->pb, end, SEEK_SET);
  389. while (!ogg_read_page (s, &i)){
  390. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  391. ogg->streams[i].codec) {
  392. s->streams[i]->duration =
  393. ogg_gptopts (s, i, ogg->streams[i].granule, NULL);
  394. if (s->streams[i]->start_time != AV_NOPTS_VALUE)
  395. s->streams[i]->duration -= s->streams[i]->start_time;
  396. }
  397. }
  398. ogg_restore (s, 0);
  399. return 0;
  400. }
  401. static int
  402. ogg_read_header (AVFormatContext * s, AVFormatParameters * ap)
  403. {
  404. struct ogg *ogg = s->priv_data;
  405. int i;
  406. ogg->curidx = -1;
  407. //linear headers seek from start
  408. if (ogg_get_headers (s) < 0){
  409. return -1;
  410. }
  411. for (i = 0; i < ogg->nstreams; i++)
  412. if (ogg->streams[i].header < 0)
  413. ogg->streams[i].codec = NULL;
  414. //linear granulepos seek from end
  415. ogg_get_length (s);
  416. //fill the extradata in the per codec callbacks
  417. return 0;
  418. }
  419. static int64_t ogg_calc_pts(AVFormatContext *s, int idx, int64_t *dts)
  420. {
  421. struct ogg *ogg = s->priv_data;
  422. struct ogg_stream *os = ogg->streams + idx;
  423. int64_t pts = AV_NOPTS_VALUE;
  424. if (dts)
  425. *dts = AV_NOPTS_VALUE;
  426. if (os->lastpts != AV_NOPTS_VALUE) {
  427. pts = os->lastpts;
  428. os->lastpts = AV_NOPTS_VALUE;
  429. }
  430. if (os->lastdts != AV_NOPTS_VALUE) {
  431. if (dts)
  432. *dts = os->lastdts;
  433. os->lastdts = AV_NOPTS_VALUE;
  434. }
  435. if (os->page_end) {
  436. if (os->granule != -1LL) {
  437. if (os->codec && os->codec->granule_is_start)
  438. pts = ogg_gptopts(s, idx, os->granule, dts);
  439. else
  440. os->lastpts = ogg_gptopts(s, idx, os->granule, &os->lastdts);
  441. os->granule = -1LL;
  442. }
  443. }
  444. return pts;
  445. }
  446. static int
  447. ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
  448. {
  449. struct ogg *ogg;
  450. struct ogg_stream *os;
  451. int idx = -1;
  452. int pstart, psize;
  453. int64_t fpos, pts, dts;
  454. //Get an ogg packet
  455. retry:
  456. do{
  457. if (ogg_packet (s, &idx, &pstart, &psize, &fpos) < 0)
  458. return AVERROR(EIO);
  459. }while (idx < 0 || !s->streams[idx]);
  460. ogg = s->priv_data;
  461. os = ogg->streams + idx;
  462. // pflags might not be set until after this
  463. pts = ogg_calc_pts(s, idx, &dts);
  464. if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
  465. goto retry;
  466. os->keyframe_seek = 0;
  467. //Alloc a pkt
  468. if (av_new_packet (pkt, psize) < 0)
  469. return AVERROR(EIO);
  470. pkt->stream_index = idx;
  471. memcpy (pkt->data, os->buf + pstart, psize);
  472. pkt->pts = pts;
  473. pkt->dts = dts;
  474. pkt->flags = os->pflags;
  475. pkt->duration = os->pduration;
  476. pkt->pos = fpos;
  477. return psize;
  478. }
  479. static int
  480. ogg_read_close (AVFormatContext * s)
  481. {
  482. struct ogg *ogg = s->priv_data;
  483. int i;
  484. for (i = 0; i < ogg->nstreams; i++){
  485. av_free (ogg->streams[i].buf);
  486. av_free (ogg->streams[i].private);
  487. }
  488. av_free (ogg->streams);
  489. return 0;
  490. }
  491. static int64_t
  492. ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
  493. int64_t pos_limit)
  494. {
  495. struct ogg *ogg = s->priv_data;
  496. struct ogg_stream *os = ogg->streams + stream_index;
  497. AVIOContext *bc = s->pb;
  498. int64_t pts = AV_NOPTS_VALUE;
  499. int i;
  500. avio_seek(bc, *pos_arg, SEEK_SET);
  501. ogg_reset(ogg);
  502. while (avio_tell(bc) < pos_limit && !ogg_packet(s, &i, NULL, NULL, pos_arg)) {
  503. if (i == stream_index) {
  504. pts = ogg_calc_pts(s, i, NULL);
  505. if (os->keyframe_seek && !(os->pflags & AV_PKT_FLAG_KEY))
  506. pts = AV_NOPTS_VALUE;
  507. }
  508. if (pts != AV_NOPTS_VALUE)
  509. break;
  510. }
  511. ogg_reset(ogg);
  512. return pts;
  513. }
  514. static int ogg_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
  515. {
  516. struct ogg *ogg = s->priv_data;
  517. struct ogg_stream *os = ogg->streams + stream_index;
  518. int ret;
  519. // Try seeking to a keyframe first. If this fails (very possible),
  520. // av_seek_frame will fall back to ignoring keyframes
  521. if (s->streams[stream_index]->codec->codec_type == AVMEDIA_TYPE_VIDEO
  522. && !(flags & AVSEEK_FLAG_ANY))
  523. os->keyframe_seek = 1;
  524. ret = av_seek_frame_binary(s, stream_index, timestamp, flags);
  525. if (ret < 0)
  526. os->keyframe_seek = 0;
  527. return ret;
  528. }
  529. static int ogg_probe(AVProbeData *p)
  530. {
  531. if (p->buf[0] == 'O' && p->buf[1] == 'g' &&
  532. p->buf[2] == 'g' && p->buf[3] == 'S' &&
  533. p->buf[4] == 0x0 && p->buf[5] <= 0x7 )
  534. return AVPROBE_SCORE_MAX;
  535. else
  536. return 0;
  537. }
  538. AVInputFormat ff_ogg_demuxer = {
  539. "ogg",
  540. NULL_IF_CONFIG_SMALL("Ogg"),
  541. sizeof (struct ogg),
  542. ogg_probe,
  543. ogg_read_header,
  544. ogg_read_packet,
  545. ogg_read_close,
  546. ogg_read_seek,
  547. ogg_read_timestamp,
  548. .extensions = "ogg",
  549. .flags = AVFMT_GENERIC_INDEX,
  550. };