| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Windows Television (WTV) muxer | ||
| 3 | * Copyright (c) 2011 Zhentan Feng <spyfeng at gmail dot com> | ||
| 4 | * Copyright (c) 2011 Peter Ross <pross@xvid.org> | ||
| 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 | /** | ||
| 23 | * @file | ||
| 24 | * Windows Television (WTV) demuxer | ||
| 25 | * @author Zhentan Feng <spyfeng at gmail dot com> | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include "libavutil/avassert.h" | ||
| 29 | #include "libavutil/mem.h" | ||
| 30 | #include "avformat.h" | ||
| 31 | #include "avio_internal.h" | ||
| 32 | #include "internal.h" | ||
| 33 | #include "mpegts.h" | ||
| 34 | #include "mux.h" | ||
| 35 | #include "wtv.h" | ||
| 36 | |||
| 37 | #define WTV_BIGSECTOR_SIZE (1 << WTV_BIGSECTOR_BITS) | ||
| 38 | #define INDEX_BASE 0x2 | ||
| 39 | #define MAX_NB_INDEX 10 | ||
| 40 | |||
| 41 | /* declare utf16le strings */ | ||
| 42 | #define _ , 0, | ||
| 43 | static const uint8_t timeline_table_0_header_events[] = | ||
| 44 | {'t'_'i'_'m'_'e'_'l'_'i'_'n'_'e'_'.'_'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'h'_'e'_'a'_'d'_'e'_'r'_'.'_'E'_'v'_'e'_'n'_'t'_'s', 0}; | ||
| 45 | static const uint8_t table_0_header_legacy_attrib[] = | ||
| 46 | {'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'h'_'e'_'a'_'d'_'e'_'r'_'.'_'l'_'e'_'g'_'a'_'c'_'y'_'_'_'a'_'t'_'t'_'r'_'i'_'b', 0}; | ||
| 47 | static const uint8_t table_0_redirector_legacy_attrib[] = | ||
| 48 | {'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'r'_'e'_'d'_'i'_'r'_'e'_'c'_'t'_'o'_'r'_'.'_'l'_'e'_'g'_'a'_'c'_'y'_'_'_'a'_'t'_'t'_'r'_'i'_'b', 0}; | ||
| 49 | static const uint8_t table_0_header_time[] = | ||
| 50 | {'t'_'a'_'b'_'l'_'e'_'.'_'0'_'.'_'h'_'e'_'a'_'d'_'e'_'r'_'.'_'t'_'i'_'m'_'e', 0}; | ||
| 51 | static const uint8_t legacy_attrib[] = | ||
| 52 | {'l'_'e'_'g'_'a'_'c'_'y'_'_'_'a'_'t'_'t'_'r'_'i'_'b', 0}; | ||
| 53 | #undef _ | ||
| 54 | |||
| 55 | static const ff_asf_guid sub_wtv_guid = | ||
| 56 | {0x8C,0xC3,0xD2,0xC2,0x7E,0x9A,0xDA,0x11,0x8B,0xF7,0x00,0x07,0xE9,0x5E,0xAD,0x8D}; | ||
| 57 | |||
| 58 | enum WtvFileIndex { | ||
| 59 | WTV_TIMELINE_TABLE_0_HEADER_EVENTS = 0, | ||
| 60 | WTV_TIMELINE_TABLE_0_ENTRIES_EVENTS, | ||
| 61 | WTV_TIMELINE, | ||
| 62 | WTV_TABLE_0_HEADER_LEGACY_ATTRIB, | ||
| 63 | WTV_TABLE_0_ENTRIES_LEGACY_ATTRIB, | ||
| 64 | WTV_TABLE_0_REDIRECTOR_LEGACY_ATTRIB, | ||
| 65 | WTV_TABLE_0_HEADER_TIME, | ||
| 66 | WTV_TABLE_0_ENTRIES_TIME, | ||
| 67 | WTV_FILES | ||
| 68 | }; | ||
| 69 | |||
| 70 | typedef struct { | ||
| 71 | int64_t length; | ||
| 72 | const void *header; | ||
| 73 | int depth; | ||
| 74 | int first_sector; | ||
| 75 | } WtvFile; | ||
| 76 | |||
| 77 | typedef struct { | ||
| 78 | int64_t pos; | ||
| 79 | int64_t serial; | ||
| 80 | const ff_asf_guid * guid; | ||
| 81 | int stream_id; | ||
| 82 | } WtvChunkEntry; | ||
| 83 | |||
| 84 | typedef struct { | ||
| 85 | int64_t serial; | ||
| 86 | int64_t value; | ||
| 87 | } WtvSyncEntry; | ||
| 88 | |||
| 89 | typedef struct { | ||
| 90 | int64_t timeline_start_pos; | ||
| 91 | WtvFile file[WTV_FILES]; | ||
| 92 | int64_t serial; /**< chunk serial number */ | ||
| 93 | int64_t last_chunk_pos; /**< last chunk position */ | ||
| 94 | int64_t last_timestamp_pos; /**< last timestamp chunk position */ | ||
| 95 | int64_t first_index_pos; /**< first index_chunk position */ | ||
| 96 | |||
| 97 | WtvChunkEntry index[MAX_NB_INDEX]; | ||
| 98 | int nb_index; | ||
| 99 | int first_video_flag; | ||
| 100 | |||
| 101 | WtvSyncEntry *st_pairs; /* (serial, timestamp) pairs */ | ||
| 102 | int nb_st_pairs; | ||
| 103 | WtvSyncEntry *sp_pairs; /* (serial, position) pairs */ | ||
| 104 | int nb_sp_pairs; | ||
| 105 | |||
| 106 | int64_t last_pts; | ||
| 107 | int64_t last_serial; | ||
| 108 | |||
| 109 | AVPacket thumbnail; | ||
| 110 | } WtvContext; | ||
| 111 | |||
| 112 | |||
| 113 | 4 | static void add_serial_pair(WtvSyncEntry ** list, int * count, int64_t serial, int64_t value) | |
| 114 | { | ||
| 115 | 4 | int new_count = *count + 1; | |
| 116 | 4 | WtvSyncEntry *new_list = av_realloc_array(*list, new_count, sizeof(WtvSyncEntry)); | |
| 117 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!new_list) |
| 118 | ✗ | return; | |
| 119 | 4 | new_list[*count] = (WtvSyncEntry){serial, value}; | |
| 120 | 4 | *list = new_list; | |
| 121 | 4 | *count = new_count; | |
| 122 | } | ||
| 123 | |||
| 124 | typedef int WTVHeaderWriteFunc(AVIOContext *pb); | ||
| 125 | |||
| 126 | typedef struct { | ||
| 127 | const uint8_t *header; | ||
| 128 | int header_size; | ||
| 129 | WTVHeaderWriteFunc *write_header; | ||
| 130 | } WTVRootEntryTable; | ||
| 131 | |||
| 132 | #define write_pad(pb, size) ffio_fill(pb, 0, size) | ||
| 133 | |||
| 134 | /** | ||
| 135 | * Write chunk header. If header chunk (0x80000000 set) then add to list of header chunks | ||
| 136 | */ | ||
| 137 | 135 | static void write_chunk_header(AVFormatContext *s, const ff_asf_guid *guid, int length, int stream_id) | |
| 138 | { | ||
| 139 | 135 | WtvContext *wctx = s->priv_data; | |
| 140 | 135 | AVIOContext *pb = s->pb; | |
| 141 | |||
| 142 | 135 | wctx->last_chunk_pos = avio_tell(pb) - wctx->timeline_start_pos; | |
| 143 | 135 | ff_put_guid(pb, guid); | |
| 144 | 135 | avio_wl32(pb, 32 + length); | |
| 145 | 135 | avio_wl32(pb, stream_id); | |
| 146 | 135 | avio_wl64(pb, wctx->serial); | |
| 147 | |||
| 148 |
4/4✓ Branch 0 taken 5 times.
✓ Branch 1 taken 130 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 1 times.
|
135 | if ((stream_id & 0x80000000) && guid != &ff_index_guid) { |
| 149 | 4 | WtvChunkEntry *t = wctx->index + wctx->nb_index; | |
| 150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | av_assert0(wctx->nb_index < MAX_NB_INDEX); |
| 151 | 4 | t->pos = wctx->last_chunk_pos; | |
| 152 | 4 | t->serial = wctx->serial; | |
| 153 | 4 | t->guid = guid; | |
| 154 | 4 | t->stream_id = stream_id & 0x3FFFFFFF; | |
| 155 | 4 | wctx->nb_index++; | |
| 156 | } | ||
| 157 | 135 | } | |
| 158 | |||
| 159 | 5 | static void write_chunk_header2(AVFormatContext *s, const ff_asf_guid *guid, int stream_id) | |
| 160 | { | ||
| 161 | 5 | WtvContext *wctx = s->priv_data; | |
| 162 | 5 | AVIOContext *pb = s->pb; | |
| 163 | |||
| 164 | 5 | int64_t last_chunk_pos = wctx->last_chunk_pos; | |
| 165 | 5 | write_chunk_header(s, guid, 0, stream_id); // length updated later | |
| 166 | 5 | avio_wl64(pb, last_chunk_pos); | |
| 167 | 5 | } | |
| 168 | |||
| 169 | 7 | static void finish_chunk_noindex(AVFormatContext *s) | |
| 170 | { | ||
| 171 | 7 | WtvContext *wctx = s->priv_data; | |
| 172 | 7 | AVIOContext *pb = s->pb; | |
| 173 | |||
| 174 | // update the chunk_len field and pad. | ||
| 175 | 7 | int64_t chunk_len = avio_tell(pb) - (wctx->last_chunk_pos + wctx->timeline_start_pos); | |
| 176 | 7 | avio_seek(pb, -(chunk_len - 16), SEEK_CUR); | |
| 177 | 7 | avio_wl32(pb, chunk_len); | |
| 178 | 7 | avio_seek(pb, chunk_len - (16 + 4), SEEK_CUR); | |
| 179 | |||
| 180 | 7 | write_pad(pb, WTV_PAD8(chunk_len) - chunk_len); | |
| 181 | 7 | wctx->serial++; | |
| 182 | 7 | } | |
| 183 | |||
| 184 | 1 | static void write_index(AVFormatContext *s) | |
| 185 | { | ||
| 186 | 1 | AVIOContext *pb = s->pb; | |
| 187 | 1 | WtvContext *wctx = s->priv_data; | |
| 188 | int i; | ||
| 189 | |||
| 190 | 1 | write_chunk_header2(s, &ff_index_guid, 0x80000000); | |
| 191 | 1 | avio_wl32(pb, 0); | |
| 192 | 1 | avio_wl32(pb, 0); | |
| 193 | |||
| 194 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | for (i = 0; i < wctx->nb_index; i++) { |
| 195 | 4 | WtvChunkEntry *t = wctx->index + i; | |
| 196 | 4 | ff_put_guid(pb, t->guid); | |
| 197 | 4 | avio_wl64(pb, t->pos); | |
| 198 | 4 | avio_wl32(pb, t->stream_id); | |
| 199 | 4 | avio_wl32(pb, 0); // checksum? | |
| 200 | 4 | avio_wl64(pb, t->serial); | |
| 201 | } | ||
| 202 | 1 | wctx->nb_index = 0; // reset index | |
| 203 | 1 | finish_chunk_noindex(s); | |
| 204 | |||
| 205 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!wctx->first_index_pos) |
| 206 | 1 | wctx->first_index_pos = wctx->last_chunk_pos; | |
| 207 | 1 | } | |
| 208 | |||
| 209 | 6 | static void finish_chunk(AVFormatContext *s) | |
| 210 | { | ||
| 211 | 6 | WtvContext *wctx = s->priv_data; | |
| 212 | 6 | finish_chunk_noindex(s); | |
| 213 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
|
6 | if (wctx->nb_index == MAX_NB_INDEX) |
| 214 | ✗ | write_index(s); | |
| 215 | 6 | } | |
| 216 | |||
| 217 | 2 | static void put_videoinfoheader2(AVIOContext *pb, AVStream *st) | |
| 218 | { | ||
| 219 | 2 | AVRational dar = av_mul_q(st->sample_aspect_ratio, (AVRational){st->codecpar->width, st->codecpar->height}); | |
| 220 | unsigned int num, den; | ||
| 221 | 2 | av_reduce(&num, &den, dar.num, dar.den, 0xFFFFFFFF); | |
| 222 | |||
| 223 | /* VIDEOINFOHEADER2 */ | ||
| 224 | 2 | avio_wl32(pb, 0); | |
| 225 | 2 | avio_wl32(pb, 0); | |
| 226 | 2 | avio_wl32(pb, st->codecpar->width); | |
| 227 | 2 | avio_wl32(pb, st->codecpar->height); | |
| 228 | |||
| 229 | 2 | avio_wl32(pb, 0); | |
| 230 | 2 | avio_wl32(pb, 0); | |
| 231 | 2 | avio_wl32(pb, 0); | |
| 232 | 2 | avio_wl32(pb, 0); | |
| 233 | |||
| 234 | 2 | avio_wl32(pb, st->codecpar->bit_rate); | |
| 235 | 2 | avio_wl32(pb, 0); | |
| 236 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | avio_wl64(pb, st->avg_frame_rate.num && st->avg_frame_rate.den ? INT64_C(10000000) / av_q2d(st->avg_frame_rate) : 0); |
| 237 | 2 | avio_wl32(pb, 0); | |
| 238 | 2 | avio_wl32(pb, 0); | |
| 239 | |||
| 240 | 2 | avio_wl32(pb, num); | |
| 241 | 2 | avio_wl32(pb, den); | |
| 242 | 2 | avio_wl32(pb, 0); | |
| 243 | 2 | avio_wl32(pb, 0); | |
| 244 | |||
| 245 | 2 | ff_put_bmp_header(pb, st->codecpar, 0, 1, 0); | |
| 246 | |||
| 247 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (st->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO) { |
| 248 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | int padding = (st->codecpar->extradata_size & 3) ? 4 - (st->codecpar->extradata_size & 3) : 0; |
| 249 | /* MPEG2VIDEOINFO */ | ||
| 250 | 2 | avio_wl32(pb, 0); | |
| 251 | 2 | avio_wl32(pb, st->codecpar->extradata_size + padding); | |
| 252 | 2 | avio_wl32(pb, -1); | |
| 253 | 2 | avio_wl32(pb, -1); | |
| 254 | 2 | avio_wl32(pb, 0); | |
| 255 | 2 | avio_write(pb, st->codecpar->extradata, st->codecpar->extradata_size); | |
| 256 | 2 | ffio_fill(pb, 0, padding); | |
| 257 | } | ||
| 258 | 2 | } | |
| 259 | |||
| 260 | 4 | static int write_stream_codec_info(AVFormatContext *s, AVStream *st) | |
| 261 | { | ||
| 262 | const ff_asf_guid *g, *media_type, *format_type; | ||
| 263 | const AVCodecTag *tags; | ||
| 264 | 4 | AVIOContext *pb = s->pb; | |
| 265 | int64_t hdr_pos_start; | ||
| 266 | 4 | int hdr_size = 0; | |
| 267 | |||
| 268 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { |
| 269 | 2 | g = ff_get_codec_guid(st->codecpar->codec_id, ff_video_guids); | |
| 270 | 2 | media_type = &ff_mediatype_video; | |
| 271 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | format_type = st->codecpar->codec_id == AV_CODEC_ID_MPEG2VIDEO ? &ff_format_mpeg2_video : &ff_format_videoinfo2; |
| 272 | 2 | tags = ff_codec_bmp_tags; | |
| 273 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | } else if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { |
| 274 | 2 | g = ff_get_codec_guid(st->codecpar->codec_id, ff_codec_wav_guids); | |
| 275 | 2 | media_type = &ff_mediatype_audio; | |
| 276 | 2 | format_type = &ff_format_waveformatex; | |
| 277 | 2 | tags = ff_codec_wav_tags; | |
| 278 | } else { | ||
| 279 | ✗ | av_log(s, AV_LOG_ERROR, "unknown codec_type (0x%x)\n", st->codecpar->codec_type); | |
| 280 | ✗ | return -1; | |
| 281 | } | ||
| 282 | |||
| 283 | 4 | ff_put_guid(pb, media_type); // mediatype | |
| 284 | 4 | ff_put_guid(pb, &ff_mediasubtype_cpfilters_processed); // subtype | |
| 285 | 4 | write_pad(pb, 12); | |
| 286 | 4 | ff_put_guid(pb,&ff_format_cpfilters_processed); // format type | |
| 287 | 4 | avio_wl32(pb, 0); // size | |
| 288 | |||
| 289 | 4 | hdr_pos_start = avio_tell(pb); | |
| 290 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { |
| 291 | 2 | put_videoinfoheader2(pb, st); | |
| 292 | } else { | ||
| 293 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (ff_put_wav_header(s, pb, st->codecpar, 0) < 0) |
| 294 | ✗ | format_type = &ff_format_none; | |
| 295 | } | ||
| 296 | 4 | hdr_size = avio_tell(pb) - hdr_pos_start; | |
| 297 | |||
| 298 | // seek back write hdr_size | ||
| 299 | 4 | avio_seek(pb, -(hdr_size + 4), SEEK_CUR); | |
| 300 | 4 | avio_wl32(pb, hdr_size + 32); | |
| 301 | 4 | avio_seek(pb, hdr_size, SEEK_CUR); | |
| 302 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (g) { |
| 303 | 4 | ff_put_guid(pb, g); // actual_subtype | |
| 304 | } else { | ||
| 305 | ✗ | int tag = ff_codec_get_tag(tags, st->codecpar->codec_id); | |
| 306 | ✗ | if (!tag) { | |
| 307 | ✗ | av_log(s, AV_LOG_ERROR, "unsupported codec_id (0x%x)\n", st->codecpar->codec_id); | |
| 308 | ✗ | return -1; | |
| 309 | } | ||
| 310 | ✗ | avio_wl32(pb, tag); | |
| 311 | ✗ | avio_write(pb, (const uint8_t[]){FF_MEDIASUBTYPE_BASE_GUID}, 12); | |
| 312 | } | ||
| 313 | 4 | ff_put_guid(pb, format_type); // actual_formattype | |
| 314 | |||
| 315 | 4 | return 0; | |
| 316 | } | ||
| 317 | |||
| 318 | 2 | static int write_stream_codec(AVFormatContext *s, AVStream * st) | |
| 319 | { | ||
| 320 | 2 | AVIOContext *pb = s->pb; | |
| 321 | int ret; | ||
| 322 | 2 | write_chunk_header2(s, &ff_stream1_guid, 0x80000000 | 0x01); | |
| 323 | |||
| 324 | 2 | avio_wl32(pb, 0x01); | |
| 325 | 2 | write_pad(pb, 4); | |
| 326 | 2 | write_pad(pb, 4); | |
| 327 | |||
| 328 | 2 | ret = write_stream_codec_info(s, st); | |
| 329 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) { |
| 330 | ✗ | av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codecpar->codec_type); | |
| 331 | ✗ | return -1; | |
| 332 | } | ||
| 333 | |||
| 334 | 2 | finish_chunk(s); | |
| 335 | 2 | return 0; | |
| 336 | } | ||
| 337 | |||
| 338 | 2 | static void write_sync(AVFormatContext *s) | |
| 339 | { | ||
| 340 | 2 | AVIOContext *pb = s->pb; | |
| 341 | 2 | WtvContext *wctx = s->priv_data; | |
| 342 | 2 | int64_t last_chunk_pos = wctx->last_chunk_pos; | |
| 343 | |||
| 344 | 2 | write_chunk_header(s, &ff_sync_guid, 0x18, 0); | |
| 345 | 2 | avio_wl64(pb, wctx->first_index_pos); | |
| 346 | 2 | avio_wl64(pb, wctx->last_timestamp_pos); | |
| 347 | 2 | avio_wl64(pb, 0); | |
| 348 | |||
| 349 | 2 | finish_chunk(s); | |
| 350 | 2 | add_serial_pair(&wctx->sp_pairs, &wctx->nb_sp_pairs, wctx->serial, wctx->last_chunk_pos); | |
| 351 | |||
| 352 | 2 | wctx->last_chunk_pos = last_chunk_pos; | |
| 353 | 2 | } | |
| 354 | |||
| 355 | 2 | static int write_stream_data(AVFormatContext *s, AVStream *st) | |
| 356 | { | ||
| 357 | 2 | AVIOContext *pb = s->pb; | |
| 358 | int ret; | ||
| 359 | |||
| 360 | 2 | write_chunk_header2(s, &ff_SBE2_STREAM_DESC_EVENT, 0x80000000 | (st->index + INDEX_BASE)); | |
| 361 | 2 | avio_wl32(pb, 0x00000001); | |
| 362 | 2 | avio_wl32(pb, st->index + INDEX_BASE); //stream_id | |
| 363 | 2 | avio_wl32(pb, 0x00000001); | |
| 364 | 2 | write_pad(pb, 8); | |
| 365 | |||
| 366 | 2 | ret = write_stream_codec_info(s, st); | |
| 367 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) { |
| 368 | ✗ | av_log(s, AV_LOG_ERROR, "write stream codec info failed codec_type(0x%x)\n", st->codecpar->codec_type); | |
| 369 | ✗ | return -1; | |
| 370 | } | ||
| 371 | 2 | finish_chunk(s); | |
| 372 | |||
| 373 | 2 | avpriv_set_pts_info(st, 64, 1, 10000000); | |
| 374 | |||
| 375 | 2 | return 0; | |
| 376 | } | ||
| 377 | |||
| 378 | 1 | static int write_header(AVFormatContext *s) | |
| 379 | { | ||
| 380 | 1 | AVIOContext *pb = s->pb; | |
| 381 | 1 | WtvContext *wctx = s->priv_data; | |
| 382 | int i, pad, ret; | ||
| 383 | AVStream *st; | ||
| 384 | |||
| 385 | 1 | wctx->last_chunk_pos = -1; | |
| 386 | 1 | wctx->last_timestamp_pos = -1; | |
| 387 | |||
| 388 | 1 | ff_put_guid(pb, &ff_wtv_guid); | |
| 389 | 1 | ff_put_guid(pb, &sub_wtv_guid); | |
| 390 | |||
| 391 | 1 | avio_wl32(pb, 0x01); | |
| 392 | 1 | avio_wl32(pb, 0x02); | |
| 393 | 1 | avio_wl32(pb, 1 << WTV_SECTOR_BITS); | |
| 394 | 1 | avio_wl32(pb, 1 << WTV_BIGSECTOR_BITS); | |
| 395 | |||
| 396 | //write initial root fields | ||
| 397 | 1 | avio_wl32(pb, 0); // root_size, update later | |
| 398 | 1 | write_pad(pb, 4); | |
| 399 | 1 | avio_wl32(pb, 0); // root_sector, update it later. | |
| 400 | |||
| 401 | 1 | write_pad(pb, 32); | |
| 402 | 1 | avio_wl32(pb, 0); // file ends pointer, update it later. | |
| 403 | |||
| 404 | 1 | pad = (1 << WTV_SECTOR_BITS) - avio_tell(pb); | |
| 405 | 1 | write_pad(pb, pad); | |
| 406 | |||
| 407 | 1 | wctx->timeline_start_pos = avio_tell(pb); | |
| 408 | |||
| 409 | 1 | wctx->serial = 1; | |
| 410 | 1 | wctx->last_chunk_pos = -1; | |
| 411 | 1 | wctx->first_video_flag = 1; | |
| 412 | |||
| 413 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (i = 0; i < s->nb_streams; i++) { |
| 414 | 2 | st = s->streams[i]; | |
| 415 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (st->codecpar->codec_id == AV_CODEC_ID_MJPEG) |
| 416 | ✗ | continue; | |
| 417 | 2 | ret = write_stream_codec(s, st); | |
| 418 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) { |
| 419 | ✗ | av_log(s, AV_LOG_ERROR, "write stream codec failed codec_type(0x%x)\n", st->codecpar->codec_type); | |
| 420 | ✗ | return -1; | |
| 421 | } | ||
| 422 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (!i) |
| 423 | 1 | write_sync(s); | |
| 424 | } | ||
| 425 | |||
| 426 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (i = 0; i < s->nb_streams; i++) { |
| 427 | 2 | st = s->streams[i]; | |
| 428 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (st->codecpar->codec_id == AV_CODEC_ID_MJPEG) |
| 429 | ✗ | continue; | |
| 430 | 2 | ret = write_stream_data(s, st); | |
| 431 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) { |
| 432 | ✗ | av_log(s, AV_LOG_ERROR, "write stream data failed codec_type(0x%x)\n", st->codecpar->codec_type); | |
| 433 | ✗ | return -1; | |
| 434 | } | ||
| 435 | } | ||
| 436 | |||
| 437 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (wctx->nb_index) |
| 438 | 1 | write_index(s); | |
| 439 | |||
| 440 | 1 | return 0; | |
| 441 | } | ||
| 442 | |||
| 443 | 64 | static void write_timestamp(AVFormatContext *s, AVPacket *pkt) | |
| 444 | { | ||
| 445 | 64 | AVIOContext *pb = s->pb; | |
| 446 | 64 | WtvContext *wctx = s->priv_data; | |
| 447 | 64 | AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar; | |
| 448 | |||
| 449 | 64 | write_chunk_header(s, &ff_timestamp_guid, 56, 0x40000000 | (INDEX_BASE + pkt->stream_index)); | |
| 450 | 64 | write_pad(pb, 8); | |
| 451 |
1/2✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
|
64 | avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts); |
| 452 |
1/2✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
|
64 | avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? -1 : pkt->pts); |
| 453 |
1/2✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
|
64 | avio_wl64(pb, pkt->pts == AV_NOPTS_VALUE ? 0 : pkt->pts); |
| 454 | 64 | avio_wl64(pb, 0); | |
| 455 |
4/4✓ Branch 0 taken 25 times.
✓ Branch 1 taken 39 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 22 times.
|
64 | avio_wl64(pb, par->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY) ? 1 : 0); |
| 456 | 64 | avio_wl64(pb, 0); | |
| 457 | |||
| 458 | 64 | wctx->last_timestamp_pos = wctx->last_chunk_pos; | |
| 459 | 64 | } | |
| 460 | |||
| 461 | 64 | static int write_packet(AVFormatContext *s, AVPacket *pkt) | |
| 462 | { | ||
| 463 | 64 | AVIOContext *pb = s->pb; | |
| 464 | 64 | WtvContext *wctx = s->priv_data; | |
| 465 | 64 | AVStream *st = s->streams[pkt->stream_index]; | |
| 466 | |||
| 467 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
64 | if (st->codecpar->codec_id == AV_CODEC_ID_MJPEG && !wctx->thumbnail.size) { |
| 468 | ✗ | av_packet_ref(&wctx->thumbnail, pkt); | |
| 469 | ✗ | return 0; | |
| 470 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | } else if (st->codecpar->codec_id == AV_CODEC_ID_H264) { |
| 471 | ✗ | int ret = ff_check_h264_startcode(s, st, pkt); | |
| 472 | ✗ | if (ret < 0) | |
| 473 | ✗ | return ret; | |
| 474 | } | ||
| 475 | |||
| 476 | /* emit sync chunk and 'timeline.table.0.entries.Event' record every 50 frames */ | ||
| 477 |
3/4✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 63 times.
|
64 | if (wctx->serial - (wctx->nb_sp_pairs ? wctx->sp_pairs[wctx->nb_sp_pairs - 1].serial : 0) >= 50) |
| 478 | 1 | write_sync(s); | |
| 479 | |||
| 480 | /* emit 'table.0.entries.time' record every 500ms */ | ||
| 481 |
5/6✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 31 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 62 times.
|
64 | if (pkt->pts != AV_NOPTS_VALUE && pkt->pts - (wctx->nb_st_pairs ? wctx->st_pairs[wctx->nb_st_pairs - 1].value : 0) >= 5000000) |
| 482 | 2 | add_serial_pair(&wctx->st_pairs, &wctx->nb_st_pairs, wctx->serial, pkt->pts); | |
| 483 | |||
| 484 |
3/4✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 38 times.
|
64 | if (pkt->pts != AV_NOPTS_VALUE && pkt->pts > wctx->last_pts) { |
| 485 | 26 | wctx->last_pts = pkt->pts; | |
| 486 | 26 | wctx->last_serial = wctx->serial; | |
| 487 | } | ||
| 488 | |||
| 489 | // write timestamp chunk | ||
| 490 | 64 | write_timestamp(s, pkt); | |
| 491 | |||
| 492 | 64 | write_chunk_header(s, &ff_data_guid, pkt->size, INDEX_BASE + pkt->stream_index); | |
| 493 | 64 | avio_write(pb, pkt->data, pkt->size); | |
| 494 | 64 | write_pad(pb, WTV_PAD8(pkt->size) - pkt->size); | |
| 495 | |||
| 496 | 64 | wctx->serial++; | |
| 497 | 64 | return 0; | |
| 498 | } | ||
| 499 | |||
| 500 | 1 | static int write_table0_header_events(AVIOContext *pb) | |
| 501 | { | ||
| 502 | 1 | avio_wl32(pb, 0x10); | |
| 503 | 1 | write_pad(pb, 84); | |
| 504 | 1 | avio_wl64(pb, 0x32); | |
| 505 | 1 | return 96; | |
| 506 | } | ||
| 507 | |||
| 508 | 1 | static int write_table0_header_legacy_attrib(AVIOContext *pb) | |
| 509 | { | ||
| 510 | 1 | int pad = 0; | |
| 511 | 1 | avio_wl32(pb, 0xFFFFFFFF); | |
| 512 | 1 | write_pad(pb, 12); | |
| 513 | 1 | avio_write(pb, legacy_attrib, sizeof(legacy_attrib)); | |
| 514 | 1 | pad = WTV_PAD8(sizeof(legacy_attrib)) - sizeof(legacy_attrib); | |
| 515 | 1 | write_pad(pb, pad); | |
| 516 | 1 | write_pad(pb, 32); | |
| 517 | 1 | return 48 + WTV_PAD8(sizeof(legacy_attrib)); | |
| 518 | } | ||
| 519 | |||
| 520 | 1 | static int write_table0_header_time(AVIOContext *pb) | |
| 521 | { | ||
| 522 | 1 | avio_wl32(pb, 0x10); | |
| 523 | 1 | write_pad(pb, 76); | |
| 524 | 1 | avio_wl64(pb, 0x40); | |
| 525 | 1 | return 88; | |
| 526 | } | ||
| 527 | |||
| 528 | static const WTVRootEntryTable wtv_root_entry_table[] = { | ||
| 529 | { timeline_table_0_header_events, sizeof(timeline_table_0_header_events), write_table0_header_events}, | ||
| 530 | { ff_timeline_table_0_entries_Events_le16, sizeof(ff_timeline_table_0_entries_Events_le16), NULL}, | ||
| 531 | { ff_timeline_le16, sizeof(ff_timeline_le16), NULL}, | ||
| 532 | { table_0_header_legacy_attrib, sizeof(table_0_header_legacy_attrib), write_table0_header_legacy_attrib}, | ||
| 533 | { ff_table_0_entries_legacy_attrib_le16, sizeof(ff_table_0_entries_legacy_attrib_le16), NULL}, | ||
| 534 | { table_0_redirector_legacy_attrib, sizeof(table_0_redirector_legacy_attrib), NULL}, | ||
| 535 | { table_0_header_time, sizeof(table_0_header_time), write_table0_header_time}, | ||
| 536 | { ff_table_0_entries_time_le16, sizeof(ff_table_0_entries_time_le16), NULL}, | ||
| 537 | }; | ||
| 538 | |||
| 539 | 1 | static int write_root_table(AVFormatContext *s, int64_t sector_pos) | |
| 540 | { | ||
| 541 | 1 | AVIOContext *pb = s->pb; | |
| 542 | 1 | WtvContext *wctx = s->priv_data; | |
| 543 | int size, pad; | ||
| 544 | int i; | ||
| 545 | |||
| 546 | 1 | const WTVRootEntryTable *h = wtv_root_entry_table; | |
| 547 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
|
9 | for (i = 0; i < sizeof(wtv_root_entry_table)/sizeof(WTVRootEntryTable); i++, h++) { |
| 548 | 8 | WtvFile *w = &wctx->file[i]; | |
| 549 | 8 | int filename_padding = WTV_PAD8(h->header_size) - h->header_size; | |
| 550 | 8 | WTVHeaderWriteFunc *write = h->write_header; | |
| 551 | 8 | int len = 0; | |
| 552 | int64_t len_pos; | ||
| 553 | |||
| 554 | 8 | ff_put_guid(pb, &ff_dir_entry_guid); | |
| 555 | 8 | len_pos = avio_tell(pb); | |
| 556 | 8 | avio_wl16(pb, 40 + h->header_size + filename_padding + 8); // maybe updated later | |
| 557 | 8 | write_pad(pb, 6); | |
| 558 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
|
8 | avio_wl64(pb, write ? 0 : w->length);// maybe update later |
| 559 | 8 | avio_wl32(pb, (h->header_size + filename_padding) >> 1); | |
| 560 | 8 | write_pad(pb, 4); | |
| 561 | |||
| 562 | 8 | avio_write(pb, h->header, h->header_size); | |
| 563 | 8 | write_pad(pb, filename_padding); | |
| 564 | |||
| 565 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
|
8 | if (write) { |
| 566 | 3 | len = write(pb); | |
| 567 | // update length field | ||
| 568 | 3 | avio_seek(pb, len_pos, SEEK_SET); | |
| 569 | 3 | avio_wl64(pb, 40 + h->header_size + filename_padding + len); | |
| 570 | 3 | avio_wl64(pb, len |(1ULL<<62) | (1ULL<<60)); | |
| 571 | 3 | avio_seek(pb, 8 + h->header_size + filename_padding + len, SEEK_CUR); | |
| 572 | } else { | ||
| 573 | 5 | avio_wl32(pb, w->first_sector); | |
| 574 | 5 | avio_wl32(pb, w->depth); | |
| 575 | } | ||
| 576 | } | ||
| 577 | |||
| 578 | // calculate root table size | ||
| 579 | 1 | size = avio_tell(pb) - sector_pos; | |
| 580 | 1 | pad = WTV_SECTOR_SIZE- size; | |
| 581 | 1 | write_pad(pb, pad); | |
| 582 | |||
| 583 | 1 | return size; | |
| 584 | } | ||
| 585 | |||
| 586 | 1 | static void write_fat(AVIOContext *pb, int start_sector, int nb_sectors, int shift) | |
| 587 | { | ||
| 588 | int i; | ||
| 589 |
2/2✓ Branch 0 taken 94 times.
✓ Branch 1 taken 1 times.
|
95 | for (i = 0; i < nb_sectors; i++) { |
| 590 | 94 | avio_wl32(pb, start_sector + (i << shift)); | |
| 591 | } | ||
| 592 | // pad left sector pointer size | ||
| 593 | 1 | write_pad(pb, WTV_SECTOR_SIZE - ((nb_sectors << 2) % WTV_SECTOR_SIZE)); | |
| 594 | 1 | } | |
| 595 | |||
| 596 | 1 | static int64_t write_fat_sector(AVFormatContext *s, int64_t start_pos, int nb_sectors, int sector_bits, int depth) | |
| 597 | { | ||
| 598 | 1 | int64_t start_sector = start_pos >> WTV_SECTOR_BITS; | |
| 599 | 1 | int shift = sector_bits - WTV_SECTOR_BITS; | |
| 600 | |||
| 601 | 1 | int64_t fat = avio_tell(s->pb); | |
| 602 | 1 | write_fat(s->pb, start_sector, nb_sectors, shift); | |
| 603 | |||
| 604 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (depth == 2) { |
| 605 | ✗ | int64_t start_sector1 = fat >> WTV_SECTOR_BITS; | |
| 606 | ✗ | int nb_sectors1 = ((nb_sectors << 2) + WTV_SECTOR_SIZE - 1) / WTV_SECTOR_SIZE; | |
| 607 | ✗ | int64_t fat1 = avio_tell(s->pb); | |
| 608 | |||
| 609 | ✗ | write_fat(s->pb, start_sector1, nb_sectors1, 0); | |
| 610 | ✗ | return fat1; | |
| 611 | } | ||
| 612 | |||
| 613 | 1 | return fat; | |
| 614 | } | ||
| 615 | |||
| 616 | 1 | static void write_table_entries_events(AVFormatContext *s) | |
| 617 | { | ||
| 618 | 1 | AVIOContext *pb = s->pb; | |
| 619 | 1 | WtvContext *wctx = s->priv_data; | |
| 620 | int i; | ||
| 621 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (i = 0; i < wctx->nb_sp_pairs; i++) { |
| 622 | 2 | avio_wl64(pb, wctx->sp_pairs[i].serial); | |
| 623 | 2 | avio_wl64(pb, wctx->sp_pairs[i].value); | |
| 624 | } | ||
| 625 | 1 | } | |
| 626 | |||
| 627 | 1 | static void write_table_entries_time(AVFormatContext *s) | |
| 628 | { | ||
| 629 | 1 | AVIOContext *pb = s->pb; | |
| 630 | 1 | WtvContext *wctx = s->priv_data; | |
| 631 | int i; | ||
| 632 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | for (i = 0; i < wctx->nb_st_pairs; i++) { |
| 633 | 2 | avio_wl64(pb, wctx->st_pairs[i].value); | |
| 634 | 2 | avio_wl64(pb, wctx->st_pairs[i].serial); | |
| 635 | } | ||
| 636 | 1 | avio_wl64(pb, wctx->last_pts); | |
| 637 | 1 | avio_wl64(pb, wctx->last_serial); | |
| 638 | 1 | } | |
| 639 | |||
| 640 | 1 | static void write_metadata_header(AVIOContext *pb, int type, const char *key, int value_size) | |
| 641 | { | ||
| 642 | 1 | ff_put_guid(pb, &ff_metadata_guid); | |
| 643 | 1 | avio_wl32(pb, type); | |
| 644 | 1 | avio_wl32(pb, value_size); | |
| 645 | 1 | avio_put_str16le(pb, key); | |
| 646 | 1 | } | |
| 647 | |||
| 648 | 1 | static int metadata_header_size(const char *key) | |
| 649 | { | ||
| 650 | 1 | return 16 + 4 + 4 + strlen(key)*2 + 2; | |
| 651 | } | ||
| 652 | |||
| 653 | ✗ | static void write_tag_int32(AVIOContext *pb, const char *key, int value) | |
| 654 | { | ||
| 655 | ✗ | write_metadata_header(pb, 0, key, 4); | |
| 656 | ✗ | avio_wl32(pb, value); | |
| 657 | ✗ | } | |
| 658 | |||
| 659 | 1 | static void write_tag(AVIOContext *pb, const char *key, const char *value) | |
| 660 | { | ||
| 661 | 1 | write_metadata_header(pb, 1, key, strlen(value)*2 + 2); | |
| 662 | 1 | avio_put_str16le(pb, value); | |
| 663 | 1 | } | |
| 664 | |||
| 665 | ✗ | static int attachment_value_size(const AVPacket *pkt, const AVDictionaryEntry *e) | |
| 666 | { | ||
| 667 | ✗ | return strlen("image/jpeg")*2 + 2 + 1 + (e ? strlen(e->value)*2 : 0) + 2 + 4 + pkt->size; | |
| 668 | } | ||
| 669 | |||
| 670 | 1 | static void write_table_entries_attrib(AVFormatContext *s) | |
| 671 | { | ||
| 672 | 1 | WtvContext *wctx = s->priv_data; | |
| 673 | 1 | AVIOContext *pb = s->pb; | |
| 674 | 1 | const AVDictionaryEntry *tag = NULL; | |
| 675 | |||
| 676 | 1 | ff_standardize_creation_time(s); | |
| 677 | //FIXME: translate special tags (e.g. WM/Bitrate) to binary representation | ||
| 678 | 1 | ff_metadata_conv(&s->metadata, ff_asf_metadata_conv, NULL); | |
| 679 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | while ((tag = av_dict_iterate(s->metadata, tag))) |
| 680 | 1 | write_tag(pb, tag->key, tag->value); | |
| 681 | |||
| 682 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (wctx->thumbnail.size) { |
| 683 | ✗ | AVStream *st = s->streams[wctx->thumbnail.stream_index]; | |
| 684 | ✗ | tag = av_dict_get(st->metadata, "title", NULL, 0); | |
| 685 | ✗ | write_metadata_header(pb, 2, "WM/Picture", attachment_value_size(&wctx->thumbnail, tag)); | |
| 686 | |||
| 687 | ✗ | avio_put_str16le(pb, "image/jpeg"); | |
| 688 | ✗ | avio_w8(pb, 0x10); | |
| 689 | ✗ | avio_put_str16le(pb, tag ? tag->value : ""); | |
| 690 | |||
| 691 | ✗ | avio_wl32(pb, wctx->thumbnail.size); | |
| 692 | ✗ | avio_write(pb, wctx->thumbnail.data, wctx->thumbnail.size); | |
| 693 | |||
| 694 | ✗ | write_tag_int32(pb, "WM/MediaThumbType", 2); | |
| 695 | } | ||
| 696 | 1 | } | |
| 697 | |||
| 698 | 1 | static void write_table_redirector_legacy_attrib(AVFormatContext *s) | |
| 699 | { | ||
| 700 | 1 | WtvContext *wctx = s->priv_data; | |
| 701 | 1 | AVIOContext *pb = s->pb; | |
| 702 | 1 | const AVDictionaryEntry *tag = NULL; | |
| 703 | 1 | int64_t pos = 0; | |
| 704 | |||
| 705 | //FIXME: translate special tags to binary representation | ||
| 706 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | while ((tag = av_dict_iterate(s->metadata, tag))) { |
| 707 | 1 | avio_wl64(pb, pos); | |
| 708 | 1 | pos += metadata_header_size(tag->key) + strlen(tag->value)*2 + 2; | |
| 709 | } | ||
| 710 | |||
| 711 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (wctx->thumbnail.size) { |
| 712 | ✗ | AVStream *st = s->streams[wctx->thumbnail.stream_index]; | |
| 713 | ✗ | avio_wl64(pb, pos); | |
| 714 | ✗ | pos += metadata_header_size("WM/Picture") + attachment_value_size(&wctx->thumbnail, av_dict_get(st->metadata, "title", NULL, 0)); | |
| 715 | |||
| 716 | ✗ | avio_wl64(pb, pos); | |
| 717 | ✗ | pos += metadata_header_size("WM/MediaThumbType") + 4; | |
| 718 | } | ||
| 719 | 1 | } | |
| 720 | |||
| 721 | /** | ||
| 722 | * Pad the remainder of a file | ||
| 723 | * Write out fat table | ||
| 724 | * @return <0 on error | ||
| 725 | */ | ||
| 726 | 5 | static int finish_file(AVFormatContext *s, enum WtvFileIndex index, int64_t start_pos) | |
| 727 | { | ||
| 728 | 5 | WtvContext *wctx = s->priv_data; | |
| 729 | 5 | AVIOContext *pb = s->pb; | |
| 730 | 5 | WtvFile *w = &wctx->file[index]; | |
| 731 | 5 | int64_t end_pos = avio_tell(pb); | |
| 732 | int sector_bits, nb_sectors, pad; | ||
| 733 | |||
| 734 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 | av_assert0(index < WTV_FILES); |
| 735 | |||
| 736 | 5 | w->length = (end_pos - start_pos); | |
| 737 | |||
| 738 | // determine optimal fat table depth, sector_bits, nb_sectors | ||
| 739 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
|
5 | if (w->length <= WTV_SECTOR_SIZE) { |
| 740 | 4 | w->depth = 0; | |
| 741 | 4 | sector_bits = WTV_SECTOR_BITS; | |
| 742 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) { |
| 743 | 1 | w->depth = 1; | |
| 744 | 1 | sector_bits = WTV_SECTOR_BITS; | |
| 745 | ✗ | } else if (w->length <= (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) { | |
| 746 | ✗ | w->depth = 1; | |
| 747 | ✗ | sector_bits = WTV_BIGSECTOR_BITS; | |
| 748 | ✗ | } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_SECTOR_SIZE) { | |
| 749 | ✗ | w->depth = 2; | |
| 750 | ✗ | sector_bits = WTV_SECTOR_BITS; | |
| 751 | ✗ | } else if (w->length <= (int64_t)(WTV_SECTOR_SIZE / 4) * (WTV_SECTOR_SIZE / 4) * WTV_BIGSECTOR_SIZE) { | |
| 752 | ✗ | w->depth = 2; | |
| 753 | ✗ | sector_bits = WTV_BIGSECTOR_BITS; | |
| 754 | } else { | ||
| 755 | ✗ | av_log(s, AV_LOG_ERROR, "unsupported file allocation table depth (%"PRIi64" bytes)\n", w->length); | |
| 756 | ✗ | return -1; | |
| 757 | } | ||
| 758 | |||
| 759 | // determine the nb_sectors | ||
| 760 | 5 | nb_sectors = (int)(w->length >> sector_bits); | |
| 761 | |||
| 762 | // pad sector of timeline | ||
| 763 | 5 | pad = (1 << sector_bits) - (w->length % (1 << sector_bits)); | |
| 764 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (pad) { |
| 765 | 5 | nb_sectors++; | |
| 766 | 5 | write_pad(pb, pad); | |
| 767 | } | ||
| 768 | |||
| 769 | //write fat table | ||
| 770 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
|
5 | if (w->depth > 0) { |
| 771 | 1 | w->first_sector = write_fat_sector(s, start_pos, nb_sectors, sector_bits, w->depth) >> WTV_SECTOR_BITS; | |
| 772 | } else { | ||
| 773 | 4 | w->first_sector = start_pos >> WTV_SECTOR_BITS; | |
| 774 | } | ||
| 775 | |||
| 776 | 5 | w->length |= 1ULL<<60; | |
| 777 |
1/2✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 | if (sector_bits == WTV_SECTOR_BITS) |
| 778 | 5 | w->length |= 1ULL<<63; | |
| 779 | |||
| 780 | 5 | return 0; | |
| 781 | } | ||
| 782 | |||
| 783 | 1 | static int write_trailer(AVFormatContext *s) | |
| 784 | { | ||
| 785 | 1 | WtvContext *wctx = s->priv_data; | |
| 786 | 1 | AVIOContext *pb = s->pb; | |
| 787 | int root_size; | ||
| 788 | int64_t sector_pos; | ||
| 789 | int64_t start_pos, file_end_pos; | ||
| 790 | |||
| 791 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (finish_file(s, WTV_TIMELINE, wctx->timeline_start_pos) < 0) |
| 792 | ✗ | return -1; | |
| 793 | |||
| 794 | 1 | start_pos = avio_tell(pb); | |
| 795 | 1 | write_table_entries_events(s); | |
| 796 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (finish_file(s, WTV_TIMELINE_TABLE_0_ENTRIES_EVENTS, start_pos) < 0) |
| 797 | ✗ | return -1; | |
| 798 | |||
| 799 | 1 | start_pos = avio_tell(pb); | |
| 800 | 1 | write_table_entries_attrib(s); | |
| 801 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (finish_file(s, WTV_TABLE_0_ENTRIES_LEGACY_ATTRIB, start_pos) < 0) |
| 802 | ✗ | return -1; | |
| 803 | |||
| 804 | 1 | start_pos = avio_tell(pb); | |
| 805 | 1 | write_table_redirector_legacy_attrib(s); | |
| 806 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (finish_file(s, WTV_TABLE_0_REDIRECTOR_LEGACY_ATTRIB, start_pos) < 0) |
| 807 | ✗ | return -1; | |
| 808 | |||
| 809 | 1 | start_pos = avio_tell(pb); | |
| 810 | 1 | write_table_entries_time(s); | |
| 811 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (finish_file(s, WTV_TABLE_0_ENTRIES_TIME, start_pos) < 0) |
| 812 | ✗ | return -1; | |
| 813 | |||
| 814 | // write root table | ||
| 815 | 1 | sector_pos = avio_tell(pb); | |
| 816 | 1 | root_size = write_root_table(s, sector_pos); | |
| 817 | |||
| 818 | 1 | file_end_pos = avio_tell(pb); | |
| 819 | // update root value | ||
| 820 | 1 | avio_seek(pb, 0x30, SEEK_SET); | |
| 821 | 1 | avio_wl32(pb, root_size); | |
| 822 | 1 | avio_seek(pb, 4, SEEK_CUR); | |
| 823 | 1 | avio_wl32(pb, sector_pos >> WTV_SECTOR_BITS); | |
| 824 | 1 | avio_seek(pb, 0x5c, SEEK_SET); | |
| 825 | 1 | avio_wl32(pb, file_end_pos >> WTV_SECTOR_BITS); | |
| 826 | |||
| 827 | 1 | av_free(wctx->sp_pairs); | |
| 828 | 1 | av_free(wctx->st_pairs); | |
| 829 | 1 | av_packet_unref(&wctx->thumbnail); | |
| 830 | 1 | return 0; | |
| 831 | } | ||
| 832 | |||
| 833 | const FFOutputFormat ff_wtv_muxer = { | ||
| 834 | .p.name = "wtv", | ||
| 835 | .p.long_name = NULL_IF_CONFIG_SMALL("Windows Television (WTV)"), | ||
| 836 | .p.extensions = "wtv", | ||
| 837 | .priv_data_size = sizeof(WtvContext), | ||
| 838 | .p.audio_codec = AV_CODEC_ID_AC3, | ||
| 839 | .p.video_codec = AV_CODEC_ID_MPEG2VIDEO, | ||
| 840 | .write_header = write_header, | ||
| 841 | .write_packet = write_packet, | ||
| 842 | .write_trailer = write_trailer, | ||
| 843 | .p.codec_tag = ff_riff_codec_tags_list, | ||
| 844 | }; | ||
| 845 |