/* * mvj - extract JPEG fields/frames from JPEG Quicktime movie (data untouched) * * mvj file.mv [trackno] frameno */ #include #include #include #include #include #include #define MVPR(s) fprintf(stderr, "%s: %s\n", s, mvGetErrorStr(mvGetErrno())) #define EMV(func, s) if (func == DM_FAILURE) { MVPR(s); exit(1); } char *prog; static void writefile(int n, int f, char *cp, size_t nb) { char name[64]; int fd; if (f) { sprintf(name, "%05d.%d.jpeg", n, f); } else { sprintf(name, "%05d.jpeg", n); } if ((fd = open(name, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) { perror(name); exit(1); } printf("%s %d bytes\n", name, nb); if (write(fd, cp, nb) != nb) { perror(name); exit(1); } close(fd); } MVid movie, track; double rate; MVframe nframes, fr; int index; MVtime mvtime; MVtime dur; MVtimescale tscale; void xframe(frameno) { mvtime = (long long)frameno * dur; EMV(mvGetTrackDataIndexAtTime(track, mvtime, tscale, &index, &fr), "get track index"); if (mvTrackDataHasFieldInfo(track, index)) { size_t nb1, nb2; char *buf1, *buf2; off64_t g; EMV(mvGetTrackDataFieldInfo(track, index, &nb1, &g, &nb2), "get field info"); buf1 = malloc(nb1); buf2 = malloc(nb2); if (!buf1 || !buf2) { fprintf(stderr, "%s: malloc(%d,%d) failed\n", prog, nb1, nb2); exit(1); } EMV(mvReadTrackDataFields(track, index, nb1, buf1, nb2, buf2), "read field data"); writefile(frameno, 1, buf1, nb1); writefile(frameno, 2, buf2, nb2); } else { MVdatatype dt; size_t nb; char *buf; int p; EMV(mvGetTrackDataInfo(track, index, &nb, &p, &dt, &fr), "get track info"); if ((buf = malloc(nb)) == NULL) { fprintf(stderr, "%s: malloc failed\n", prog); exit(1); } EMV(mvReadTrackData(track, index, nb, buf), "read track data"); writefile(frameno, 0, buf, nb); } } main(int argc, char **argv) { char *mvname = argv[1]; int frameno, trackno; prog = argv[0]; if (argc != 3 && argc != 4) { fprintf(stderr, "usage: %s file.mv [trackno] frameno|\'a\'\n", prog); exit(1); } trackno = argc == 4 ? atoi(argv[2]) : 0; if (argv[argc - 1][0] == 'a') { frameno = -1; } else { frameno = atoi(argv[argc - 1]); } EMV(mvOpenFile(mvname, O_RDONLY, &movie), mvname); if (mvGetFileFormat(movie) != MV_FORMAT_QT) { fprintf(stderr, "%s: %s is not Quicktime\n", prog, mvname); exit(1); } EMV(mvFindTrackByIndex(movie, trackno, &track), "find track N"); if (mvGetTrackMedium(track) != DM_IMAGE) { fprintf(stderr, "%s: %s track %d is not DM_IMAGE\n", prog, mvname, trackno); exit(1); } if (strcmp(mvGetImageCompression(track), "JPEG") != 0) { fprintf(stderr, "%s: %s track %d is not JPEG\n", prog, mvname, trackno); exit(1); } nframes = mvGetTrackLength(track); tscale = mvGetMovieTimeScale(movie); rate = mvGetImageRate(track); dur = (long long)((double)tscale/rate); if (frameno == -1) { for (frameno = 0; frameno < nframes; frameno++) { xframe(frameno); } } else { if (frameno < 0 || frameno >= nframes) { fprintf(stderr, "%s: %s has %d frames\n", prog, mvname, nframes); exit(1); } xframe(frameno); } }