| 1 | /* |
| 2 | * dvavi.c |
| 3 | * |
| 4 | * Copyright (C) Peter Schlaile - January 2002 |
| 5 | * |
| 6 | * This file is part of libdv, a free DV (IEC 61834/SMPTE 314M) |
| 7 | * codec. |
| 8 | * |
| 9 | * libdv is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU Lesser Public License as published by |
| 11 | * the Free Software Foundation; either version 2.1, or (at your |
| 12 | * option) any later version. |
| 13 | * |
| 14 | * libdv is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * Lesser Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser Public License |
| 20 | * along with libdv; see the file COPYING. If not, write to |
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | * |
| 23 | * The libdv homepage is http://libdv.sourceforge.net/. |
| 24 | */ |
| 25 | |
| 26 | #ifdef HAVE_CONFIG_H |
| 27 | #include <config.h> |
| 28 | #endif // HAVE_CONFIG_H |
| 29 | |
| 30 | #include "libdv/dv_types.h" |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <sys/ioctl.h> |
| 34 | #include <sys/mman.h> |
| 35 | #include <unistd.h> |
| 36 | #include <fcntl.h> |
| 37 | #include <setjmp.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #if HAVE_LIBPOPT |
| 41 | #include <popt.h> |
| 42 | #endif |
| 43 | |
| 44 | #include <stdlib.h> |
| 45 | #include <stdarg.h> |
| 46 | |
| 47 | #include "libdv/headers.h" |
| 48 | #include "libdv/enc_audio_input.h" |
| 49 | #include "libdv/enc_input.h" |
| 50 | #include "libdv/enc_output.h" |
| 51 | |
| 52 | static jmp_buf error_jmp_env; |
| 53 | |
| 54 | unsigned long read_long(FILE* in_wav) |
| 55 | { |
| 56 | unsigned char buf[4]; |
| 57 | unsigned long rval; |
| 58 | |
| 59 | if (fread(buf, 1, 4, in_wav) != 4) { |
| 60 | fprintf(stderr, "AVI: Short read!\n"); |
| 61 | longjmp(error_jmp_env, 1); |
| 62 | } |
| 63 | |
| 64 | rval = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24); |
| 65 | return rval; |
| 66 | } |
| 67 | |
| 68 | unsigned long read_short(FILE* in_wav) |
| 69 | { |
| 70 | unsigned char buf[2]; |
| 71 | if (fread(buf, 1, 2, in_wav) != 2) { |
| 72 | fprintf(stderr, "AVI: Short read!\n"); |
| 73 | longjmp(error_jmp_env, 1); |
| 74 | } |
| 75 | |
| 76 | return buf[0] + (buf[1] << 8); |
| 77 | } |
| 78 | |
| 79 | int read_header(FILE* in_wav, ...) |
| 80 | { |
| 81 | unsigned char buf[4]; |
| 82 | char* header; |
| 83 | int i = 1; |
| 84 | |
| 85 | va_list ap; |
| 86 | va_start(ap, in_wav); |
| 87 | |
| 88 | if (fread(buf, 1, 4, in_wav) != 4) { |
| 89 | fprintf(stderr, "AVI: Short read!\n"); |
| 90 | longjmp(error_jmp_env, 1); |
| 91 | } |
| 92 | |
| 93 | for (;;) { |
| 94 | header = va_arg(ap, char *); |
| 95 | if (!header) break; |
| 96 | if (memcmp(buf, header, 4) == 0) { |
| 97 | return i; |
| 98 | } |
| 99 | i++; |
| 100 | } |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | void read_header_excp(FILE* in_wav, char* header) |
| 105 | { |
| 106 | if (!read_header(in_wav, header, NULL)) { |
| 107 | fprintf(stderr, "AVI: No %s header!\n", header); |
| 108 | longjmp(error_jmp_env, 1); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void skip(FILE* in_avi, unsigned long amount) |
| 113 | { |
| 114 | if (!amount) { |
| 115 | return; |
| 116 | } |
| 117 | if (fseek(in_avi, amount, SEEK_CUR) == -1) { |
| 118 | char buf[1024]; |
| 119 | |
| 120 | while (amount > 1024) { |
| 121 | if (fread(buf, 1, 1024, in_avi) < 1024) { |
| 122 | fprintf(stderr, "AVI: short read in skip!\n"); |
| 123 | longjmp(error_jmp_env, 1); |
| 124 | } |
| 125 | amount-=1024; |
| 126 | } |
| 127 | if (fread(buf, 1, amount, in_avi) < amount) { |
| 128 | fprintf(stderr, "AVI: short read in skip!\n"); |
| 129 | longjmp(error_jmp_env, 1); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | #define PAL_SIZE 144000 |
| 135 | #define NODE_SIZE PAL_SIZE |
| 136 | |
| 137 | struct buf_node { |
| 138 | unsigned char buffer[NODE_SIZE]; |
| 139 | int usage; |
| 140 | int processed; |
| 141 | struct buf_node* next; |
| 142 | }; |
| 143 | |
| 144 | struct buf_list { |
| 145 | struct buf_node * first; |
| 146 | struct buf_node * last; |
| 147 | int usage; |
| 148 | }; |
| 149 | |
| 150 | static struct buf_list free_list = { NULL, NULL, 0 }; |
| 151 | static struct buf_list video_bufs = { NULL, NULL, 0 }; |
| 152 | static struct buf_list audio_bufs = { NULL, NULL, 0 }; |
| 153 | |
| 154 | int max_buffer_blocks = 25; |
| 155 | |
| 156 | void push_back(struct buf_list * l, struct buf_node * elem) |
| 157 | { |
| 158 | if (l->last) { |
| 159 | l->last->next = elem; |
| 160 | } else { |
| 161 | l->first = elem; |
| 162 | } |
| 163 | elem->next = NULL; |
| 164 | l->last = elem; |
| 165 | l->usage++; |
| 166 | } |
| 167 | |
| 168 | struct buf_node * pop_front(struct buf_list * l) |
| 169 | { |
| 170 | struct buf_node * rval; |
| 171 | |
| 172 | rval = l->first; |
| 173 | |
| 174 | if (l->first == l->last) { |
| 175 | l->last = NULL; |
| 176 | } |
| 177 | if (rval) { |
| 178 | l->first = rval->next; |
| 179 | l->usage--; |
| 180 | } else { |
| 181 | l->first = NULL; |
| 182 | } |
| 183 | |
| 184 | return rval; |
| 185 | } |
| 186 | |
| 187 | struct buf_node * get_free_block() |
| 188 | { |
| 189 | struct buf_node * f = pop_front(&free_list); |
| 190 | if (!f) { |
| 191 | if (audio_bufs.usage >= max_buffer_blocks |
| 192 | || video_bufs.usage >= max_buffer_blocks) { |
| 193 | fprintf(stderr, "AVI: max buffer limit reached!\n"); |
| 194 | longjmp(error_jmp_env, 1); |
| 195 | } |
| 196 | f = (struct buf_node*) malloc(sizeof(struct buf_node)); |
| 197 | } |
| 198 | return f; |
| 199 | } |
| 200 | |
| 201 | dv_enc_audio_info_t audio_info; |
| 202 | |
| 203 | void get_audio_header(FILE* in_avi, int * len_, |
| 204 | dv_enc_audio_info_t * res) |
| 205 | { |
| 206 | int len = *len_; |
| 207 | int head_len; |
| 208 | int avihead_len; |
| 209 | |
| 210 | len -= 4; |
| 211 | if (!read_header(in_avi, "avih", NULL)) { |
| 212 | *len_ = len; |
| 213 | return; |
| 214 | } |
| 215 | len -= 4; |
| 216 | avihead_len = read_long(in_avi); |
| 217 | |
| 218 | skip(in_avi, avihead_len); |
| 219 | len -= avihead_len; |
| 220 | |
| 221 | while (len > 0) { |
| 222 | int strl_len; |
| 223 | |
| 224 | len -= 4; |
| 225 | if (!read_header(in_avi, "LIST", NULL)) { |
| 226 | *len_ = len; |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | len -= 4; |
| 231 | strl_len = read_long(in_avi); |
| 232 | |
| 233 | len -= 4; |
| 234 | strl_len -= 4; |
| 235 | if (!read_header(in_avi, "strl", NULL)) { |
| 236 | *len_ = len; |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | len -= 4; |
| 241 | strl_len -= 4; |
| 242 | if (!read_header(in_avi, "strh", NULL)) { |
| 243 | *len_ = len; |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | head_len = read_long(in_avi); |
| 248 | len -= 8; |
| 249 | strl_len -= 8; |
| 250 | if (!read_header(in_avi, "auds", NULL)) { |
| 251 | skip(in_avi, strl_len); |
| 252 | len -= strl_len; |
| 253 | } else { |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | if (len <= 0) { |
| 258 | *len_ = len; |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | skip(in_avi, head_len - 4); |
| 263 | len -= head_len - 4; |
| 264 | |
| 265 | len -= 4; |
| 266 | if (read_header(in_avi, "strf", NULL)) { |
| 267 | int ft; |
| 268 | len -= 4; |
| 269 | read_long(in_avi); /* len */ |
| 270 | ft = read_short(in_avi); /* format tag */ |
| 271 | fprintf(stderr, |
| 272 | "Found audio channel: format: ft=%d\n", ft); |
| 273 | len -= 2; |
| 274 | if (ft != 1) { |
| 275 | fprintf(stderr, "Not supported!!!\n"); |
| 276 | *len_ = len; |
| 277 | return; |
| 278 | } |
| 279 | res->channels = read_short(in_avi); |
| 280 | res->frequency = read_long(in_avi); |
| 281 | res->bytespersecond = read_long(in_avi); /* bytes per second */ |
| 282 | res->bytealignment = read_short(in_avi); /* byte alignment */ |
| 283 | res->bitspersample = read_short(in_avi); |
| 284 | |
| 285 | fprintf(stderr, "Opening audio source with:\n" |
| 286 | "Channels: %d\n" |
| 287 | "Frequency: %d\n" |
| 288 | "Bytes per second: %d\n" |
| 289 | "Byte alignment: %d\n" |
| 290 | "Bits per sample: %d\n", |
| 291 | res->channels, res->frequency, |
| 292 | res->bytespersecond, |
| 293 | res->bytealignment, |
| 294 | res->bitspersample); |
| 295 | |
| 296 | len -= 2 + 4 + 4 + 2 + 2; |
| 297 | } |
| 298 | *len_ = len; |
| 299 | } |
| 300 | |
| 301 | static void convert_s16_le(unsigned char* in_buf, unsigned char* out_buf, |
| 302 | int num_samples) |
| 303 | { |
| 304 | int i; |
| 305 | for (i = 0; i < num_samples; i++) { |
| 306 | char a = in_buf[1]; |
| 307 | char b = in_buf[0]; |
| 308 | *out_buf++ = a; |
| 309 | *out_buf++ = b; |
| 310 | in_buf += 2; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | int run_queue(dv_enc_output_filter_t * output_filter, time_t now, int isPAL) |
| 315 | { |
| 316 | struct buf_node * it; |
| 317 | struct buf_node * next; |
| 318 | int audio_needed; |
| 319 | unsigned char * p; |
| 320 | |
| 321 | if (!audio_bufs.usage || !video_bufs.usage) { |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | audio_needed = audio_info.bytesperframe; |
| 326 | |
| 327 | for (it = audio_bufs.first; it != NULL; it = it->next) { |
| 328 | audio_needed -= it->usage - it->processed; |
| 329 | if (audio_needed <= 0) { |
| 330 | break; |
| 331 | } |
| 332 | } |
| 333 | if (audio_needed > 0) { |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | p = audio_info.data; |
| 338 | audio_needed = audio_info.bytesperframe; |
| 339 | |
| 340 | for (it = audio_bufs.first; it != NULL; it = next) { |
| 341 | int real_usage = it->usage - it->processed; |
| 342 | next = it->next; |
| 343 | if (real_usage <= audio_needed) { |
| 344 | memcpy(p, it->buffer + it->processed, real_usage); |
| 345 | p += real_usage; |
| 346 | audio_needed -= real_usage; |
| 347 | push_back(&free_list, pop_front(&audio_bufs)); |
| 348 | } else { |
| 349 | memcpy(p, it->buffer + it->processed, audio_needed); |
| 350 | it->processed += audio_needed; |
| 351 | audio_needed = 0; |
| 352 | } |
| 353 | if (audio_needed == 0) { |
| 354 | break; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | it = pop_front(&video_bufs); |
| 359 | |
| 360 | convert_s16_le(audio_info.data, audio_info.data, |
| 361 | audio_info.bytesperframe/2); |
| 362 | |
| 363 | if (output_filter->store(it->buffer, &audio_info, 0, |
| 364 | isPAL, 0, now) != 0) { |
| 365 | fprintf(stderr, "AVI: write error!\n"); |
| 366 | longjmp(error_jmp_env, 1); |
| 367 | } |
| 368 | |
| 369 | push_back(&free_list, it); |
| 370 | |
| 371 | return 1; |
| 372 | } |
| 373 | |
| 374 | int read_avi(FILE* in_avi, dv_enc_output_filter_t * out_filter, |
| 375 | FILE* raw_dv_out) |
| 376 | { |
| 377 | int len; |
| 378 | int blocktype; |
| 379 | int frame_count; |
| 380 | time_t now = time(NULL); |
| 381 | volatile int isPAL = 0; |
| 382 | |
| 383 | if (setjmp(error_jmp_env)) { |
| 384 | while (run_queue(out_filter, now, isPAL)); |
| 385 | return -1; |
| 386 | } |
| 387 | |
| 388 | frame_count = 0; |
| 389 | |
| 390 | read_header_excp(in_avi, "RIFF"); |
| 391 | read_long(in_avi); /* ignore length, this is important, |
| 392 | since stream generated avis do not have a |
| 393 | valid length! */ |
| 394 | read_header_excp(in_avi, "AVI "); |
| 395 | len = 0; |
| 396 | for (;;) { |
| 397 | skip(in_avi, len); |
| 398 | while (!read_header(in_avi, "LIST", NULL)) { |
| 399 | skip(in_avi, read_long(in_avi)); |
| 400 | } |
| 401 | len = read_long(in_avi) - 4; |
| 402 | blocktype = read_header(in_avi, "movi", "hdrl", NULL); |
| 403 | if (blocktype == 1) { |
| 404 | break; |
| 405 | } else if (blocktype == 2) { |
| 406 | get_audio_header(in_avi, &len, &audio_info); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | for (;;) { |
| 411 | struct buf_node * bn = NULL; |
| 412 | |
| 413 | for (;;) { |
| 414 | blocktype = read_header( |
| 415 | in_avi, "00dc", "01wb", "00__", NULL); |
| 416 | if (blocktype != 0) { |
| 417 | break; |
| 418 | } |
| 419 | skip(in_avi, read_long(in_avi)); |
| 420 | } |
| 421 | len = read_long(in_avi); |
| 422 | |
| 423 | switch (blocktype) { |
| 424 | case 2: /* audio : DV format 2 */ |
| 425 | if (!audio_bufs.usage || |
| 426 | (audio_bufs.last->usage + len > NODE_SIZE)) { |
| 427 | bn = get_free_block(); |
| 428 | fread(bn->buffer, 1, len, in_avi); |
| 429 | bn->usage = len; |
| 430 | bn->processed = 0; |
| 431 | push_back(&audio_bufs, bn); |
| 432 | } else { |
| 433 | bn = audio_bufs.last; |
| 434 | fread(bn->buffer + bn->usage, 1, len, in_avi); |
| 435 | bn->usage += len; |
| 436 | } |
| 437 | break; |
| 438 | case 1: /* video : DV format 2 */ |
| 439 | bn = get_free_block(); |
| 440 | if (len > NODE_SIZE) { |
| 441 | fprintf(stderr, "AVI: DV frame too large!\n"); |
| 442 | longjmp(error_jmp_env, 1); |
| 443 | } |
| 444 | isPAL = (len == PAL_SIZE); |
| 445 | fread(bn->buffer, 1, len, in_avi); |
| 446 | audio_info.bytesperframe = |
| 447 | audio_info.bytespersecond / (isPAL ? 25 : 30); |
| 448 | bn->usage = len; |
| 449 | push_back(&video_bufs, bn); |
| 450 | break; |
| 451 | case 3: /* video / audio : DV format 1*/ |
| 452 | bn = get_free_block(); |
| 453 | if (len > NODE_SIZE) { |
| 454 | fprintf(stderr, "AVI: DV frame too large!\n"); |
| 455 | longjmp(error_jmp_env, 1); |
| 456 | } |
| 457 | fread(bn->buffer, 1, len, in_avi); |
| 458 | fwrite(bn->buffer, 1, len, raw_dv_out); |
| 459 | |
| 460 | push_back(&free_list, bn); |
| 461 | break; |
| 462 | } |
| 463 | while (run_queue(out_filter, now, isPAL)); |
| 464 | } |
| 465 | while (run_queue(out_filter, now, isPAL)); |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | int main(int argc, const char** argv) |
| 470 | { |
| 471 | FILE* in_avi = NULL; |
| 472 | const char* output_filter_str = "raw"; |
| 473 | dv_enc_output_filter_t * output_filter; |
| 474 | int count; |
| 475 | |
| 476 | if (argc < 2) { |
| 477 | fprintf(stderr, "Usage: dvavi avi-file > raw-dv-file\n"); |
| 478 | exit(0); |
| 479 | } |
| 480 | |
| 481 | if (strcmp(argv[1], "-") == 0) { |
| 482 | in_avi = stdin; |
| 483 | } else { |
| 484 | in_avi = fopen(argv[1], "r"); |
| 485 | } |
| 486 | |
| 487 | if (in_avi == NULL) { |
| 488 | fprintf(stderr, "AVI: can't open file: %s\n", argv[1]); |
| 489 | exit(-1); |
| 490 | } |
| 491 | |
| 492 | dv_enc_get_output_filters(&output_filter, &count); |
| 493 | while (count && |
| 494 | strcmp(output_filter->filter_name, output_filter_str) != 0) { |
| 495 | output_filter++; |
| 496 | count--; |
| 497 | } |
| 498 | if (!count) { |
| 499 | fprintf(stderr, "Unknown output filter selected: %s!\n" |
| 500 | "The following filters are supported:\n", |
| 501 | output_filter_str); |
| 502 | dv_enc_get_output_filters(&output_filter, &count); |
| 503 | while (count--) { |
| 504 | fprintf(stderr, "%s\n", output_filter->filter_name); |
| 505 | output_filter++; |
| 506 | } |
| 507 | return(-1); |
| 508 | } |
| 509 | |
| 510 | output_filter->init(stdout); |
| 511 | |
| 512 | return read_avi(in_avi, output_filter, stdout); |
| 513 | } |
| 514 | |
| 515 | |
| 516 |