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.

665 lines
14KB

  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. static AVOutputFormat ogg_oformat = {
  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. memset (os->buf, 0, os->bufsize);
  160. os->header = -1;
  161. st = av_new_stream (s, idx);
  162. if (!st)
  163. return AVERROR_NOMEM;
  164. av_set_pts_info(st, 64, 1, 1000000);
  165. st->start_time = 0;
  166. return idx;
  167. }
  168. static int
  169. ogg_read_page (AVFormatContext * s, int *str)
  170. {
  171. ByteIOContext *bc = &s->pb;
  172. ogg_t *ogg = s->priv_data;
  173. ogg_stream_t *os;
  174. int i = 0;
  175. int flags, nsegs;
  176. uint64_t gp;
  177. uint32_t serial;
  178. uint32_t seq;
  179. uint32_t crc;
  180. int size, idx;
  181. char sync[4];
  182. int sp = 0;
  183. if (get_buffer (bc, sync, 4) < 4)
  184. return -1;
  185. do{
  186. int c;
  187. if (sync[sp & 3] == 'O' &&
  188. sync[(sp + 1) & 3] == 'g' &&
  189. sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
  190. break;
  191. c = url_fgetc (bc);
  192. if (c < 0)
  193. return -1;
  194. sync[sp++ & 3] = c;
  195. }while (i++ < MAX_PAGE_SIZE);
  196. if (i >= MAX_PAGE_SIZE){
  197. av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
  198. return -1;
  199. }
  200. if (url_fgetc (bc) != 0) /* version */
  201. return -1;
  202. flags = url_fgetc (bc);
  203. gp = get_le64 (bc);
  204. serial = get_le32 (bc);
  205. seq = get_le32 (bc);
  206. crc = get_le32 (bc);
  207. nsegs = url_fgetc (bc);
  208. idx = ogg_find_stream (ogg, serial);
  209. if (idx < 0){
  210. idx = ogg_new_stream (s, serial);
  211. if (idx < 0)
  212. return -1;
  213. }
  214. os = ogg->streams + idx;
  215. if (get_buffer (bc, os->segments, nsegs) < nsegs)
  216. return -1;
  217. os->nsegs = nsegs;
  218. os->segp = 0;
  219. size = 0;
  220. for (i = 0; i < nsegs; i++)
  221. size += os->segments[i];
  222. if (flags & OGG_FLAG_CONT){
  223. if (!os->psize){
  224. while (os->segp < os->nsegs){
  225. int seg = os->segments[os->segp++];
  226. os->pstart += seg;
  227. if (seg < 255)
  228. break;
  229. }
  230. }
  231. }else{
  232. os->psize = 0;
  233. }
  234. if (os->bufsize - os->bufpos < size){
  235. uint8_t *nb = av_malloc (os->bufsize *= 2);
  236. memset (nb, 0, os->bufsize);
  237. memcpy (nb, os->buf, os->bufpos);
  238. av_free (os->buf);
  239. os->buf = nb;
  240. }
  241. if (get_buffer (bc, os->buf + os->bufpos, size) < size)
  242. return -1;
  243. os->lastgp = os->granule;
  244. os->bufpos += size;
  245. os->granule = gp;
  246. os->flags = flags;
  247. if (str)
  248. *str = idx;
  249. return 0;
  250. }
  251. static int
  252. ogg_packet (AVFormatContext * s, int *str)
  253. {
  254. ogg_t *ogg = s->priv_data;
  255. int idx;
  256. ogg_stream_t *os;
  257. int complete = 0;
  258. int segp = 0, psize = 0;
  259. #if 0
  260. av_log (s, AV_LOG_DEBUG, "ogg_packet: curidx=%i\n", ogg->curidx);
  261. #endif
  262. do{
  263. idx = ogg->curidx;
  264. while (idx < 0){
  265. if (ogg_read_page (s, &idx) < 0)
  266. return -1;
  267. }
  268. os = ogg->streams + idx;
  269. #if 0
  270. av_log (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. #endif
  274. if (!os->codec){
  275. if (os->header < 0){
  276. os->codec = ogg_find_codec (os->buf, os->bufpos);
  277. if (!os->codec){
  278. os->header = 0;
  279. return 0;
  280. }
  281. }else{
  282. return 0;
  283. }
  284. }
  285. segp = os->segp;
  286. psize = os->psize;
  287. while (os->segp < os->nsegs){
  288. int ss = os->segments[os->segp++];
  289. os->psize += ss;
  290. if (ss < 255){
  291. complete = 1;
  292. break;
  293. }
  294. }
  295. if (!complete && os->segp == os->nsegs){
  296. uint8_t *nb = av_malloc (os->bufsize);
  297. int size = os->bufpos - os->pstart;
  298. memset (nb, 0, os->bufsize);
  299. memcpy (nb, os->buf + os->pstart, size);
  300. av_free (os->buf);
  301. os->buf = nb;
  302. os->bufpos = size;
  303. os->pstart = 0;
  304. ogg->curidx = -1;
  305. }
  306. }while (!complete);
  307. #if 0
  308. av_log (s, AV_LOG_DEBUG,
  309. "ogg_packet: idx %i, frame size %i, start %i\n",
  310. idx, os->psize, os->pstart);
  311. #endif
  312. ogg->curidx = idx;
  313. if (os->header < 0){
  314. int hdr = os->codec->header (s, idx);
  315. if (!hdr){
  316. os->header = os->seq;
  317. os->segp = segp;
  318. os->psize = psize;
  319. ogg->headers = 1;
  320. }else{
  321. os->pstart += os->psize;
  322. os->psize = 0;
  323. }
  324. }
  325. if (os->header > -1 && os->seq > os->header){
  326. if (os->codec && os->codec->packet)
  327. os->codec->packet (s, idx);
  328. if (str)
  329. *str = idx;
  330. }
  331. os->seq++;
  332. if (os->segp == os->nsegs)
  333. ogg->curidx = -1;
  334. return 0;
  335. }
  336. static int
  337. ogg_get_headers (AVFormatContext * s)
  338. {
  339. ogg_t *ogg = s->priv_data;
  340. do{
  341. if (ogg_packet (s, NULL) < 0)
  342. return -1;
  343. }while (!ogg->headers);
  344. #if 0
  345. av_log (s, AV_LOG_DEBUG, "found headers\n");
  346. #endif
  347. return 0;
  348. }
  349. static uint64_t
  350. ogg_gptopts (AVFormatContext * s, int i, uint64_t gp)
  351. {
  352. ogg_t *ogg = s->priv_data;
  353. ogg_stream_t *os = ogg->streams + i;
  354. uint64_t pts = AV_NOPTS_VALUE;
  355. if(os->codec->gptopts){
  356. pts = os->codec->gptopts(s, i, gp);
  357. } else {
  358. pts = gp;
  359. }
  360. return pts;
  361. }
  362. static int
  363. ogg_get_length (AVFormatContext * s)
  364. {
  365. ogg_t *ogg = s->priv_data;
  366. int idx = -1, i;
  367. offset_t size, end;
  368. if(s->pb.is_streamed)
  369. return 0;
  370. // already set
  371. if (s->duration != AV_NOPTS_VALUE)
  372. return 0;
  373. size = url_fsize(&s->pb);
  374. if(size < 0)
  375. return 0;
  376. end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: size;
  377. ogg_save (s);
  378. url_fseek (&s->pb, end, SEEK_SET);
  379. while (!ogg_read_page (s, &i)){
  380. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0)
  381. idx = i;
  382. }
  383. if (idx != -1){
  384. s->streams[idx]->duration =
  385. ogg_gptopts (s, idx, ogg->streams[idx].granule);
  386. }
  387. ogg->size = size;
  388. ogg_restore (s, 0);
  389. return 0;
  390. }
  391. static int
  392. ogg_read_header (AVFormatContext * s, AVFormatParameters * ap)
  393. {
  394. ogg_t *ogg = s->priv_data;
  395. ogg->curidx = -1;
  396. //linear headers seek from start
  397. if (ogg_get_headers (s) < 0){
  398. return -1;
  399. }
  400. //linear granulepos seek from end
  401. ogg_get_length (s);
  402. //fill the extradata in the per codec callbacks
  403. return 0;
  404. }
  405. static int
  406. ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
  407. {
  408. ogg_t *ogg;
  409. ogg_stream_t *os;
  410. int idx = -1;
  411. //Get an ogg packet
  412. do{
  413. if (ogg_packet (s, &idx) < 0)
  414. return AVERROR_IO;
  415. }while (idx < 0 || !s->streams[idx]);
  416. ogg = s->priv_data;
  417. os = ogg->streams + idx;
  418. //Alloc a pkt
  419. if (av_new_packet (pkt, os->psize) < 0)
  420. return AVERROR_IO;
  421. pkt->stream_index = idx;
  422. memcpy (pkt->data, os->buf + os->pstart, os->psize);
  423. if (os->lastgp != -1LL){
  424. pkt->pts = ogg_gptopts (s, idx, os->lastgp);
  425. os->lastgp = -1;
  426. }
  427. //next
  428. os->pstart += os->psize;
  429. os->psize = 0;
  430. return os->psize;
  431. }
  432. static int
  433. ogg_read_close (AVFormatContext * s)
  434. {
  435. ogg_t *ogg = s->priv_data;
  436. int i;
  437. for (i = 0; i < ogg->nstreams; i++){
  438. av_free (ogg->streams[i].buf);
  439. av_free (ogg->streams[i].private);
  440. }
  441. av_free (ogg->streams);
  442. return 0;
  443. }
  444. static int
  445. ogg_read_seek (AVFormatContext * s, int stream_index, int64_t target_ts,
  446. int flags)
  447. {
  448. AVStream *st = s->streams[stream_index];
  449. ogg_t *ogg = s->priv_data;
  450. ByteIOContext *bc = &s->pb;
  451. uint64_t min = 0, max = ogg->size;
  452. uint64_t tmin = 0, tmax = st->duration;
  453. int64_t pts = AV_NOPTS_VALUE;
  454. ogg_save (s);
  455. while (min <= max){
  456. uint64_t p = min + (max - min) * (target_ts - tmin) / (tmax - tmin);
  457. int i = -1;
  458. url_fseek (bc, p, SEEK_SET);
  459. while (!ogg_read_page (s, &i)){
  460. if (i == stream_index && ogg->streams[i].granule != 0 &&
  461. ogg->streams[i].granule != -1)
  462. break;
  463. }
  464. if (i == -1)
  465. break;
  466. pts = ogg_gptopts (s, i, ogg->streams[i].granule);
  467. p = url_ftell (bc);
  468. if (ABS (pts - target_ts) * st->time_base.num < st->time_base.den)
  469. break;
  470. if (pts > target_ts){
  471. max = p;
  472. tmax = pts;
  473. }else{
  474. min = p;
  475. tmin = pts;
  476. }
  477. }
  478. if (ABS (pts - target_ts) * st->time_base.num < st->time_base.den){
  479. ogg_restore (s, 1);
  480. ogg_reset (ogg);
  481. }else{
  482. ogg_restore (s, 0);
  483. pts = AV_NOPTS_VALUE;
  484. }
  485. return pts;
  486. #if 0
  487. //later...
  488. int64_t pos;
  489. if (av_seek_frame_binary (s, stream_index, target_ts, flags) < 0)
  490. return -1;
  491. pos = url_ftell (&s->pb);
  492. ogg_read_timestamp (s, stream_index, &pos, pos - 1);
  493. #endif
  494. }
  495. #if 0
  496. static int64_t
  497. ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
  498. int64_t pos_limit)
  499. {
  500. ogg_t *ogg = s->priv_data;
  501. ByteIOContext *bc = &s->pb;
  502. int64_t pos, pts;
  503. if (*pos_arg < 0)
  504. return AV_NOPTS_VALUE;
  505. pos = *pos_arg;
  506. }
  507. #endif
  508. static int ogg_probe(AVProbeData *p)
  509. {
  510. if (p->buf_size < 6)
  511. return 0;
  512. if (p->buf[0] == 'O' && p->buf[1] == 'g' &&
  513. p->buf[2] == 'g' && p->buf[3] == 'S' &&
  514. p->buf[4] == 0x0 && p->buf[5] <= 0x7 )
  515. return AVPROBE_SCORE_MAX;
  516. else
  517. return 0;
  518. }
  519. static AVInputFormat ogg_iformat = {
  520. "ogg",
  521. "Ogg",
  522. sizeof (ogg_t),
  523. ogg_probe,
  524. ogg_read_header,
  525. ogg_read_packet,
  526. ogg_read_close,
  527. ogg_read_seek,
  528. // ogg_read_timestamp,
  529. .extensions = "ogg",
  530. };
  531. int
  532. ogg_init (void)
  533. {
  534. #if 0 // CONFIG_MUXERS
  535. av_register_output_format (&ogg_oformat);
  536. #endif
  537. av_register_input_format (&ogg_iformat);
  538. return 0;
  539. }