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.

587 lines
13KB

  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_speex_codec,
  34. &ff_vorbis_codec,
  35. &ff_theora_codec,
  36. &ff_flac_codec,
  37. &ff_old_flac_codec,
  38. &ff_ogm_video_codec,
  39. &ff_ogm_audio_codec,
  40. &ff_ogm_text_codec,
  41. &ff_ogm_old_codec,
  42. NULL
  43. };
  44. //FIXME We could avoid some structure duplication
  45. static int
  46. ogg_save (AVFormatContext * s)
  47. {
  48. struct ogg *ogg = s->priv_data;
  49. struct ogg_state *ost =
  50. av_malloc(sizeof (*ost) + (ogg->nstreams-1) * sizeof (*ogg->streams));
  51. int i;
  52. ost->pos = url_ftell (s->pb);
  53. ost->curidx = ogg->curidx;
  54. ost->next = ogg->state;
  55. ost->nstreams = ogg->nstreams;
  56. memcpy(ost->streams, ogg->streams, ogg->nstreams * sizeof(*ogg->streams));
  57. for (i = 0; i < ogg->nstreams; i++){
  58. struct ogg_stream *os = ogg->streams + i;
  59. os->buf = av_malloc (os->bufsize);
  60. memset (os->buf, 0, os->bufsize);
  61. memcpy (os->buf, ost->streams[i].buf, os->bufpos);
  62. }
  63. ogg->state = ost;
  64. return 0;
  65. }
  66. static int
  67. ogg_restore (AVFormatContext * s, int discard)
  68. {
  69. struct ogg *ogg = s->priv_data;
  70. ByteIOContext *bc = s->pb;
  71. struct ogg_state *ost = ogg->state;
  72. int i;
  73. if (!ost)
  74. return 0;
  75. ogg->state = ost->next;
  76. if (!discard){
  77. for (i = 0; i < ogg->nstreams; i++)
  78. av_free (ogg->streams[i].buf);
  79. url_fseek (bc, ost->pos, SEEK_SET);
  80. ogg->curidx = ost->curidx;
  81. ogg->nstreams = ost->nstreams;
  82. memcpy(ogg->streams, ost->streams,
  83. ost->nstreams * sizeof(*ogg->streams));
  84. }
  85. av_free (ost);
  86. return 0;
  87. }
  88. static int
  89. ogg_reset (struct ogg * ogg)
  90. {
  91. int i;
  92. for (i = 0; i < ogg->nstreams; i++){
  93. struct ogg_stream *os = ogg->streams + i;
  94. os->bufpos = 0;
  95. os->pstart = 0;
  96. os->psize = 0;
  97. os->granule = -1;
  98. os->lastgp = -1;
  99. os->nsegs = 0;
  100. os->segp = 0;
  101. }
  102. ogg->curidx = -1;
  103. return 0;
  104. }
  105. static const struct ogg_codec *
  106. ogg_find_codec (uint8_t * buf, int size)
  107. {
  108. int i;
  109. for (i = 0; ogg_codecs[i]; i++)
  110. if (size >= ogg_codecs[i]->magicsize &&
  111. !memcmp (buf, ogg_codecs[i]->magic, ogg_codecs[i]->magicsize))
  112. return ogg_codecs[i];
  113. return NULL;
  114. }
  115. static int
  116. ogg_find_stream (struct ogg * ogg, int serial)
  117. {
  118. int i;
  119. for (i = 0; i < ogg->nstreams; i++)
  120. if (ogg->streams[i].serial == serial)
  121. return i;
  122. return -1;
  123. }
  124. static int
  125. ogg_new_stream (AVFormatContext * s, uint32_t serial)
  126. {
  127. struct ogg *ogg = s->priv_data;
  128. int idx = ogg->nstreams++;
  129. AVStream *st;
  130. struct ogg_stream *os;
  131. ogg->streams = av_realloc (ogg->streams,
  132. ogg->nstreams * sizeof (*ogg->streams));
  133. memset (ogg->streams + idx, 0, sizeof (*ogg->streams));
  134. os = ogg->streams + idx;
  135. os->serial = serial;
  136. os->bufsize = DECODER_BUFFER_SIZE;
  137. os->buf = av_malloc(os->bufsize);
  138. os->header = -1;
  139. st = av_new_stream (s, idx);
  140. if (!st)
  141. return AVERROR(ENOMEM);
  142. av_set_pts_info(st, 64, 1, 1000000);
  143. return idx;
  144. }
  145. static int
  146. ogg_new_buf(struct ogg *ogg, int idx)
  147. {
  148. struct ogg_stream *os = ogg->streams + idx;
  149. uint8_t *nb = av_malloc(os->bufsize);
  150. int size = os->bufpos - os->pstart;
  151. if(os->buf){
  152. memcpy(nb, os->buf + os->pstart, size);
  153. av_free(os->buf);
  154. }
  155. os->buf = nb;
  156. os->bufpos = size;
  157. os->pstart = 0;
  158. return 0;
  159. }
  160. static int
  161. ogg_read_page (AVFormatContext * s, int *str)
  162. {
  163. ByteIOContext *bc = s->pb;
  164. struct ogg *ogg = s->priv_data;
  165. struct ogg_stream *os;
  166. int i = 0;
  167. int flags, nsegs;
  168. uint64_t gp;
  169. uint32_t serial;
  170. uint32_t seq;
  171. uint32_t crc;
  172. int size, idx;
  173. uint8_t sync[4];
  174. int sp = 0;
  175. if (get_buffer (bc, sync, 4) < 4)
  176. return -1;
  177. do{
  178. int c;
  179. if (sync[sp & 3] == 'O' &&
  180. sync[(sp + 1) & 3] == 'g' &&
  181. sync[(sp + 2) & 3] == 'g' && sync[(sp + 3) & 3] == 'S')
  182. break;
  183. c = url_fgetc (bc);
  184. if (c < 0)
  185. return -1;
  186. sync[sp++ & 3] = c;
  187. }while (i++ < MAX_PAGE_SIZE);
  188. if (i >= MAX_PAGE_SIZE){
  189. av_log (s, AV_LOG_INFO, "ogg, can't find sync word\n");
  190. return -1;
  191. }
  192. if (url_fgetc (bc) != 0) /* version */
  193. return -1;
  194. flags = url_fgetc (bc);
  195. gp = get_le64 (bc);
  196. serial = get_le32 (bc);
  197. seq = get_le32 (bc);
  198. crc = get_le32 (bc);
  199. nsegs = url_fgetc (bc);
  200. idx = ogg_find_stream (ogg, serial);
  201. if (idx < 0){
  202. idx = ogg_new_stream (s, serial);
  203. if (idx < 0)
  204. return -1;
  205. }
  206. os = ogg->streams + idx;
  207. if(os->psize > 0)
  208. ogg_new_buf(ogg, idx);
  209. if (get_buffer (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){
  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. }
  225. }else{
  226. os->psize = 0;
  227. }
  228. if (os->bufsize - os->bufpos < size){
  229. uint8_t *nb = av_malloc (os->bufsize *= 2);
  230. memcpy (nb, os->buf, os->bufpos);
  231. av_free (os->buf);
  232. os->buf = nb;
  233. }
  234. if (get_buffer (bc, os->buf + os->bufpos, size) < size)
  235. return -1;
  236. os->lastgp = os->granule;
  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)
  246. {
  247. struct ogg *ogg = s->priv_data;
  248. int idx;
  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. }
  291. }while (!complete);
  292. #if 0
  293. av_log (s, AV_LOG_DEBUG,
  294. "ogg_packet: idx %i, frame size %i, start %i\n",
  295. idx, os->psize, os->pstart);
  296. #endif
  297. ogg->curidx = idx;
  298. if (os->header < 0){
  299. int hdr = os->codec->header (s, idx);
  300. if (!hdr){
  301. os->header = os->seq;
  302. os->segp = segp;
  303. os->psize = psize;
  304. ogg->headers = 1;
  305. }else{
  306. os->pstart += os->psize;
  307. os->psize = 0;
  308. }
  309. }
  310. if (os->header > -1 && os->seq > os->header){
  311. os->pflags = 0;
  312. if (os->codec && os->codec->packet)
  313. os->codec->packet (s, idx);
  314. if (str)
  315. *str = idx;
  316. if (dstart)
  317. *dstart = os->pstart;
  318. if (dsize)
  319. *dsize = os->psize;
  320. os->pstart += os->psize;
  321. os->psize = 0;
  322. }
  323. os->seq++;
  324. if (os->segp == os->nsegs)
  325. ogg->curidx = -1;
  326. return 0;
  327. }
  328. static int
  329. ogg_get_headers (AVFormatContext * s)
  330. {
  331. struct ogg *ogg = s->priv_data;
  332. do{
  333. if (ogg_packet (s, NULL, NULL, NULL) < 0)
  334. return -1;
  335. }while (!ogg->headers);
  336. #if 0
  337. av_log (s, AV_LOG_DEBUG, "found headers\n");
  338. #endif
  339. return 0;
  340. }
  341. static uint64_t
  342. ogg_gptopts (AVFormatContext * s, int i, uint64_t gp)
  343. {
  344. struct ogg *ogg = s->priv_data;
  345. struct ogg_stream *os = ogg->streams + i;
  346. uint64_t pts = AV_NOPTS_VALUE;
  347. if(os->codec->gptopts){
  348. pts = os->codec->gptopts(s, i, gp);
  349. } else {
  350. pts = gp;
  351. }
  352. return pts;
  353. }
  354. static int
  355. ogg_get_length (AVFormatContext * s)
  356. {
  357. struct ogg *ogg = s->priv_data;
  358. int idx = -1, i;
  359. int64_t size, end;
  360. if(url_is_streamed(s->pb))
  361. return 0;
  362. // already set
  363. if (s->duration != AV_NOPTS_VALUE)
  364. return 0;
  365. size = url_fsize(s->pb);
  366. if(size < 0)
  367. return 0;
  368. end = size > MAX_PAGE_SIZE? size - MAX_PAGE_SIZE: 0;
  369. ogg_save (s);
  370. url_fseek (s->pb, end, SEEK_SET);
  371. while (!ogg_read_page (s, &i)){
  372. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  373. ogg->streams[i].codec)
  374. idx = i;
  375. }
  376. if (idx != -1){
  377. s->streams[idx]->duration =
  378. ogg_gptopts (s, idx, ogg->streams[idx].granule);
  379. }
  380. ogg->size = size;
  381. ogg_restore (s, 0);
  382. return 0;
  383. }
  384. static int
  385. ogg_read_header (AVFormatContext * s, AVFormatParameters * ap)
  386. {
  387. struct ogg *ogg = s->priv_data;
  388. ogg->curidx = -1;
  389. //linear headers seek from start
  390. if (ogg_get_headers (s) < 0){
  391. return -1;
  392. }
  393. //linear granulepos seek from end
  394. ogg_get_length (s);
  395. //fill the extradata in the per codec callbacks
  396. return 0;
  397. }
  398. static int
  399. ogg_read_packet (AVFormatContext * s, AVPacket * pkt)
  400. {
  401. struct ogg *ogg;
  402. struct ogg_stream *os;
  403. int idx = -1;
  404. int pstart, psize;
  405. //Get an ogg packet
  406. do{
  407. if (ogg_packet (s, &idx, &pstart, &psize) < 0)
  408. return AVERROR(EIO);
  409. }while (idx < 0 || !s->streams[idx]);
  410. ogg = s->priv_data;
  411. os = ogg->streams + idx;
  412. //Alloc a pkt
  413. if (av_new_packet (pkt, psize) < 0)
  414. return AVERROR(EIO);
  415. pkt->stream_index = idx;
  416. memcpy (pkt->data, os->buf + pstart, psize);
  417. if (os->lastgp != -1LL){
  418. pkt->pts = ogg_gptopts (s, idx, os->lastgp);
  419. os->lastgp = -1;
  420. }
  421. pkt->flags = os->pflags;
  422. return psize;
  423. }
  424. static int
  425. ogg_read_close (AVFormatContext * s)
  426. {
  427. struct ogg *ogg = s->priv_data;
  428. int i;
  429. for (i = 0; i < ogg->nstreams; i++){
  430. av_free (ogg->streams[i].buf);
  431. av_free (ogg->streams[i].private);
  432. }
  433. av_free (ogg->streams);
  434. return 0;
  435. }
  436. static int64_t
  437. ogg_read_timestamp (AVFormatContext * s, int stream_index, int64_t * pos_arg,
  438. int64_t pos_limit)
  439. {
  440. struct ogg *ogg = s->priv_data;
  441. ByteIOContext *bc = s->pb;
  442. int64_t pts = AV_NOPTS_VALUE;
  443. int i;
  444. url_fseek(bc, *pos_arg, SEEK_SET);
  445. while (url_ftell(bc) < pos_limit && !ogg_read_page (s, &i)) {
  446. if (ogg->streams[i].granule != -1 && ogg->streams[i].granule != 0 &&
  447. ogg->streams[i].codec && i == stream_index) {
  448. pts = ogg_gptopts(s, i, ogg->streams[i].granule);
  449. // FIXME: this is the position of the packet after the one with above
  450. // pts.
  451. *pos_arg = url_ftell(bc);
  452. break;
  453. }
  454. }
  455. ogg_reset(ogg);
  456. return pts;
  457. }
  458. static int ogg_probe(AVProbeData *p)
  459. {
  460. if (p->buf[0] == 'O' && p->buf[1] == 'g' &&
  461. p->buf[2] == 'g' && p->buf[3] == 'S' &&
  462. p->buf[4] == 0x0 && p->buf[5] <= 0x7 )
  463. return AVPROBE_SCORE_MAX;
  464. else
  465. return 0;
  466. }
  467. AVInputFormat ogg_demuxer = {
  468. "ogg",
  469. NULL_IF_CONFIG_SMALL("Ogg"),
  470. sizeof (struct ogg),
  471. ogg_probe,
  472. ogg_read_header,
  473. ogg_read_packet,
  474. ogg_read_close,
  475. NULL,
  476. ogg_read_timestamp,
  477. .extensions = "ogg",
  478. };