Revision 95ca3b1e libavformat/oggenc.c

View differences:

libavformat/oggenc.c
27 27
#include "internal.h"
28 28
#include "vorbiscomment.h"
29 29

  
30
#define MAX_PAGE_SIZE 65025
31

  
32
typedef struct {
33
    int64_t granule;
34
    int stream_index;
35
    uint8_t flags;
36
    uint8_t segments_count;
37
    uint8_t segments[255];
38
    uint8_t data[MAX_PAGE_SIZE];
39
    uint16_t size;
40
} OGGPage;
41

  
30 42
typedef struct {
31 43
    int64_t duration;
32 44
    unsigned page_counter;
......
37 49
    int64_t last_kf_pts;
38 50
    int vrev;
39 51
    int eos;
40
    unsigned packet_count; ///< number of packet buffered
52
    unsigned page_count; ///< number of page buffered
53
    OGGPage page; ///< current page
41 54
} OGGStreamContext;
42 55

  
56
typedef struct OGGPageList {
57
    OGGPage page;
58
    struct OGGPageList *next;
59
} OGGPageList;
60

  
61
typedef struct {
62
    OGGPageList *page_list;
63
} OGGContext;
64

  
43 65
static void ogg_update_checksum(AVFormatContext *s, int64_t crc_offset)
44 66
{
45 67
    int64_t pos = url_ftell(s->pb);
......
49 71
    url_fseek(s->pb, pos, SEEK_SET);
50 72
}
51 73

  
52
static int ogg_write_page(AVFormatContext *s, const uint8_t *data, int size,
53
                          int64_t granule, int stream_index, int flags)
74
static void ogg_write_page(AVFormatContext *s, OGGPage *page, int extra_flags)
54 75
{
55
    OGGStreamContext *oggstream = s->streams[stream_index]->priv_data;
76
    OGGStreamContext *oggstream = s->streams[page->stream_index]->priv_data;
56 77
    int64_t crc_offset;
57
    int page_segments, i;
58

  
59
    if (size >= 255*255) {
60
        granule = -1;
61
        size = 255*255;
62
    } else if (oggstream->eos)
63
        flags |= 4;
64

  
65
    page_segments = FFMIN(size/255 + 1, 255);
66 78

  
67 79
    init_checksum(s->pb, ff_crc04C11DB7_update, 0);
68 80
    put_tag(s->pb, "OggS");
69 81
    put_byte(s->pb, 0);
70
    put_byte(s->pb, flags);
71
    put_le64(s->pb, granule);
72
    put_le32(s->pb, stream_index);
82
    put_byte(s->pb, page->flags | extra_flags);
83
    put_le64(s->pb, page->granule);
84
    put_le32(s->pb, page->stream_index);
73 85
    put_le32(s->pb, oggstream->page_counter++);
74 86
    crc_offset = url_ftell(s->pb);
75 87
    put_le32(s->pb, 0); // crc
76
    put_byte(s->pb, page_segments);
77
    for (i = 0; i < page_segments-1; i++)
78
        put_byte(s->pb, 255);
79
    put_byte(s->pb, size - (page_segments-1)*255);
80
    put_buffer(s->pb, data, size);
88
    put_byte(s->pb, page->segments_count);
89
    put_buffer(s->pb, page->segments, page->segments_count);
90
    put_buffer(s->pb, page->data, page->size);
81 91

  
82 92
    ogg_update_checksum(s, crc_offset);
83 93
    put_flush_packet(s->pb);
84
    return size;
94
    oggstream->page_count--;
95
}
96

  
97
static int64_t ogg_granule_to_timestamp(OGGStreamContext *oggstream, OGGPage *page)
98
{
99
    if (oggstream->kfgshift)
100
        return (page->granule>>oggstream->kfgshift) +
101
            (page->granule & ((1<<oggstream->kfgshift)-1));
102
    else
103
        return page->granule;
104
}
105

  
106
static int ogg_compare_granule(AVFormatContext *s, OGGPage *next, OGGPage *page)
107
{
108
    AVStream *st2 = s->streams[next->stream_index];
109
    AVStream *st  = s->streams[page->stream_index];
110
    int64_t next_granule, cur_granule;
111

  
112
    if (next->granule == -1 || page->granule == -1)
113
        return 0;
114

  
115
    next_granule = av_rescale_q(ogg_granule_to_timestamp(st2->priv_data, next),
116
                                st2->time_base, AV_TIME_BASE_Q);
117
    cur_granule  = av_rescale_q(ogg_granule_to_timestamp(st->priv_data, page),
118
                                st ->time_base, AV_TIME_BASE_Q);
119
    return next_granule > cur_granule;
120
}
121

  
122
static int ogg_reset_cur_page(OGGStreamContext *oggstream)
123
{
124
    oggstream->page.granule = -1;
125
    oggstream->page.flags = 0;
126
    oggstream->page.segments_count = 0;
127
    oggstream->page.size = 0;
128
    return 0;
129
}
130

  
131
static int ogg_buffer_page(AVFormatContext *s, OGGStreamContext *oggstream)
132
{
133
    OGGContext *ogg = s->priv_data;
134
    OGGPageList **p = &ogg->page_list;
135
    OGGPageList *l = av_mallocz(sizeof(*l));
136

  
137
    if (!l)
138
        return AVERROR(ENOMEM);
139
    l->page = oggstream->page;
140

  
141
    oggstream->page_count++;
142
    ogg_reset_cur_page(oggstream);
143

  
144
    while (*p) {
145
        if (ogg_compare_granule(s, &(*p)->page, &l->page))
146
            break;
147
        p = &(*p)->next;
148
    }
149
    l->next = *p;
150
    *p = l;
151

  
152
    return 0;
153
}
154

  
155
static int ogg_buffer_data(AVFormatContext *s, AVStream *st,
156
                           uint8_t *data, unsigned size, int64_t granule)
157
{
158
    OGGStreamContext *oggstream = st->priv_data;
159
    int total_segments = size / 255 + 1;
160
    uint8_t *p = data;
161
    int i, segments, len;
162

  
163
    for (i = 0; i < total_segments; ) {
164
        OGGPage *page = &oggstream->page;
165

  
166
        segments = FFMIN(total_segments - i, 255 - page->segments_count);
167

  
168
        if (i && !page->segments_count)
169
            page->flags |= 1; // continued packet
170

  
171
        memset(page->segments+page->segments_count, 255, segments - 1);
172
        page->segments_count += segments - 1;
173

  
174
        len = FFMIN(size, segments*255);
175
        page->segments[page->segments_count++] = len - (segments-1)*255;
176
        memcpy(page->data+page->size, p, len);
177
        p += len;
178
        size -= len;
179
        i += segments;
180
        page->size += len;
181

  
182
        if (i == total_segments)
183
            page->granule = granule;
184

  
185
        if (page->segments_count == 255) {
186
            ogg_buffer_page(s, oggstream);
187
        }
188
    }
189
    return 0;
85 190
}
86 191

  
87 192
static uint8_t *ogg_write_vorbiscomment(int offset, int bitexact,
......
195 300
            return -1;
196 301
        }
197 302
        oggstream = av_mallocz(sizeof(*oggstream));
303
        oggstream->page.stream_index = i;
198 304
        st->priv_data = oggstream;
199 305
        if (st->codec->codec_id == CODEC_ID_FLAC) {
200 306
            int err = ogg_build_flac_headers(st->codec, oggstream,
......
232 338
            }
233 339
        }
234 340
    }
235
    for (i = 0; i < 3; i++) {
236
        for (j = 0; j < s->nb_streams; j++) {
237
            AVStream *st = s->streams[j];
238
            OGGStreamContext *oggstream = st->priv_data;
239
            if (oggstream && oggstream->header_len[i]) {
240
                ogg_write_page(s, oggstream->header[i], oggstream->header_len[i],
241
                               0, st->index, i ? 0 : 2); // bos
242
            }
341

  
342
    for (j = 0; j < s->nb_streams; j++) {
343
        OGGStreamContext *oggstream = s->streams[j]->priv_data;
344
        ogg_buffer_data(s, s->streams[j], oggstream->header[0],
345
                        oggstream->header_len[0], 0);
346
        oggstream->page.flags |= 2; // bos
347
        ogg_buffer_page(s, oggstream);
348
    }
349
    for (j = 0; j < s->nb_streams; j++) {
350
        AVStream *st = s->streams[j];
351
        OGGStreamContext *oggstream = st->priv_data;
352
        for (i = 1; i < 3; i++) {
353
            if (oggstream && oggstream->header_len[i])
354
                ogg_buffer_data(s, st, oggstream->header[i],
355
                                oggstream->header_len[i], 0);
243 356
        }
357
        ogg_buffer_page(s, oggstream);
244 358
    }
245 359
    return 0;
246 360
}
247 361

  
362
static void ogg_write_pages(AVFormatContext *s, int flush)
363
{
364
    OGGContext *ogg = s->priv_data;
365
    OGGPageList *next, *p;
366

  
367
    if (!ogg->page_list)
368
        return;
369

  
370
    for (p = ogg->page_list; p; ) {
371
        OGGStreamContext *oggstream =
372
            s->streams[p->page.stream_index]->priv_data;
373
        if (oggstream->page_count < 2 && !flush)
374
            break;
375
        ogg_write_page(s, &p->page,
376
                       flush && oggstream->page_count == 1 ? 4 : 0); // eos
377
        next = p->next;
378
        av_freep(&p);
379
        p = next;
380
    }
381
    ogg->page_list = p;
382
}
383

  
248 384
static int ogg_write_packet(AVFormatContext *s, AVPacket *pkt)
249 385
{
250 386
    AVStream *st = s->streams[pkt->stream_index];
251 387
    OGGStreamContext *oggstream = st->priv_data;
252
    uint8_t *ptr = pkt->data;
253
    int ret, size = pkt->size;
388
    int ret;
254 389
    int64_t granule;
255 390

  
256 391
    if (st->codec->codec_id == CODEC_ID_THEORA) {
......
268 403
    } else
269 404
        granule = pkt->pts + pkt->duration;
270 405
    oggstream->duration = granule;
271
    do {
272
        ret = ogg_write_page(s, ptr, size, granule, pkt->stream_index, ptr != pkt->data);
273
        ptr  += ret; size -= ret;
274
    } while (size > 0 || ret == 255*255); // need to output a last nil page
275 406

  
276
    return 0;
277
}
407
    ret = ogg_buffer_data(s, st, pkt->data, pkt->size, granule);
408
    if (ret < 0)
409
        return ret;
278 410

  
279
static int ogg_compare_granule(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
280
{
281
    AVStream *st2 = s->streams[next->stream_index];
282
    AVStream *st  = s->streams[pkt ->stream_index];
411
    ogg_write_pages(s, 0);
283 412

  
284
    int64_t next_granule = av_rescale_q(next->pts + next->duration,
285
                                        st2->time_base, AV_TIME_BASE_Q);
286
    int64_t cur_granule  = av_rescale_q(pkt ->pts + pkt ->duration,
287
                                        st ->time_base, AV_TIME_BASE_Q);
288
    return next_granule > cur_granule;
413
    return 0;
289 414
}
290 415

  
291
static int ogg_interleave_per_granule(AVFormatContext *s, AVPacket *out, AVPacket *pkt, int flush)
416
static int ogg_write_trailer(AVFormatContext *s)
292 417
{
293
    OGGStreamContext *ogg;
294
    int i, stream_count = 0;
295
    int interleaved = 0;
296

  
297
    if (pkt) {
298
        ff_interleave_add_packet(s, pkt, ogg_compare_granule);
299
        ogg = s->streams[pkt->stream_index]->priv_data;
300
        ogg->packet_count++;
301
    }
302

  
303
    for (i = 0; i < s->nb_streams; i++) {
304
        ogg = s->streams[i]->priv_data;
305
        stream_count += !!ogg->packet_count;
306
        interleaved += ogg->packet_count > 1;
307
    }
308

  
309
    if ((s->nb_streams == stream_count && interleaved == stream_count) ||
310
        (flush && stream_count)) {
311
        AVPacketList *pktl= s->packet_buffer;
312
        *out= pktl->pkt;
313
        s->packet_buffer = pktl->next;
314

  
315
        ogg = s->streams[out->stream_index]->priv_data;
316
        if (flush && ogg->packet_count == 1)
317
            ogg->eos = 1;
318
        ogg->packet_count--;
418
    int i;
319 419

  
320
        if(!s->packet_buffer)
321
            s->packet_buffer_end= NULL;
420
    /* flush current page */
421
    for (i = 0; i < s->nb_streams; i++)
422
        ogg_buffer_page(s, s->streams[i]->priv_data);
322 423

  
323
        if(s->streams[out->stream_index]->last_in_packet_buffer == pktl)
324
            s->streams[out->stream_index]->last_in_packet_buffer= NULL;
424
    ogg_write_pages(s, 1);
325 425

  
326
        av_freep(&pktl);
327
        return 1;
328
    } else {
329
        av_init_packet(out);
330
        return 0;
331
    }
332
}
333

  
334
static int ogg_write_trailer(AVFormatContext *s)
335
{
336
    int i;
337 426
    for (i = 0; i < s->nb_streams; i++) {
338 427
        AVStream *st = s->streams[i];
339 428
        OGGStreamContext *oggstream = st->priv_data;
......
352 441
    NULL_IF_CONFIG_SMALL("Ogg"),
353 442
    "application/ogg",
354 443
    "ogg,ogv,spx",
355
    0,
444
    sizeof(OGGContext),
356 445
    CODEC_ID_FLAC,
357 446
    CODEC_ID_THEORA,
358 447
    ogg_write_header,
359 448
    ogg_write_packet,
360 449
    ogg_write_trailer,
361
    .interleave_packet = ogg_interleave_per_granule,
362 450
    .metadata_conv = ff_vorbiscomment_metadata_conv,
363 451
};

Also available in: Unified diff