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.

699 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 "ogg2.h"
  29. #include "avformat.h"
  30. #define MAX_PAGE_SIZE 65307
  31. #define DECODER_BUFFER_SIZE MAX_PAGE_SIZE
  32. static ogg_codec_t *ogg_codecs[] = {
  33. &vorbis_codec,
  34. &theora_codec,
  35. &flac_codec,
  36. &ogm_video_codec,
  37. &ogm_audio_codec,
  38. &ogm_old_codec,
  39. NULL
  40. };
  41. #if 0 // CONFIG_MUXERS
  42. static int
  43. ogg_write_header (AVFormatContext * avfcontext)
  44. {
  45. }
  46. static int
  47. ogg_write_packet (AVFormatContext * avfcontext, AVPacket * pkt)
  48. {
  49. }
  50. static int
  51. ogg_write_trailer (AVFormatContext * avfcontext)
  52. {
  53. }
  54. AVOutputFormat ogg_muxer = {
  55. "ogg",
  56. "Ogg Vorbis",
  57. "audio/x-vorbis",
  58. "ogg",
  59. sizeof (OggContext),
  60. CODEC_ID_VORBIS,
  61. 0,
  62. ogg_write_header,
  63. ogg_write_packet,
  64. ogg_write_trailer,
  65. };
  66. #endif //CONFIG_MUXERS
  67. //FIXME We could avoid some structure duplication
  68. static int
  69. ogg_save (AVFormatContext * s)
  70. {
  71. ogg_t *ogg = s->priv_data;
  72. ogg_state_t *ost =
  73. av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
  74. int i;
  75. ost->pos = url_ftell (&s->pb);;
  76. ost->curidx = ogg->curidx;
  77. ost->next = ogg->state;
  78. memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
  79. for (i = 0; i < ogg->nstreams; i++){
  80. ogg_stream_t *os = ogg->streams + i;
  81. os->buf = av_malloc (os->bufsize);
  82. memset (os->buf, 0, os->bufsize);
  83. memcpy (os->buf, ost->streams[i].buf, os->bufpos);
  84. }
  85. ogg->state = ost;
  86. return 0;
  87. }
  88. static int
  89. ogg_restore (AVFormatContext * s, int discard)
  90. {
  91. ogg_t *ogg = s->priv_data;
  92. ByteIOContext *bc = &s->pb;
  93. ogg_state_t *ost = ogg->state;
  94. int i;
  95. if (!ost)
  96. return 0;
  97. ogg->state = ost->next;
  98. if (!discard){
  99. for (i = 0; i < ogg->nstreams; i++)
  100. av_free (ogg->streams[i].buf);
  101. url_fseek (bc, ost->pos, SEEK_SET);
  102. ogg->curidx = ost->curidx;
  103. memcpy (ogg->streams, ost->streams,
  104. ogg->nstreams * sizeof (*ogg->streams));
  105. }
  106. av_free (ost);
  107. return 0;
  108. }
  109. static int
  110. ogg_reset (ogg_t * ogg)
  111. {
  112. int i;
  113. for (i = 0; i < ogg->nstreams; i++){
  114. ogg_stream_t *os = ogg->streams + i;
  115. os->bufpos = 0;
  116. os->pstart = 0;
  117. os->psize = 0;
  118. os->granule = -1;
  119. os->lastgp = -1;
  120. os->nsegs = 0;
  121. os->segp = 0;
  122. }
  123. ogg->curidx = -1;
  124. return 0;
  125. }
  126. static ogg_codec_t *
  127. ogg_find_codec (uint8_t * buf, int size)
  128. {
  129. int i;
  130. for (i = 0; ogg_codecs[i]; i++)
  131. if (size >= ogg_codecs[i]->magicsize &&
  132. !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
  133. return ogg_codecs[i];
  134. return NULL;
  135. }
  136. static int
  137. ogg_find_stream (ogg_t * ogg, int serial)
  138. {
  139. int i;
  140. for (i = 0; i < ogg->nstreams; i++)
  141. if (ogg->streams[i].serial == serial)
  142. return i;
  143. return -1;
  144. }
  145. static int
  146. ogg_new_stream (AVFormatContext * s, uint32_t serial)
  147. {
  148. ogg_t *ogg = s->priv_data;
  149. int idx = ogg->nstreams++;
  150. AVStream *st;
  151. ogg_stream_t *os;
  152. ogg->streams = av_realloc (ogg->streams,
  153. ogg->nstreams * sizeof (*ogg->streams));
  154. memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
  155. os = ogg->streams + idx;
  156. os->serial = serial;
  157. os->bufsize = DECODER_BUFFER_SIZE;
  158. os->buf = av_malloc(os->bufsize);
  159. os->header = -1;
  160. st = av_new_stream (s, idx);
  161. if (!st)
  162. return AVERROR_NOMEM;
  163. av_set_pts_info(st, 64, 1, 1000000);
  164. return idx;
  165. }
  166. static int
  167. ogg_new_buf(ogg_t *ogg, int idx)
  168. {
  169. ogg_stream_t *os = ogg->streams + idx;
  170. uint8_t *nb = av_malloc(os->bufsize);
  171. int size = os->bufpos - os->pstart;
  172. if(os->buf){
  173. memcpy(nb, os->buf + os->pstart, size);
  174. av_free(os->buf);
  175. }
  176. os->buf = nb;
  177. os->bufpos = size;
  178. os->pstart = 0;
  179. return 0;
  180. }
  181. static int
  182. ogg_read_page (AVFormatContext * s, int *str)
  183. {
  184. ByteIOContext *bc = &s->pb;
  185. ogg_t *ogg = s->priv_data;
  186. ogg_stream_t *os;
  187. int i = 0;
  188. int flags, nsegs;
  189. uint64_t gp;
  190. uint32_t serial;
  191. uint32_t seq;
  192. uint32_t crc;
  193. int size, idx;
  194. char sync[4];
  195. int sp = 0;
  196. if (get_buffer (bc, sync, 4) < 4)
  197. return -1;
  198. do{
  199. int c;
  200. if (sync[sp & 3] == 'O' &&
  201. sync[(sp + 1) & 3] == 'g' &&
  202. sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
  203. break;
  204. c = url_fgetc (bc);
  205. if (c < 0)
  206. return -1;
  207. sync[sp++ & 3] = c;
  208. }while (i++ < MAX_PAGE_SIZE);
  209. if (i >= MAX_PAGE_SIZE){
  210. av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
  211. return -1;
  212. }
  213. if (url_fgetc (bc) != 0) /* version */
  214. return -1;
  215. flags = url_fgetc (bc);
  216. gp = get_le64 (bc);
  217. serial = get_le32 (bc);
  218. seq = get_le32 (bc);
  219. crc = get_le32 (bc);
  220. nsegs = url_fgetc (bc);
  221. idx = ogg_find_stream (ogg, serial);
  222. if (idx < 0){
  223. idx = ogg_new_stream (s, serial);
  224. if (idx < 0)
  225. return -1;
  226. }
  227. os = ogg->streams + idx;
  228. if(os->psize > 0)
  229. ogg_new_buf(ogg, idx);
  230. if (get_buffer (bc, os->segments, nsegs) < nsegs)
  231. return -1;
  232. os->nsegs = nsegs;
  233. os->segp = 0;
  234. size = 0;
  235. for (i = 0; i < nsegs; i++)
  236. size += os->segments[i];
  237. if (flags & OGG_FLAG_CONT){
  238. if (!os->psize){
  239. while (os->segp < os->nsegs){
  240. int seg = os->segments[os->segp++];
  241. os->pstart += seg;
  242. if (seg < 255)
  243. break;
  244. }
  245. }
  246. }else{
  247. os->psize = 0;
  248. }
  249. if (os->bufsize - os->bufpos < size){
  250. uint8_t *nb = av_malloc (os->bufsize *= 2);
  251. memset (nb, 0, os->bufsize);
  252. memcpy (nb, os->buf, os->bufpos);
  253. av_free (os->buf);
  254. os->buf = nb;
  255. }
  256. if (get_buffer (bc, os->buf + os->bufpos, size) < size)
  257. return -1;
  258. os->lastgp = os->granule;
  259. os->bufpos += size;
  260. os->granule = gp;
  261. os->flags = flags;
  262. if (str)
  263. *str = idx;
  264. return 0;
  265. }
  266. static int
  267. ogg_packet (AVFormatContext * s, int *str, int *dstart, int *dsize)
  268. {
  269. ogg_t *ogg = s->priv_data;
  270. int idx;
  271. ogg_stream_t *os;
  272. int complete = 0;
  273. int segp = 0, psize = 0;
  274. #if 0
  275. av_log (s, AV_LOG_DEBUG, "ogg_packet: curidx=%i\n", ogg->curidx);
  276. #endif
  277. do{
  278. idx = ogg->curidx;
  279. while (idx < 0){
  280. if (ogg_read_page (s, &idx) < 0)
  281. return -1;
  282. }
  283. os = ogg->streams + idx;
  284. #if 0
  285. av_log (s, AV_LOG_DEBUG,
  286. "ogg_packet: idx=%d pstart=%d psize=%d segp=%d nsegs=%d\n",
  287. idx, os->pstart, os->psize, os->segp, os->nsegs);
  288. #endif
  289. if (!os->codec){
  290. if (os->header < 0){
  291. os->codec = ogg_find_codec (os->buf, os->bufpos);
  292. if (!os->codec){
  293. os->header = 0;
  294. return 0;
  295. }
  296. }else{
  297. return 0;
  298. }
  299. }
  300. segp = os->segp;
  301. psize = os->psize;
  302. while (os->segp < os->nsegs){
  303. int ss = os->segments[os->segp++];
  304. os->psize += ss;
  305. if (ss < 255){
  306. complete = 1;
  307. break;
  308. }
  309. }
  310. if (!complete && os->segp == os->nsegs){
  311. ogg->curidx = -1;
  312. }
  313. }while (!complete);
  314. #if 0
  315. av_log (s, AV_LOG_DEBUG,
  316. "ogg_packet: idx %i, frame size %i, start %i\n",
  317. idx, os->psize, os->pstart);
  318. #endif
  319. ogg->curidx = idx;
  320. if (os->header < 0){
  321. int hdr = os->codec->header (s, idx);
  322. if (!hdr){
  323. os->header = os->seq;
  324. os->segp = segp;
  325. os->psize = psize;
  326. ogg->headers = 1;
  327. }else{
  328. os->pstart += os->psize;
  329. os->psize = 0;
  330. }
  331. }
  332. if (os->header > -1 && os->seq > os->header){
  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. os->pstart += os->psize;
  342. os->psize = 0;
  343. }
  344. os->seq++;
  345. if (os->segp == os->nsegs)
  346. ogg->curidx = -1;
  347. return 0;
  348. }
  349. static int
  350. ogg_get_headers (AVFormatContext * s)
  351. {
  352. ogg_t *ogg = s->priv_data;
  353. do{
  354. if (ogg_packet (s, NULL, NULL, NULL) < 0)
  355. return -1;
  356. }while (!ogg->headers);
  357. #if 0
  358. av_log (s, AV_LOG_DEBUG, "found headers\n");
  359. #endif
  360. return 0;
  361. }
  362. static uint64_t
  363. ogg_gptopts (AVFormatContext * s, int i, uint64_t gp)
  364. {
  365. ogg_t *ogg = s->priv_data;
  366. ogg_stream_t *os = ogg->streams + i;
  367. uint64_t pts = AV_NOPTS_VALUE;
  368. if(os->codec->gptopts){
  369. pts = os->codec->gptopts(s, i, gp);
  370. } else {
  371. pts = gp;
  372. }
  373. return pts;
  374. }
  375. static int
  376. ogg_get_length (AVFormatContext * s)
  377. {
  378. ogg_t *ogg = s->priv_data;
  379. int idx = -1, i;
  380. offset_t size, end;
  381. if(s->pb.is_streamed)
  382. return 0;
  383. // already set
  384. if (s->duration != AV_NOPTS_VALUE)
  385. return 0;
  386. size = url_fsize(&s->pb);
  387. if(size < 0)
  388. return 0;
  389. end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: size;
  390. ogg_save (s);
  391. url_fseek (&s->pb, end, SEEK_SET);
  392. while (!ogg_read_page (s, &i)){
  393. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0)
  394. idx = i;
  395. }
  396. if (idx != -1){
  397. s->streams[idx]->duration =
  398. ogg_gptopts (s, idx, ogg->streams[idx].granule);
  399. }
  400. ogg->size = size;
  401. ogg_restore (s, 0);
  402. ogg_save (s);
  403. while (ogg_read_page (s, &i)) {
  404. if (i == idx && ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0)
  405. break;
  406. }
  407. if (i == idx) {
  408. s->streams[idx]->start_time = ogg_gptopts (s, idx, ogg->streams[idx].granule);
  409. s->streams[idx]->duration -= s->streams[idx]->start_time;
  410. }
  411. ogg_restore (s, 0);
  412. return 0;
  413. }
  414. static int
  415. ogg_read_header (AVFormatContext * s, AVFormatParameters * ap)
  416. {
  417. ogg_t *ogg = s->priv_data;
  418. ogg->curidx = -1;
  419. //linear headers seek from start
  420. if (ogg_get_headers (s) < 0){
  421. return -1;
  422. }
  423. //linear granulepos seek from end
  424. ogg_get_length (s);
  425. //fill the extradata in the per codec callbacks
  426. return 0;
  427. }
  428. static int
  429. ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
  430. {
  431. ogg_t *ogg;
  432. ogg_stream_t *os;
  433. int idx = -1;
  434. int pstart, psize;
  435. //Get an ogg packet
  436. do{
  437. if (ogg_packet (s, &idx, &pstart, &psize) < 0)
  438. return AVERROR_IO;
  439. }while (idx < 0 || !s->streams[idx]);
  440. ogg = s->priv_data;
  441. os = ogg->streams + idx;
  442. //Alloc a pkt
  443. if (av_new_packet (pkt, psize) < 0)
  444. return AVERROR_IO;
  445. pkt->stream_index = idx;
  446. memcpy (pkt->data, os->buf + pstart, psize);
  447. if (os->lastgp != -1LL){
  448. pkt->pts = ogg_gptopts (s, idx, os->lastgp);
  449. os->lastgp = -1;
  450. }
  451. return psize;
  452. }
  453. static int
  454. ogg_read_close (AVFormatContext * s)
  455. {
  456. ogg_t *ogg = s->priv_data;
  457. int i;
  458. for (i = 0; i < ogg->nstreams; i++){
  459. av_free (ogg->streams[i].buf);
  460. av_free (ogg->streams[i].private);
  461. }
  462. av_free (ogg->streams);
  463. return 0;
  464. }
  465. static int
  466. ogg_read_seek (AVFormatContext * s, int stream_index, int64_t target_ts,
  467. int flags)
  468. {
  469. AVStream *st = s->streams[stream_index];
  470. ogg_t *ogg = s->priv_data;
  471. ByteIOContext *bc = &s->pb;
  472. uint64_t min = 0, max = ogg->size;
  473. uint64_t tmin = st->start_time, tmax = st->start_time + st->duration;
  474. int64_t pts = AV_NOPTS_VALUE;
  475. ogg_save (s);
  476. if ((uint64_t)target_ts < tmin || target_ts < 0)
  477. target_ts = tmin;
  478. while (min <= max && tmin < tmax){
  479. uint64_t p = min + (max - min) * (target_ts - tmin) / (tmax - tmin);
  480. int i = -1;
  481. url_fseek (bc, p, SEEK_SET);
  482. while (!ogg_read_page (s, &i)){
  483. if (i == stream_index && ogg->streams[i].granule != 0 &&
  484. ogg->streams[i].granule != -1)
  485. break;
  486. }
  487. if (i == -1)
  488. break;
  489. pts = ogg_gptopts (s, i, ogg->streams[i].granule);
  490. p = url_ftell (bc);
  491. if (ABS (pts - target_ts) * st->time_base.num < st->time_base.den)
  492. break;
  493. if (pts > target_ts){
  494. if (max == p && tmax == pts) {
  495. // probably our tmin is wrong, causing us to always end up too late in the file
  496. tmin = (target_ts + tmin + 1) / 2;
  497. if (tmin == target_ts) {
  498. url_fseek(bc, min, SEEK_SET);
  499. break;
  500. }
  501. }
  502. max = p;
  503. tmax = pts;
  504. }else{
  505. if (min == p && tmin == pts) {
  506. // probably our tmax is wrong, causing us to always end up too early in the file
  507. tmax = (target_ts + tmax) / 2;
  508. if (tmax == target_ts) {
  509. url_fseek(bc, max, SEEK_SET);
  510. break;
  511. }
  512. }
  513. min = p;
  514. tmin = pts;
  515. }
  516. }
  517. if (ABS (pts - target_ts) * st->time_base.num < st->time_base.den){
  518. ogg_restore (s, 1);
  519. ogg_reset (ogg);
  520. }else{
  521. ogg_restore (s, 0);
  522. pts = AV_NOPTS_VALUE;
  523. }
  524. av_update_cur_dts(s, st, pts);
  525. return 0;
  526. #if 0
  527. //later...
  528. int64_t pos;
  529. if (av_seek_frame_binary (s, stream_index, target_ts, flags) < 0)
  530. return -1;
  531. pos = url_ftell (&s->pb);
  532. ogg_read_timestamp (s, stream_index, &pos, pos - 1);
  533. #endif
  534. }
  535. #if 0
  536. static int64_t
  537. ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
  538. int64_t pos_limit)
  539. {
  540. ogg_t *ogg = s->priv_data;
  541. ByteIOContext *bc = &s->pb;
  542. int64_t pos, pts;
  543. if (*pos_arg < 0)
  544. return AV_NOPTS_VALUE;
  545. pos = *pos_arg;
  546. }
  547. #endif
  548. static int ogg_probe(AVProbeData *p)
  549. {
  550. if (p->buf_size < 6)
  551. return 0;
  552. if (p->buf[0] == 'O' && p->buf[1] == 'g' &&
  553. p->buf[2] == 'g' && p->buf[3] == 'S' &&
  554. p->buf[4] == 0x0 && p->buf[5] <= 0x7 )
  555. return AVPROBE_SCORE_MAX;
  556. else
  557. return 0;
  558. }
  559. AVInputFormat ogg_demuxer = {
  560. "ogg",
  561. "Ogg",
  562. sizeof (ogg_t),
  563. ogg_probe,
  564. ogg_read_header,
  565. ogg_read_packet,
  566. ogg_read_close,
  567. ogg_read_seek,
  568. // ogg_read_timestamp,
  569. .extensions = "ogg",
  570. };