oggdec.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. #define MAX_PAGE_SIZE 65307
  31. #define DECODER_BUFFER_SIZE MAX_PAGE_SIZE
  32. static const struct ogg_codec * const ogg_codecs[] = {
  33. &ff_skeleton_codec,
  34. &ff_dirac_codec,
  35. &ff_speex_codec,
  36. &ff_vorbis_codec,
  37. &ff_theora_codec,
  38. &ff_flac_codec,
  39. &ff_old_dirac_codec,
  40. &ff_old_flac_codec,
  41. &ff_ogm_video_codec,
  42. &ff_ogm_audio_codec,
  43. &ff_ogm_text_codec,
  44. &ff_ogm_old_codec,
  45. NULL
  46. };
  47. //FIXME We could avoid some structure duplication
  48. static int
  49. ogg_save (AVFormatContext * s)
  50. {
  51. struct ogg *ogg = s->priv_data;
  52. struct ogg_state *ost =
  53. av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
  54. int i;
  55. ost->pos = url_ftell (s->pb);
  56. ost->curidx = ogg->curidx;
  57. ost->next = ogg->state;
  58. ost->nstreams = ogg->nstreams;
  59. memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
  60. for (i = 0; i < ogg->nstreams; i++){
  61. struct ogg_stream *os = ogg->streams + i;
  62. os->buf = av_malloc (os->bufsize);
  63. memset (os->buf, 0, os->bufsize);
  64. memcpy (os->buf, ost->streams[i].buf, os->bufpos);
  65. }
  66. ogg->state = ost;
  67. return 0;
  68. }
  69. static int
  70. ogg_restore (AVFormatContext * s, int discard)
  71. {
  72. struct ogg *ogg = s->priv_data;
  73. ByteIOContext *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. url_fseek (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
  92. ogg_reset (struct ogg * ogg)
  93. {
  94. int i;
  95. for (i = 0; i < ogg->nstreams; i++){
  96. struct ogg_stream *os = ogg->streams + i;
  97. os->bufpos = 0;
  98. os->pstart = 0;
  99. os->psize = 0;
  100. os->granule = -1;
  101. os->lastpts = AV_NOPTS_VALUE;
  102. os->lastdts = AV_NOPTS_VALUE;
  103. os->sync_pos = -1;
  104. os->page_pos = 0;
  105. os->nsegs = 0;
  106. os->segp = 0;
  107. os->incomplete = 0;
  108. }
  109. ogg->curidx = -1;
  110. return 0;
  111. }
  112. static const struct ogg_codec *
  113. ogg_find_codec (uint8_t * buf, int size)
  114. {
  115. int i;
  116. for (i = 0; ogg_codecs[i]; i++)
  117. if (size >= ogg_codecs[i]->magicsize &&
  118. !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
  119. return ogg_codecs[i];
  120. return NULL;
  121. }
  122. static int
  123. ogg_new_stream (AVFormatContext * s, uint32_t serial)
  124. {
  125. struct ogg *ogg = s->priv_data;
  126. int idx = ogg->nstreams++;
  127. AVStream *st;
  128. struct ogg_stream *os;
  129. ogg->streams = av_realloc (ogg->streams,
  130. ogg->nstreams * sizeof (*ogg->streams));
  131. memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
  132. os = ogg->streams + idx;
  133. os->serial = serial;
  134. os->bufsize = DECODER_BUFFER_SIZE;
  135. os->buf = av_malloc(os->bufsize);
  136. os->header = -1;
  137. st = av_new_stream (s, idx);
  138. if (!st)
  139. return AVERROR(ENOMEM);
  140. av_set_pts_info(st, 64, 1, 1000000);
  141. return idx;
  142. }
  143. static int
  144. ogg_new_buf(struct ogg *ogg, int idx)
  145. {
  146. struct ogg_stream *os = ogg->streams + idx;
  147. uint8_t *nb = av_malloc(os->bufsize);
  148. int size = os->bufpos - os->pstart;
  149. if(os->buf){
  150. memcpy(nb, os->buf + os->pstart, size);
  151. av_free(os->buf);
  152. }
  153. os->buf = nb;
  154. os->bufpos = size;
  155. os->pstart = 0;
  156. return 0;
  157. }
  158. static int
  159. ogg_read_page (AVFormatContext * s, int *str)
  160. {
  161. ByteIOContext *bc = s->pb;
  162. struct ogg *ogg = s->priv_data;
  163. struct ogg_stream *os;
  164. int i = 0;
  165. int flags, nsegs;
  166. uint64_t gp;
  167. uint32_t serial;
  168. uint32_t seq;
  169. uint32_t crc;
  170. int size, idx;
  171. uint8_t sync[4];
  172. int sp = 0;
  173. if (get_buffer (bc, sync, 4) < 4)
  174. return -1;
  175. do{
  176. int c;
  177. if (sync[sp & 3] == 'O' &&
  178. sync[(sp + 1) & 3] == 'g' &&
  179. sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
  180. break;
  181. c = url_fgetc (bc);
  182. if (c < 0)
  183. return -1;
  184. sync[sp++ & 3] = c;
  185. }while (i++ < MAX_PAGE_SIZE);
  186. if (i >= MAX_PAGE_SIZE){
  187. av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
  188. return -1;
  189. }
  190. if (url_fgetc (bc) != 0) /* version */
  191. return -1;
  192. flags = url_fgetc (bc);
  193. gp = get_le64 (bc);
  194. serial = get_le32 (bc);
  195. seq = get_le32 (bc);
  196. crc = get_le32 (bc);
  197. nsegs = url_fgetc (bc);
  198. idx = ogg_find_stream (ogg, serial);
  199. if (idx < 0){
  200. idx = ogg_new_stream (s, serial);
  201. if (idx < 0)
  202. return -1;
  203. }
  204. os = ogg->streams + idx;
  205. os->page_pos = url_ftell(bc) - 27;
  206. if(os->psize > 0)
  207. ogg_new_buf(ogg, idx);
  208. if (get_buffer (bc, os->segments, nsegs) < nsegs)
  209. return -1;
  210. os->nsegs = nsegs;
  211. os->segp = 0;
  212. size = 0;
  213. for (i = 0; i < nsegs; i++)
  214. size += os->segments[i];
  215. if (flags & OGG_FLAG_CONT || os->incomplete){
  216. if (!os->psize){
  217. while (os->segp < os->nsegs){
  218. int seg = os->segments[os->segp++];
  219. os->pstart += seg;
  220. if (seg < 255)
  221. break;
  222. }
  223. os->sync_pos = os->page_pos;
  224. }
  225. }else{
  226. os->psize = 0;
  227. os->sync_pos = os->page_pos;
  228. }
  229. if (os->bufsize - os->bufpos < size){
  230. uint8_t *nb = av_malloc (os->bufsize *= 2);
  231. memcpy (nb, os->buf, os->bufpos);
  232. av_free (os->buf);
  233. os->buf = nb;
  234. }
  235. if (get_buffer (bc, os->buf + os->bufpos, size) < size)
  236. return -1;
  237. os->bufpos += size;
  238. os->granule = gp;
  239. os->flags = flags;
  240. if (str)
  241. *str = idx;
  242. return 0;
  243. }
  244. static int
  245. ogg_packet (AVFormatContext * s, int *str, int *dstart, int *dsize, int64_t *fpos)
  246. {
  247. struct ogg *ogg = s->priv_data;
  248. int idx, i;
  249. struct ogg_stream *os;
  250. int complete = 0;
  251. int segp = 0, psize = 0;
  252. #if 0
  253. av_log (s, AV_LOG_DEBUG, "ogg_packet: curidx=%i\n", ogg->curidx);
  254. #endif
  255. do{
  256. idx = ogg->curidx;
  257. while (idx < 0){
  258. if (ogg_read_page (s, &idx) < 0)
  259. return -1;
  260. }
  261. os = ogg->streams + idx;
  262. #if 0
  263. av_log (s, AV_LOG_DEBUG,
  264. "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
  265. idx, os->pstart, os->psize, os->segp, os->nsegs);
  266. #endif
  267. if (!os->codec){
  268. if (os->header < 0){
  269. os->codec = ogg_find_codec (os->buf, os->bufpos);
  270. if (!os->codec){
  271. os->header = 0;
  272. return 0;
  273. }
  274. }else{
  275. return 0;
  276. }
  277. }
  278. segp = os->segp;
  279. psize = os->psize;
  280. while (os->segp < os->nsegs){
  281. int ss = os->segments[os->segp++];
  282. os->psize += ss;
  283. if (ss < 255){
  284. complete = 1;
  285. break;
  286. }
  287. }
  288. if (!complete && os->segp == os->nsegs){
  289. ogg->curidx = -1;
  290. os->incomplete = 1;
  291. }
  292. }while (!complete);
  293. #if 0
  294. av_log (s, AV_LOG_DEBUG,
  295. "ogg_packet: idx %i, frame size %i, start %i\n",
  296. idx, os->psize, os->pstart);
  297. #endif
  298. ogg->curidx = idx;
  299. os->incomplete = 0;
  300. if (!ogg->headers){
  301. int hdr = os->codec->header (s, idx);
  302. os->header = os->seq;
  303. if (!hdr){
  304. os->segp = segp;
  305. os->psize = psize;
  306. ogg->headers = 1;
  307. s->data_offset = os->sync_pos;
  308. }else{
  309. os->pstart += os->psize;
  310. os->psize = 0;
  311. }
  312. }
  313. if (os->header > -1 && os->seq > os->header){
  314. os->pflags = 0;
  315. os->pduration = 0;
  316. if (os->codec && os->codec->packet)
  317. os->codec->packet (s, idx);
  318. if (str)
  319. *str = idx;
  320. if (dstart)
  321. *dstart = os->pstart;
  322. if (dsize)
  323. *dsize = os->psize;
  324. if (fpos)
  325. *fpos = os->sync_pos;
  326. os->pstart += os->psize;
  327. os->psize = 0;
  328. os->sync_pos = os->page_pos;
  329. }
  330. // determine whether there are more complete packets in this page
  331. // if not, the page's granule will apply to this packet
  332. os->page_end = 1;
  333. for (i = os->segp; i < os->nsegs; i++)
  334. if (os->segments[i] < 255) {
  335. os->page_end = 0;
  336. break;
  337. }
  338. os->seq++;
  339. if (os->segp == os->nsegs)
  340. ogg->curidx = -1;
  341. return 0;
  342. }
  343. static int
  344. ogg_get_headers (AVFormatContext * s)
  345. {
  346. struct ogg *ogg = s->priv_data;
  347. do{
  348. if (ogg_packet (s, NULL, NULL, NULL, NULL) < 0)
  349. return -1;
  350. }while (!ogg->headers);
  351. #if 0
  352. av_log (s, AV_LOG_DEBUG, "found headers\n");
  353. #endif
  354. return 0;
  355. }
  356. static int
  357. ogg_get_length (AVFormatContext * s)
  358. {
  359. struct ogg *ogg = s->priv_data;
  360. int idx = -1, i;
  361. int64_t size, end;
  362. if(url_is_streamed(s->pb))
  363. return 0;
  364. // already set
  365. if (s->duration != AV_NOPTS_VALUE)
  366. return 0;
  367. size = url_fsize(s->pb);
  368. if(size < 0)
  369. return 0;
  370. end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: 0;
  371. ogg_save (s);
  372. url_fseek (s->pb, end, SEEK_SET);
  373. while (!ogg_read_page (s, &i)){
  374. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  375. ogg->streams[i].codec)
  376. idx = i;
  377. }
  378. if (idx != -1){
  379. s->streams[idx]->duration =
  380. ogg_gptopts (s, idx, ogg->streams[idx].granule, NULL);
  381. if (s->streams[idx]->start_time != AV_NOPTS_VALUE)
  382. s->streams[idx]->duration -= s->streams[idx]->start_time;
  383. }
  384. ogg->size = size;
  385. ogg_restore (s, 0);
  386. return 0;
  387. }
  388. static int
  389. ogg_read_header (AVFormatContext * s, AVFormatParameters * ap)
  390. {
  391. struct ogg *ogg = s->priv_data;
  392. int i;
  393. ogg->curidx = -1;
  394. //linear headers seek from start
  395. if (ogg_get_headers (s) < 0){
  396. return -1;
  397. }
  398. for (i = 0; i < ogg->nstreams; i++)
  399. if (ogg->streams[i].header < 0)
  400. ogg->streams[i].codec = NULL;
  401. //linear granulepos seek from end
  402. ogg_get_length (s);
  403. //fill the extradata in the per codec callbacks
  404. return 0;
  405. }
  406. static int64_t ogg_calc_pts(AVFormatContext *s, int idx, int64_t *dts)
  407. {
  408. struct ogg *ogg = s->priv_data;
  409. struct ogg_stream *os = ogg->streams + idx;
  410. int64_t pts = AV_NOPTS_VALUE;
  411. if (dts)
  412. *dts = AV_NOPTS_VALUE;
  413. if (os->lastpts != AV_NOPTS_VALUE) {
  414. pts = os->lastpts;
  415. os->lastpts = AV_NOPTS_VALUE;
  416. }
  417. if (os->lastdts != AV_NOPTS_VALUE) {
  418. if (dts)
  419. *dts = os->lastdts;
  420. os->lastdts = AV_NOPTS_VALUE;
  421. }
  422. if (os->page_end) {
  423. if (os->granule != -1LL) {
  424. if (os->codec && os->codec->granule_is_start)
  425. pts = ogg_gptopts(s, idx, os->granule, dts);
  426. else
  427. os->lastpts = ogg_gptopts(s, idx, os->granule, &os->lastdts);
  428. os->granule = -1LL;
  429. } else
  430. av_log(s, AV_LOG_WARNING, "Packet is missing granule\n");
  431. }
  432. return pts;
  433. }
  434. static int
  435. ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
  436. {
  437. struct ogg *ogg;
  438. struct ogg_stream *os;
  439. int idx = -1;
  440. int pstart, psize;
  441. int64_t fpos;
  442. //Get an ogg packet
  443. do{
  444. if (ogg_packet (s, &idx, &pstart, &psize, &fpos) < 0)
  445. return AVERROR(EIO);
  446. }while (idx < 0 || !s->streams[idx]);
  447. ogg = s->priv_data;
  448. os = ogg->streams + idx;
  449. //Alloc a pkt
  450. if (av_new_packet (pkt, psize) < 0)
  451. return AVERROR(EIO);
  452. pkt->stream_index = idx;
  453. memcpy (pkt->data, os->buf + pstart, psize);
  454. pkt->pts = ogg_calc_pts(s, idx, &pkt->dts);
  455. pkt->flags = os->pflags;
  456. pkt->duration = os->pduration;
  457. pkt->pos = fpos;
  458. return psize;
  459. }
  460. static int
  461. ogg_read_close (AVFormatContext * s)
  462. {
  463. struct ogg *ogg = s->priv_data;
  464. int i;
  465. for (i = 0; i < ogg->nstreams; i++){
  466. av_free (ogg->streams[i].buf);
  467. av_free (ogg->streams[i].private);
  468. }
  469. av_free (ogg->streams);
  470. return 0;
  471. }
  472. static int64_t
  473. ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
  474. int64_t pos_limit)
  475. {
  476. struct ogg *ogg = s->priv_data;
  477. ByteIOContext *bc = s->pb;
  478. int64_t pts = AV_NOPTS_VALUE;
  479. int i;
  480. url_fseek(bc, *pos_arg, SEEK_SET);
  481. ogg_reset(ogg);
  482. while (url_ftell(bc) < pos_limit && !ogg_packet(s, &i, NULL, NULL, pos_arg)) {
  483. if (i == stream_index) {
  484. pts = ogg_calc_pts(s, i, NULL);
  485. }
  486. if (pts != AV_NOPTS_VALUE)
  487. break;
  488. }
  489. ogg_reset(ogg);
  490. return pts;
  491. }
  492. static int ogg_probe(AVProbeData *p)
  493. {
  494. if (p->buf[0] == 'O' && p->buf[1] == 'g' &&
  495. p->buf[2] == 'g' && p->buf[3] == 'S' &&
  496. p->buf[4] == 0x0 && p->buf[5] <= 0x7 )
  497. return AVPROBE_SCORE_MAX;
  498. else
  499. return 0;
  500. }
  501. AVInputFormat ogg_demuxer = {
  502. "ogg",
  503. NULL_IF_CONFIG_SMALL("Ogg"),
  504. sizeof (struct ogg),
  505. ogg_probe,
  506. ogg_read_header,
  507. ogg_read_packet,
  508. ogg_read_close,
  509. NULL,
  510. ogg_read_timestamp,
  511. .extensions = "ogg",
  512. .metadata_conv = ff_vorbiscomment_metadata_conv,
  513. };