static int mkv_write_ass_blocks(AVFormatContext *s, AVIOContext *pb,

                                AVPacket *pkt)

{

    MatroskaMuxContext *mkv = s->priv_data;

    int i, layer = 0, max_duration = 0, size, line_size, data_size = pkt->size;

    uint8_t *start, *end, *data = pkt->data;

    ebml_master blockgroup;

    char buffer[2048];



    while (data_size) {

        int duration = ass_get_duration(data);

        max_duration = FFMAX(duration, max_duration);

        end          = memchr(data, '\n', data_size);

        size         = line_size = end ? end - data + 1 : data_size;

        size        -= end ? (end[-1] == '\r') + 1 : 0;

        start        = data;

        for (i = 0; i < 3; i++, start++)

            if (!(start = memchr(start, ',', size - (start - data))))

                return max_duration;

        size -= start - data;

        sscanf(data, "Dialogue: %d,", &layer);

        i = snprintf(buffer, sizeof(buffer), "%" PRId64 ",%d,",

                     s->streams[pkt->stream_index]->nb_frames, layer);

        size = FFMIN(i + size, sizeof(buffer));

        memcpy(buffer + i, start, size - i);



        av_log(s, AV_LOG_DEBUG,

               "Writing block at offset %" PRIu64 ", size %d, "

               "pts %" PRId64 ", duration %d\n",

               avio_tell(pb), size, pkt->pts, duration);

        blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,

                                       mkv_blockgroup_size(size));

        put_ebml_id(pb, MATROSKA_ID_BLOCK);

        put_ebml_num(pb, size + 4, 0);

        // this assumes stream_index is less than 126

        avio_w8(pb, 0x80 | (pkt->stream_index + 1));

        avio_wb16(pb, pkt->pts - mkv->cluster_pts);

        avio_w8(pb, 0);

        avio_write(pb, buffer, size);

        put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);

        end_ebml_master(pb, blockgroup);



        data      += line_size;

        data_size -= line_size;

    }



    return max_duration;

}

static int mkv_write_ass_blocks(AVFormatContext *s, AVIOContext *pb,

                                AVPacket *pkt)

{

    MatroskaMuxContext *mkv = s->priv_data;

    int i, layer = 0, max_duration = 0, size, line_size, data_size = pkt->size;

    uint8_t *start, *end, *data = pkt->data;

    ebml_master blockgroup;

    char buffer[2048];



    while (data_size) {

        int duration = ass_get_duration(data);

        max_duration = FFMAX(duration, max_duration);

        end          = memchr(data, '\n', data_size);

        size         = line_size = end ? end - data + 1 : data_size;

        size        -= end ? (end[-1] == '\r') + 1 : 0;

        start        = data;

        for (i = 0; i < 3; i++, start++)

            if (!(start = memchr(start, ',', size - (start - data))))

                return max_duration;

        size -= start - data;

        sscanf(data, "Dialogue: %d,", &layer);

        i = snprintf(buffer, sizeof(buffer), "%" PRId64 ",%d,",

                     s->streams[pkt->stream_index]->nb_frames, layer);

        size = FFMIN(i + size, sizeof(buffer));

        memcpy(buffer + i, start, size - i);



        av_log(s, AV_LOG_DEBUG,

               "Writing block at offset %" PRIu64 ", size %d, "

               "pts %" PRId64 ", duration %d\n",

               avio_tell(pb), size, pkt->pts, duration);

        blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,

                                       mkv_blockgroup_size(size));

        put_ebml_id(pb, MATROSKA_ID_BLOCK);

        put_ebml_num(pb, size + 4, 0);

        

        avio_w8(pb, 0x80 | (pkt->stream_index + 1));

        avio_wb16(pb, pkt->pts - mkv->cluster_pts);

        avio_w8(pb, 0);

        avio_write(pb, buffer, size);

        put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);

        end_ebml_master(pb, blockgroup);



        data      += line_size;

        data_size -= line_size;

    }



    return max_duration;

}

static int FUNC_0(AVFormatContext *VAR_0, AVIOContext *VAR_1,

                                AVPacket *VAR_2)

{

    MatroskaMuxContext *mkv = VAR_0->priv_data;

    int VAR_3, VAR_4 = 0, VAR_5 = 0, VAR_6, VAR_7, VAR_8 = VAR_2->VAR_6;

    uint8_t *start, *end, *data = VAR_2->data;

    ebml_master blockgroup;

    char VAR_9[2048];



    while (VAR_8) {

        int VAR_10 = ass_get_duration(data);

        VAR_5 = FFMAX(VAR_10, VAR_5);

        end          = memchr(data, '\n', VAR_8);

        VAR_6         = VAR_7 = end ? end - data + 1 : VAR_8;

        VAR_6        -= end ? (end[-1] == '\r') + 1 : 0;

        start        = data;

        for (VAR_3 = 0; VAR_3 < 3; VAR_3++, start++)

            if (!(start = memchr(start, ',', VAR_6 - (start - data))))

                return VAR_5;

        VAR_6 -= start - data;

        sscanf(data, "Dialogue: %d,", &VAR_4);

        VAR_3 = snprintf(VAR_9, sizeof(VAR_9), "%" PRId64 ",%d,",

                     VAR_0->streams[VAR_2->stream_index]->nb_frames, VAR_4);

        VAR_6 = FFMIN(VAR_3 + VAR_6, sizeof(VAR_9));

        memcpy(VAR_9 + VAR_3, start, VAR_6 - VAR_3);



        av_log(VAR_0, AV_LOG_DEBUG,

               "Writing block at offset %" PRIu64 ", VAR_6 %d, "

               "pts %" PRId64 ", VAR_10 %d\n",

               avio_tell(VAR_1), VAR_6, VAR_2->pts, VAR_10);

        blockgroup = start_ebml_master(VAR_1, MATROSKA_ID_BLOCKGROUP,

                                       mkv_blockgroup_size(VAR_6));

        put_ebml_id(VAR_1, MATROSKA_ID_BLOCK);

        put_ebml_num(VAR_1, VAR_6 + 4, 0);

        

        avio_w8(VAR_1, 0x80 | (VAR_2->stream_index + 1));

        avio_wb16(VAR_1, VAR_2->pts - mkv->cluster_pts);

        avio_w8(VAR_1, 0);

        avio_write(VAR_1, VAR_9, VAR_6);

        put_ebml_uint(VAR_1, MATROSKA_ID_BLOCKDURATION, VAR_10);

        end_ebml_master(VAR_1, blockgroup);



        data      += VAR_7;

        VAR_8 -= VAR_7;

    }



    return VAR_5;

}

static int FUNC_0(AVFormatContext *VAR_0, AVIOContext *VAR_1,
AVPacket *VAR_2)
{ MatroskaMuxContext *mkv = VAR_0->priv_data; int VAR_3, VAR_4 = 0, VAR_5 = 0, VAR_6, VAR_7, VAR_8 = VAR_2->VAR_6; uint8_t *start, *end, *data = VAR_2->data; ebml_master blockgroup; char VAR_9[2048]; while (VAR_8) { int VAR_10 = ass_get_duration(data); VAR_5 = FFMAX(VAR_10, VAR_5); end          = memchr(data, '\n', VAR_8); VAR_6         = VAR_7 = end ? end - data + 1 : VAR_8; VAR_6        -= end ? (end[-1] == '\r') + 1 : 0; start        = data; for (VAR_3 = 0; VAR_3 < 3; VAR_3++, start++) if (!(start = memchr(start, ',', VAR_6 - (start - data))))
return VAR_5; VAR_6 -= start - data; sscanf(data, "Dialogue: %d,", &VAR_4); VAR_3 = snprintf(VAR_9, sizeof(VAR_9), "%" PRId64 ",%d,",
VAR_0->streams[VAR_2->stream_index]->nb_frames, VAR_4); VAR_6 = FFMIN(VAR_3 + VAR_6, sizeof(VAR_9)); memcpy(VAR_9 + VAR_3, start, VAR_6 - VAR_3); av_log(VAR_0, AV_LOG_DEBUG,
"Writing block at offset %" PRIu64 ", VAR_6 %d, "
"pts %" PRId64 ", VAR_10 %d\n",
avio_tell(VAR_1), VAR_6, VAR_2->pts, VAR_10); blockgroup = start_ebml_master(VAR_1, MATROSKA_ID_BLOCKGROUP,
mkv_blockgroup_size(VAR_6)); put_ebml_id(VAR_1, MATROSKA_ID_BLOCK); put_ebml_num(VAR_1, VAR_6 + 4, 0); avio_w8(VAR_1, 0x80 | (VAR_2->stream_index + 1)); avio_wb16(VAR_1, VAR_2->pts - mkv->cluster_pts); avio_w8(VAR_1, 0); avio_write(VAR_1, VAR_9, VAR_6); put_ebml_uint(VAR_1, MATROSKA_ID_BLOCKDURATION, VAR_10); end_ebml_master(VAR_1, blockgroup); data      += VAR_7; VAR_8 -= VAR_7; } return VAR_5; }