| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Discworld II BMV demuxer | ||
| 3 | * Copyright (c) 2011 Konstantin Shishkov | ||
| 4 | * | ||
| 5 | * This file is part of FFmpeg. | ||
| 6 | * | ||
| 7 | * FFmpeg is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with FFmpeg; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "libavutil/channel_layout.h" | ||
| 23 | #include "libavutil/mem.h" | ||
| 24 | #include "avformat.h" | ||
| 25 | #include "demux.h" | ||
| 26 | #include "internal.h" | ||
| 27 | |||
| 28 | enum BMVFlags { | ||
| 29 | BMV_NOP = 0, | ||
| 30 | BMV_END, | ||
| 31 | BMV_DELTA, | ||
| 32 | BMV_INTRA, | ||
| 33 | |||
| 34 | BMV_AUDIO = 0x20, | ||
| 35 | }; | ||
| 36 | |||
| 37 | typedef struct BMVContext { | ||
| 38 | uint8_t *packet; | ||
| 39 | int size; | ||
| 40 | int get_next; | ||
| 41 | int64_t audio_pos; | ||
| 42 | } BMVContext; | ||
| 43 | |||
| 44 | 2 | static int bmv_read_header(AVFormatContext *s) | |
| 45 | { | ||
| 46 | AVStream *st, *ast; | ||
| 47 | 2 | BMVContext *c = s->priv_data; | |
| 48 | |||
| 49 | 2 | st = avformat_new_stream(s, 0); | |
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!st) |
| 51 | ✗ | return AVERROR(ENOMEM); | |
| 52 | 2 | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
| 53 | 2 | st->codecpar->codec_id = AV_CODEC_ID_BMV_VIDEO; | |
| 54 | 2 | st->codecpar->width = 640; | |
| 55 | 2 | st->codecpar->height = 429; | |
| 56 | 2 | st->codecpar->format = AV_PIX_FMT_PAL8; | |
| 57 | 2 | avpriv_set_pts_info(st, 16, 1, 12); | |
| 58 | 2 | ast = avformat_new_stream(s, 0); | |
| 59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!ast) |
| 60 | ✗ | return AVERROR(ENOMEM); | |
| 61 | 2 | ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 62 | 2 | ast->codecpar->codec_id = AV_CODEC_ID_BMV_AUDIO; | |
| 63 | 2 | ast->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; | |
| 64 | 2 | ast->codecpar->sample_rate = 22050; | |
| 65 | 2 | avpriv_set_pts_info(ast, 16, 1, 22050); | |
| 66 | |||
| 67 | 2 | c->get_next = 1; | |
| 68 | 2 | c->audio_pos = 0; | |
| 69 | 2 | return 0; | |
| 70 | } | ||
| 71 | |||
| 72 | 88 | static int bmv_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 73 | { | ||
| 74 | 88 | BMVContext *c = s->priv_data; | |
| 75 | int type, err; | ||
| 76 | |||
| 77 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 42 times.
|
88 | while (c->get_next) { |
| 78 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 44 times.
|
46 | if (s->pb->eof_reached) |
| 79 | 2 | return AVERROR_EOF; | |
| 80 | 44 | type = avio_r8(s->pb); | |
| 81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | if (type == BMV_NOP) |
| 82 | ✗ | continue; | |
| 83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | if (type == BMV_END) |
| 84 | ✗ | return AVERROR_EOF; | |
| 85 | 44 | c->size = avio_rl24(s->pb); | |
| 86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
|
44 | if (!c->size) |
| 87 | ✗ | return AVERROR_INVALIDDATA; | |
| 88 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
|
44 | if ((err = av_reallocp(&c->packet, c->size + 1)) < 0) |
| 89 | ✗ | return err; | |
| 90 | 44 | c->packet[0] = type; | |
| 91 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 42 times.
|
44 | if (avio_read(s->pb, c->packet + 1, c->size) != c->size) |
| 92 | 2 | return AVERROR(EIO); | |
| 93 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | if (type & BMV_AUDIO) { |
| 94 | 42 | int audio_size = c->packet[1] * 65 + 1; | |
| 95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
|
42 | if (audio_size >= c->size) { |
| 96 | ✗ | av_log(s, AV_LOG_ERROR, "Reported audio size %d is bigger than packet size (%d)\n", | |
| 97 | audio_size, c->size); | ||
| 98 | ✗ | return AVERROR_INVALIDDATA; | |
| 99 | } | ||
| 100 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
|
42 | if ((err = av_new_packet(pkt, audio_size)) < 0) |
| 101 | ✗ | return err; | |
| 102 | 42 | memcpy(pkt->data, c->packet + 1, pkt->size); | |
| 103 | 42 | pkt->stream_index = 1; | |
| 104 | 42 | pkt->pts = c->audio_pos; | |
| 105 | 42 | pkt->duration = c->packet[1] * 32; | |
| 106 | 42 | c->audio_pos += pkt->duration; | |
| 107 | 42 | c->get_next = 0; | |
| 108 | 42 | return pkt->size; | |
| 109 | } else | ||
| 110 | ✗ | break; | |
| 111 | } | ||
| 112 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
|
42 | if ((err = av_new_packet(pkt, c->size + 1)) < 0) |
| 113 | ✗ | return err; | |
| 114 | 42 | pkt->stream_index = 0; | |
| 115 | 42 | c->get_next = 1; | |
| 116 | 42 | memcpy(pkt->data, c->packet, pkt->size); | |
| 117 | 42 | return pkt->size; | |
| 118 | } | ||
| 119 | |||
| 120 | 2 | static int bmv_read_close(AVFormatContext *s) | |
| 121 | { | ||
| 122 | 2 | BMVContext *c = s->priv_data; | |
| 123 | |||
| 124 | 2 | av_freep(&c->packet); | |
| 125 | |||
| 126 | 2 | return 0; | |
| 127 | } | ||
| 128 | |||
| 129 | const FFInputFormat ff_bmv_demuxer = { | ||
| 130 | .p.name = "bmv", | ||
| 131 | .p.long_name = NULL_IF_CONFIG_SMALL("Discworld II BMV"), | ||
| 132 | .p.extensions = "bmv", | ||
| 133 | .priv_data_size = sizeof(BMVContext), | ||
| 134 | .read_header = bmv_read_header, | ||
| 135 | .read_packet = bmv_read_packet, | ||
| 136 | .read_close = bmv_read_close, | ||
| 137 | }; | ||
| 138 |