History

ogg/vorbis: implement header packet skip in chained ogg bitstreams.

Romain Beauxis committed 1 year ago

Commit 574f6341 parent c9cc61d4 files · +119 −37Browse files

Unified · Split

4 files
  1. libavcodec/vorbis_parser.h +11 −0
  2. libavcodec/vorbisdec.c +44 −31
  3. libavformat/oggparsevorbis.c +64 −3
  4. tests/ref/fate/ogg-vorbis-chained-meta.txt +0 −3
libavcodec/vorbis_parser.h+11−0
@@ -30,6 +30,17 @@ · 29 unmodified lines
3030
3131 typedef struct AVVorbisParseContext AVVorbisParseContext;
3232
33+/**
34+ * Used by the vorbis parser to pass new chained stream headers
35+ * as extradata.
36+ */
37+typedef struct vorbis_new_extradata {
38+ uint8_t *header;
39+ size_t header_size;
40+ uint8_t *setup;
41+ size_t setup_size;
42+} vorbis_new_extradata;
43+
3344 /**
3445 * Allocate and initialize the Vorbis parser using headers in the extradata.
3546 */
libavcodec/vorbisdec.c+44−31
@@ -43,6 +43,7 @@ · 42 unmodified lines
4343 #include "vorbis.h"
4444 #include "vorbisdsp.h"
4545 #include "vorbis_data.h"
46+#include "vorbis_parser.h"
4647 #include "xiph.h"
4748
4849 #define V_NB_BITS 8
@@ -1778,47 +1779,59 @@ static int vorbis_decode_frame(AVCodecContext *avctx, AVFrame *frame, · 1729 unmodified lines
17781779 GetBitContext *gb = &vc->gb;
17791780 float *channel_ptrs[255];
17801781 int i, len, ret;
1782+ size_t new_extradata_size;
1783+ vorbis_new_extradata *new_extradata;
1784+ const uint8_t *header;
1785+ const uint8_t *setup;
17811786
17821787 ff_dlog(NULL, "packet length %d \n", buf_size);
17831788
1784− if (*buf == 1 && buf_size > 7) {
1785− if ((ret = init_get_bits8(gb, buf + 1, buf_size - 1)) < 0)
1786− return ret;
1789+ new_extradata = (vorbis_new_extradata *)av_packet_get_side_data(
1790+ avpkt, AV_PKT_DATA_NEW_EXTRADATA, &new_extradata_size);
17871791
1788− vorbis_free(vc);
1789− if ((ret = vorbis_parse_id_hdr(vc))) {
1790− av_log(avctx, AV_LOG_ERROR, "Id header corrupt.\n");
1791− vorbis_free(vc);
1792− return ret;
1793− }
1792+ if (new_extradata) {
1793+ header = new_extradata->header;
1794+ setup = new_extradata->setup;
17941795
1795− av_channel_layout_uninit(&avctx->ch_layout);
1796− if (vc->audio_channels > 8) {
1797− avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
1798− avctx->ch_layout.nb_channels = vc->audio_channels;
1799− } else {
1800− av_channel_layout_copy(&avctx->ch_layout, &ff_vorbis_ch_layouts[vc->audio_channels - 1]);
1801− }
1796+ if (new_extradata->header_size > 7 && *header == 1) {
1797+ if ((ret = init_get_bits8(
1798+ gb, header + 1,
1799+ new_extradata->header_size - 1)) < 0)
1800+ return ret;
18021801
1803− avctx->sample_rate = vc->audio_samplerate;
1804− return buf_size;
1805− }
1802+ vorbis_free(vc);
1803+ if ((ret = vorbis_parse_id_hdr(vc))) {
1804+ av_log(avctx, AV_LOG_ERROR, "Id header corrupt.\n");
1805+ vorbis_free(vc);
1806+ return ret;
1807+ }
18061808
1807− if (*buf == 3 && buf_size > 7) {
1808− av_log(avctx, AV_LOG_DEBUG, "Ignoring comment header\n");
1809− return buf_size;
1810− }
1809+ av_channel_layout_uninit(&avctx->ch_layout);
1810+ if (vc->audio_channels > 8) {
1811+ avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
1812+ avctx->ch_layout.nb_channels = vc->audio_channels;
1813+ } else {
1814+ av_channel_layout_copy(
1815+ &avctx->ch_layout,
1816+ &ff_vorbis_ch_layouts[vc->audio_channels - 1]);
1817+ }
18111818
1812− if (*buf == 5 && buf_size > 7 && vc->channel_residues && !vc->modes) {
1813− if ((ret = init_get_bits8(gb, buf + 1, buf_size - 1)) < 0)
1814− return ret;
1819+ avctx->sample_rate = vc->audio_samplerate;
1820+ }
18151821
1816− if ((ret = vorbis_parse_setup_hdr(vc))) {
1817− av_log(avctx, AV_LOG_ERROR, "Setup header corrupt.\n");
1818− vorbis_free(vc);
1819− return ret;
1822+ if (new_extradata->setup_size > 7 && *setup == 5 &&
1823+ vc->channel_residues && !vc->modes) {
1824+ if ((ret = init_get_bits8(
1825+ gb, setup + 1,
1826+ new_extradata->setup_size - 1)) < 0)
1827+ return ret;
1828+
1829+ if ((ret = vorbis_parse_setup_hdr(vc))) {
1830+ av_log(avctx, AV_LOG_ERROR, "Setup header corrupt.\n");
1831+ vorbis_free(vc);
1832+ return ret;
1833+ }
18201834 }
1821− return buf_size;
18221835 }
18231836
18241837 if (!vc->channel_residues || !vc->modes) {
libavformat/oggparsevorbis.c+64−3
@@ -255,12 +255,19 @@ static void vorbis_cleanup(AVFormatContext *s, int idx) · 254 unmodified lines
255255 struct ogg *ogg = s->priv_data;
256256 struct ogg_stream *os = ogg->streams + idx;
257257 struct oggvorbis_private *priv = os->private;
258+ vorbis_new_extradata *new_extradata;
258259 int i;
259260 if (os->private) {
260261 av_vorbis_parse_free(&priv->vp);
261262 for (i = 0; i < 3; i++)
262263 av_freep(&priv->packet[i]);
263264 }
265+
266+ if (os->new_extradata) {
267+ new_extradata = (vorbis_new_extradata *)os->new_extradata;
268+ av_freep(&new_extradata->header);
269+ av_freep(&new_extradata->setup);
270+ }
264271 }
265272
266273 static int vorbis_update_metadata(AVFormatContext *s, int idx)
@@ -433,7 +440,10 @@ static int vorbis_packet(AVFormatContext *s, int idx) · 166 unmodified lines
433440 struct ogg *ogg = s->priv_data;
434441 struct ogg_stream *os = ogg->streams + idx;
435442 struct oggvorbis_private *priv = os->private;
443+ vorbis_new_extradata *new_extradata;
436444 int duration, flags = 0;
445+ int skip_packet = 0;
446+ int ret;
437447
438448 if (!priv->vp)
439449 return AVERROR_INVALIDDATA;
@@ -496,10 +506,61 @@ static int vorbis_packet(AVFormatContext *s, int idx) · 56 unmodified lines
496506 if (duration < 0) {
497507 os->pflags |= AV_PKT_FLAG_CORRUPT;
498508 return 0;
499− } else if (flags & VORBIS_FLAG_COMMENT) {
500− vorbis_update_metadata(s, idx);
509+ }
510+
511+ if (flags & VORBIS_FLAG_HEADER) {
512+ ret = vorbis_parse_header(s, s->streams[idx], os->buf + os->pstart, os->psize);
513+ if (ret < 0)
514+ return ret;
515+
516+ if (!os->new_extradata) {
517+ os->new_extradata = av_mallocz(sizeof(vorbis_new_extradata));
518+ if (!os->new_extradata)
519+ return AVERROR(ENOMEM);
520+ }
521+
522+ os->new_extradata_size = sizeof(vorbis_new_extradata);
523+ new_extradata = (vorbis_new_extradata *)os->new_extradata;
524+
525+ ret = av_reallocp(&new_extradata->header, os->psize);
526+ if (ret < 0)
527+ return ret;
528+
529+ memcpy(new_extradata->header, os->buf + os->pstart, os->psize);
530+ new_extradata->header_size = os->psize;
531+
532+ skip_packet = 1;
533+ }
534+
535+ if (flags & VORBIS_FLAG_COMMENT) {
536+ ret = vorbis_update_metadata(s, idx);
537+ if (ret < 0)
538+ return ret;
539+
501540 flags = 0;
541+ skip_packet = 1;
542+ }
543+
544+ if (flags & VORBIS_FLAG_SETUP) {
545+ if (!os->new_extradata) {
546+ os->new_extradata = av_mallocz(sizeof(vorbis_new_extradata));
547+ if (!os->new_extradata)
548+ return AVERROR(ENOMEM);
549+ }
550+
551+ os->new_extradata_size = sizeof(vorbis_new_extradata);
552+ new_extradata = (vorbis_new_extradata *)os->new_extradata;
553+
554+ ret = av_reallocp(&new_extradata->setup, os->psize);
555+ if (ret < 0)
556+ return ret;
557+
558+ memcpy(new_extradata->setup, os->buf + os->pstart, os->psize);
559+ new_extradata->setup_size = os->psize;
560+
561+ skip_packet = 1;
502562 }
563+
503564 os->pduration = duration;
504565 }
505566
@@ -521,7 +582,7 @@ static int vorbis_packet(AVFormatContext *s, int idx) · 15 unmodified lines
521582 priv->final_duration += os->pduration;
522583 }
523584
524− return 0;
585+ return skip_packet;
525586 }
526587
527588 const struct ogg_codec ff_vorbis_codec = {
tests/ref/fate/ogg-vorbis-chained-meta.txt+0−3
@@ -6,10 +6,7 @@ Stream ID: 0, frame PTS: 128, metadata: N/A · 5 unmodified lines
66 Stream ID: 0, packet PTS: 704, packet DTS: 704
77 Stream ID: 0, frame PTS: 704, metadata: N/A
88 Stream ID: 0, packet PTS: 0, packet DTS: 0
9−Stream ID: 0, packet PTS: 0, packet DTS: 0
109 Stream ID: 0, new metadata: encoder=Lavc61.19.100 libvorbis:title=Second Stream
11−Stream ID: 0, packet PTS: 0, packet DTS: 0
12−Stream ID: 0, packet PTS: 0, packet DTS: 0
1310 Stream ID: 0, frame PTS: 0, metadata: N/A
1411 Stream ID: 0, packet PTS: 128, packet DTS: 128
1512 Stream ID: 0, frame PTS: 128, metadata: N/A

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t