Libav
ismindex.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Martin Storsjo
3  *
4  * This file is part of Libav.
5  *
6  * Libav is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * Libav is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * To create a simple file for smooth streaming:
23  * avconv <normal input/transcoding options> -movflags frag_keyframe foo.ismv
24  * ismindex -n foo foo.ismv
25  * This step creates foo.ism and foo.ismc that is required by IIS for
26  * serving it.
27  *
28  * By adding -path-prefix path/, the produced foo.ism will refer to the
29  * files foo.ismv as "path/foo.ismv" - the prefix for the generated ismc
30  * file can be set with the -ismc-prefix option similarly.
31  *
32  * To pre-split files for serving as static files by a web server without
33  * any extra server support, create the ismv file as above, and split it:
34  * ismindex -split foo.ismv
35  * This step creates a file Manifest and directories QualityLevel(...),
36  * that can be read directly by a smooth streaming player.
37  *
38  * The -output dir option can be used to request that output files
39  * (both .ism/.ismc, or Manifest/QualityLevels* when splitting)
40  * should be written to this directory instead of in the current directory.
41  * (The directory itself isn't created if it doesn't already exist.)
42  */
43 
44 #include <stdio.h>
45 #include <string.h>
46 
47 #include "libavformat/avformat.h"
48 #include "libavformat/os_support.h"
49 #include "libavutil/intreadwrite.h"
50 #include "libavutil/mathematics.h"
51 
52 static int usage(const char *argv0, int ret)
53 {
54  fprintf(stderr, "%s [-split] [-n basename] [-path-prefix prefix] "
55  "[-ismc-prefix prefix] [-output dir] file1 [file2] ...\n", argv0);
56  return ret;
57 }
58 
59 struct MoofOffset {
60  int64_t time;
61  int64_t offset;
62  int64_t duration;
63 };
64 
65 struct Track {
66  const char *name;
67  int64_t duration;
68  int bitrate;
69  int track_id;
71  int width, height;
72  int chunks;
77  int timescale;
78  const char *fourcc;
79  int blocksize;
80  int tag;
81 };
82 
83 struct Tracks {
84  int nb_tracks;
85  int64_t duration;
86  struct Track **tracks;
89 };
90 
91 static int copy_tag(AVIOContext *in, AVIOContext *out, int32_t tag_name)
92 {
93  int32_t size, tag;
94 
95  size = avio_rb32(in);
96  tag = avio_rb32(in);
97  avio_wb32(out, size);
98  avio_wb32(out, tag);
99  if (tag != tag_name)
100  return -1;
101  size -= 8;
102  while (size > 0) {
103  char buf[1024];
104  int len = FFMIN(sizeof(buf), size);
105  if (avio_read(in, buf, len) != len)
106  break;
107  avio_write(out, buf, len);
108  size -= len;
109  }
110  return 0;
111 }
112 
113 static int write_fragment(const char *filename, AVIOContext *in)
114 {
115  AVIOContext *out = NULL;
116  int ret;
117 
118  if ((ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, NULL, NULL)) < 0)
119  return ret;
120  copy_tag(in, out, MKBETAG('m', 'o', 'o', 'f'));
121  copy_tag(in, out, MKBETAG('m', 'd', 'a', 't'));
122 
123  avio_flush(out);
124  avio_close(out);
125 
126  return ret;
127 }
128 
129 static int write_fragments(struct Tracks *tracks, int start_index,
130  AVIOContext *in, const char *output_prefix)
131 {
132  char dirname[2048], filename[2048];
133  int i, j;
134 
135  for (i = start_index; i < tracks->nb_tracks; i++) {
136  struct Track *track = tracks->tracks[i];
137  const char *type = track->is_video ? "video" : "audio";
138  snprintf(dirname, sizeof(dirname), "%sQualityLevels(%d)", output_prefix, track->bitrate);
139  mkdir(dirname, 0777);
140  for (j = 0; j < track->chunks; j++) {
141  snprintf(filename, sizeof(filename), "%s/Fragments(%s=%"PRId64")",
142  dirname, type, track->offsets[j].time);
143  avio_seek(in, track->offsets[j].offset, SEEK_SET);
144  write_fragment(filename, in);
145  }
146  }
147  return 0;
148 }
149 
150 static int read_tfra(struct Tracks *tracks, int start_index, AVIOContext *f)
151 {
152  int ret = AVERROR_EOF, track_id;
153  int version, fieldlength, i, j;
154  int64_t pos = avio_tell(f);
155  uint32_t size = avio_rb32(f);
156  struct Track *track = NULL;
157 
158  if (avio_rb32(f) != MKBETAG('t', 'f', 'r', 'a'))
159  goto fail;
160  version = avio_r8(f);
161  avio_rb24(f);
162  track_id = avio_rb32(f); /* track id */
163  for (i = start_index; i < tracks->nb_tracks && !track; i++)
164  if (tracks->tracks[i]->track_id == track_id)
165  track = tracks->tracks[i];
166  if (!track) {
167  /* Ok, continue parsing the next atom */
168  ret = 0;
169  goto fail;
170  }
171  fieldlength = avio_rb32(f);
172  track->chunks = avio_rb32(f);
173  track->offsets = av_mallocz(sizeof(*track->offsets) * track->chunks);
174  if (!track->offsets) {
175  ret = AVERROR(ENOMEM);
176  goto fail;
177  }
178  for (i = 0; i < track->chunks; i++) {
179  if (version == 1) {
180  track->offsets[i].time = avio_rb64(f);
181  track->offsets[i].offset = avio_rb64(f);
182  } else {
183  track->offsets[i].time = avio_rb32(f);
184  track->offsets[i].offset = avio_rb32(f);
185  }
186  for (j = 0; j < ((fieldlength >> 4) & 3) + 1; j++)
187  avio_r8(f);
188  for (j = 0; j < ((fieldlength >> 2) & 3) + 1; j++)
189  avio_r8(f);
190  for (j = 0; j < ((fieldlength >> 0) & 3) + 1; j++)
191  avio_r8(f);
192  if (i > 0)
193  track->offsets[i - 1].duration = track->offsets[i].time -
194  track->offsets[i - 1].time;
195  }
196  if (track->chunks > 0)
197  track->offsets[track->chunks - 1].duration = track->duration -
198  track->offsets[track->chunks - 1].time;
199  ret = 0;
200 
201 fail:
202  avio_seek(f, pos + size, SEEK_SET);
203  return ret;
204 }
205 
206 static int read_mfra(struct Tracks *tracks, int start_index,
207  const char *file, int split, const char *output_prefix)
208 {
209  int err = 0;
210  AVIOContext *f = NULL;
211  int32_t mfra_size;
212 
213  if ((err = avio_open2(&f, file, AVIO_FLAG_READ, NULL, NULL)) < 0)
214  goto fail;
215  avio_seek(f, avio_size(f) - 4, SEEK_SET);
216  mfra_size = avio_rb32(f);
217  avio_seek(f, -mfra_size, SEEK_CUR);
218  if (avio_rb32(f) != mfra_size) {
219  err = AVERROR_INVALIDDATA;
220  goto fail;
221  }
222  if (avio_rb32(f) != MKBETAG('m', 'f', 'r', 'a')) {
223  err = AVERROR_INVALIDDATA;
224  goto fail;
225  }
226  while (!read_tfra(tracks, start_index, f)) {
227  /* Empty */
228  }
229 
230  if (split)
231  write_fragments(tracks, start_index, f, output_prefix);
232 
233 fail:
234  if (f)
235  avio_close(f);
236  if (err)
237  fprintf(stderr, "Unable to read the MFRA atom in %s\n", file);
238  return err;
239 }
240 
241 static int get_private_data(struct Track *track, AVCodecContext *codec)
242 {
243  track->codec_private_size = codec->extradata_size;
244  track->codec_private = av_mallocz(codec->extradata_size);
245  if (!track->codec_private)
246  return AVERROR(ENOMEM);
247  memcpy(track->codec_private, codec->extradata, codec->extradata_size);
248  return 0;
249 }
250 
251 static int get_video_private_data(struct Track *track, AVCodecContext *codec)
252 {
253  AVIOContext *io = NULL;
254  uint16_t sps_size, pps_size;
255  int err = AVERROR(EINVAL);
256 
257  if (codec->codec_id == AV_CODEC_ID_VC1)
258  return get_private_data(track, codec);
259 
260  avio_open_dyn_buf(&io);
261  if (codec->extradata_size < 11 || codec->extradata[0] != 1)
262  goto fail;
263  sps_size = AV_RB16(&codec->extradata[6]);
264  if (11 + sps_size > codec->extradata_size)
265  goto fail;
266  avio_wb32(io, 0x00000001);
267  avio_write(io, &codec->extradata[8], sps_size);
268  pps_size = AV_RB16(&codec->extradata[9 + sps_size]);
269  if (11 + sps_size + pps_size > codec->extradata_size)
270  goto fail;
271  avio_wb32(io, 0x00000001);
272  avio_write(io, &codec->extradata[11 + sps_size], pps_size);
273  err = 0;
274 
275 fail:
277  return err;
278 }
279 
280 static int handle_file(struct Tracks *tracks, const char *file, int split,
281  const char *output_prefix)
282 {
283  AVFormatContext *ctx = NULL;
284  int err = 0, i, orig_tracks = tracks->nb_tracks;
285  char errbuf[50], *ptr;
286  struct Track *track;
287 
288  err = avformat_open_input(&ctx, file, NULL, NULL);
289  if (err < 0) {
290  av_strerror(err, errbuf, sizeof(errbuf));
291  fprintf(stderr, "Unable to open %s: %s\n", file, errbuf);
292  return 1;
293  }
294 
295  err = avformat_find_stream_info(ctx, NULL);
296  if (err < 0) {
297  av_strerror(err, errbuf, sizeof(errbuf));
298  fprintf(stderr, "Unable to identify %s: %s\n", file, errbuf);
299  goto fail;
300  }
301 
302  if (ctx->nb_streams < 1) {
303  fprintf(stderr, "No streams found in %s\n", file);
304  goto fail;
305  }
306 
307  for (i = 0; i < ctx->nb_streams; i++) {
308  struct Track **temp;
309  AVStream *st = ctx->streams[i];
310  track = av_mallocz(sizeof(*track));
311  if (!track) {
312  err = AVERROR(ENOMEM);
313  goto fail;
314  }
315  temp = av_realloc(tracks->tracks,
316  sizeof(*tracks->tracks) * (tracks->nb_tracks + 1));
317  if (!temp) {
318  av_free(track);
319  err = AVERROR(ENOMEM);
320  goto fail;
321  }
322  tracks->tracks = temp;
323  tracks->tracks[tracks->nb_tracks] = track;
324 
325  track->name = file;
326  if ((ptr = strrchr(file, '/')))
327  track->name = ptr + 1;
328 
329  track->bitrate = st->codec->bit_rate;
330  track->track_id = st->id;
331  track->timescale = st->time_base.den;
332  track->duration = st->duration;
333  track->is_audio = st->codec->codec_type == AVMEDIA_TYPE_AUDIO;
334  track->is_video = st->codec->codec_type == AVMEDIA_TYPE_VIDEO;
335 
336  if (!track->is_audio && !track->is_video) {
337  fprintf(stderr,
338  "Track %d in %s is neither video nor audio, skipping\n",
339  track->track_id, file);
340  av_freep(&tracks->tracks[tracks->nb_tracks]);
341  continue;
342  }
343 
344  tracks->duration = FFMAX(tracks->duration,
346  track->timescale, AV_ROUND_UP));
347 
348  if (track->is_audio) {
349  if (tracks->audio_track < 0)
350  tracks->audio_track = tracks->nb_tracks;
351  tracks->nb_audio_tracks++;
352  track->channels = st->codec->channels;
353  track->sample_rate = st->codec->sample_rate;
354  if (st->codec->codec_id == AV_CODEC_ID_AAC) {
355  track->fourcc = "AACL";
356  track->tag = 255;
357  track->blocksize = 4;
358  } else if (st->codec->codec_id == AV_CODEC_ID_WMAPRO) {
359  track->fourcc = "WMAP";
360  track->tag = st->codec->codec_tag;
361  track->blocksize = st->codec->block_align;
362  }
363  get_private_data(track, st->codec);
364  }
365  if (track->is_video) {
366  if (tracks->video_track < 0)
367  tracks->video_track = tracks->nb_tracks;
368  tracks->nb_video_tracks++;
369  track->width = st->codec->width;
370  track->height = st->codec->height;
371  if (st->codec->codec_id == AV_CODEC_ID_H264)
372  track->fourcc = "H264";
373  else if (st->codec->codec_id == AV_CODEC_ID_VC1)
374  track->fourcc = "WVC1";
375  get_video_private_data(track, st->codec);
376  }
377 
378  tracks->nb_tracks++;
379  }
380 
381  avformat_close_input(&ctx);
382 
383  err = read_mfra(tracks, orig_tracks, file, split, output_prefix);
384 
385 fail:
386  if (ctx)
387  avformat_close_input(&ctx);
388  return err;
389 }
390 
391 static void output_server_manifest(struct Tracks *tracks, const char *basename,
392  const char *output_prefix,
393  const char *path_prefix,
394  const char *ismc_prefix)
395 {
396  char filename[1000];
397  FILE *out;
398  int i;
399 
400  snprintf(filename, sizeof(filename), "%s%s.ism", output_prefix, basename);
401  out = fopen(filename, "w");
402  if (!out) {
403  perror(filename);
404  return;
405  }
406  fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
407  fprintf(out, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
408  fprintf(out, "\t<head>\n");
409  fprintf(out, "\t\t<meta name=\"clientManifestRelativePath\" "
410  "content=\"%s%s.ismc\" />\n", ismc_prefix, basename);
411  fprintf(out, "\t</head>\n");
412  fprintf(out, "\t<body>\n");
413  fprintf(out, "\t\t<switch>\n");
414  for (i = 0; i < tracks->nb_tracks; i++) {
415  struct Track *track = tracks->tracks[i];
416  const char *type = track->is_video ? "video" : "audio";
417  fprintf(out, "\t\t\t<%s src=\"%s%s\" systemBitrate=\"%d\">\n",
418  type, path_prefix, track->name, track->bitrate);
419  fprintf(out, "\t\t\t\t<param name=\"trackID\" value=\"%d\" "
420  "valueType=\"data\" />\n", track->track_id);
421  fprintf(out, "\t\t\t</%s>\n", type);
422  }
423  fprintf(out, "\t\t</switch>\n");
424  fprintf(out, "\t</body>\n");
425  fprintf(out, "</smil>\n");
426  fclose(out);
427 }
428 
429 static void print_track_chunks(FILE *out, struct Tracks *tracks, int main,
430  const char *type)
431 {
432  int i, j;
433  struct Track *track = tracks->tracks[main];
434  for (i = 0; i < track->chunks; i++) {
435  for (j = main + 1; j < tracks->nb_tracks; j++) {
436  if (tracks->tracks[j]->is_audio == track->is_audio &&
437  track->offsets[i].duration != tracks->tracks[j]->offsets[i].duration)
438  fprintf(stderr, "Mismatched duration of %s chunk %d in %s and %s\n",
439  type, i, track->name, tracks->tracks[j]->name);
440  }
441  fprintf(out, "\t\t<c n=\"%d\" d=\"%"PRId64"\" />\n",
442  i, track->offsets[i].duration);
443  }
444 }
445 
446 static void output_client_manifest(struct Tracks *tracks, const char *basename,
447  const char *output_prefix, int split)
448 {
449  char filename[1000];
450  FILE *out;
451  int i, j;
452 
453  if (split)
454  snprintf(filename, sizeof(filename), "%sManifest", output_prefix);
455  else
456  snprintf(filename, sizeof(filename), "%s%s.ismc", output_prefix, basename);
457  out = fopen(filename, "w");
458  if (!out) {
459  perror(filename);
460  return;
461  }
462  fprintf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
463  fprintf(out, "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" "
464  "Duration=\"%"PRId64 "\">\n", tracks->duration * 10);
465  if (tracks->video_track >= 0) {
466  struct Track *track = tracks->tracks[tracks->video_track];
467  struct Track *first_track = track;
468  int index = 0;
469  fprintf(out,
470  "\t<StreamIndex Type=\"video\" QualityLevels=\"%d\" "
471  "Chunks=\"%d\" "
472  "Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n",
473  tracks->nb_video_tracks, track->chunks);
474  for (i = 0; i < tracks->nb_tracks; i++) {
475  track = tracks->tracks[i];
476  if (!track->is_video)
477  continue;
478  fprintf(out,
479  "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
480  "FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" "
481  "CodecPrivateData=\"",
482  index, track->bitrate, track->fourcc, track->width, track->height);
483  for (j = 0; j < track->codec_private_size; j++)
484  fprintf(out, "%02X", track->codec_private[j]);
485  fprintf(out, "\" />\n");
486  index++;
487  if (track->chunks != first_track->chunks)
488  fprintf(stderr, "Mismatched number of video chunks in %s and %s\n",
489  track->name, first_track->name);
490  }
491  print_track_chunks(out, tracks, tracks->video_track, "video");
492  fprintf(out, "\t</StreamIndex>\n");
493  }
494  if (tracks->audio_track >= 0) {
495  struct Track *track = tracks->tracks[tracks->audio_track];
496  struct Track *first_track = track;
497  int index = 0;
498  fprintf(out,
499  "\t<StreamIndex Type=\"audio\" QualityLevels=\"%d\" "
500  "Chunks=\"%d\" "
501  "Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n",
502  tracks->nb_audio_tracks, track->chunks);
503  for (i = 0; i < tracks->nb_tracks; i++) {
504  track = tracks->tracks[i];
505  if (!track->is_audio)
506  continue;
507  fprintf(out,
508  "\t\t<QualityLevel Index=\"%d\" Bitrate=\"%d\" "
509  "FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" "
510  "BitsPerSample=\"16\" PacketSize=\"%d\" "
511  "AudioTag=\"%d\" CodecPrivateData=\"",
512  index, track->bitrate, track->fourcc, track->sample_rate,
513  track->channels, track->blocksize, track->tag);
514  for (j = 0; j < track->codec_private_size; j++)
515  fprintf(out, "%02X", track->codec_private[j]);
516  fprintf(out, "\" />\n");
517  index++;
518  if (track->chunks != first_track->chunks)
519  fprintf(stderr, "Mismatched number of audio chunks in %s and %s\n",
520  track->name, first_track->name);
521  }
522  print_track_chunks(out, tracks, tracks->audio_track, "audio");
523  fprintf(out, "\t</StreamIndex>\n");
524  }
525  fprintf(out, "</SmoothStreamingMedia>\n");
526  fclose(out);
527 }
528 
529 static void clean_tracks(struct Tracks *tracks)
530 {
531  int i;
532  for (i = 0; i < tracks->nb_tracks; i++) {
533  av_freep(&tracks->tracks[i]->codec_private);
534  av_freep(&tracks->tracks[i]->offsets);
535  av_freep(&tracks->tracks[i]);
536  }
537  av_freep(&tracks->tracks);
538  tracks->nb_tracks = 0;
539 }
540 
541 int main(int argc, char **argv)
542 {
543  const char *basename = NULL;
544  const char *path_prefix = "", *ismc_prefix = "";
545  const char *output_prefix = "";
546  char output_prefix_buf[2048];
547  int split = 0, i;
548  struct Tracks tracks = { 0, .video_track = -1, .audio_track = -1 };
549 
550  av_register_all();
551 
552  for (i = 1; i < argc; i++) {
553  if (!strcmp(argv[i], "-n")) {
554  basename = argv[i + 1];
555  i++;
556  } else if (!strcmp(argv[i], "-path-prefix")) {
557  path_prefix = argv[i + 1];
558  i++;
559  } else if (!strcmp(argv[i], "-ismc-prefix")) {
560  ismc_prefix = argv[i + 1];
561  i++;
562  } else if (!strcmp(argv[i], "-output")) {
563  output_prefix = argv[i + 1];
564  i++;
565  if (output_prefix[strlen(output_prefix) - 1] != '/') {
566  snprintf(output_prefix_buf, sizeof(output_prefix_buf),
567  "%s/", output_prefix);
568  output_prefix = output_prefix_buf;
569  }
570  } else if (!strcmp(argv[i], "-split")) {
571  split = 1;
572  } else if (argv[i][0] == '-') {
573  return usage(argv[0], 1);
574  } else {
575  if (handle_file(&tracks, argv[i], split, output_prefix))
576  return 1;
577  }
578  }
579  if (!tracks.nb_tracks || (!basename && !split))
580  return usage(argv[0], 1);
581 
582  if (!split)
583  output_server_manifest(&tracks, basename, output_prefix,
584  path_prefix, ismc_prefix);
585  output_client_manifest(&tracks, basename, output_prefix, split);
586 
587  clean_tracks(&tracks);
588 
589  return 0;
590 }
static int copy_tag(AVIOContext *in, AVIOContext *out, int32_t tag_name)
Definition: ismindex.c:91
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:241
int size
int avio_close_dyn_buf(AVIOContext *s, uint8_t **pbuffer)
Return the written size and a pointer to the buffer.
Definition: aviobuf.c:966
static int usage(const char *argv0, int ret)
Definition: ismindex.c:52
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:61
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:247
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
#define AVIO_FLAG_READ
read-only
Definition: avio.h:292
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:293
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
int is_audio
Definition: ismindex.c:70
static void print_track_chunks(FILE *out, struct Tracks *tracks, int main, const char *type)
Definition: ismindex.c:429
int avio_open_dyn_buf(AVIOContext **s)
Open a write only memory stream.
Definition: aviobuf.c:954
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1844
int tag
Definition: ismindex.c:80
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
int height
Definition: ismindex.c:71
Format I/O context.
Definition: avformat.h:922
uint8_t
Round toward +infinity.
Definition: mathematics.h:53
struct MoofOffset * offsets
Definition: ismindex.c:76
miscellaneous OS support macros and functions.
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:595
int id
Format-specific stream ID.
Definition: avformat.h:706
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
int bitrate
Definition: ismindex.c:68
int nb_tracks
Definition: ismindex.c:84
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
uint32_t tag
Definition: movenc.c:844
#define AVERROR_EOF
End of file.
Definition: error.h:51
Definition: ismindex.c:65
int64_t time
Definition: ismindex.c:60
uint64_t avio_rb64(AVIOContext *s)
Definition: aviobuf.c:660
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:165
static int write_fragment(const char *filename, AVIOContext *in)
Definition: ismindex.c:113
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
int width
Definition: ismindex.c:71
int is_video
Definition: ismindex.c:70
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
int channels
Definition: ismindex.c:73
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:800
int sample_rate
Definition: ismindex.c:73
static void output_client_manifest(struct Tracks *tracks, const char *basename, const char *output_prefix, int split)
Definition: ismindex.c:446
int64_t duration
Definition: ismindex.c:85
#define FFMAX(a, b)
Definition: common.h:55
static char * split(char *message, char delim)
Definition: af_channelmap.c:85
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:443
const char * fourcc
Definition: ismindex.c:78
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:978
static int handle_file(struct Tracks *tracks, const char *file, int split, const char *output_prefix)
Definition: ismindex.c:280
int bit_rate
the average bitrate
Definition: avcodec.h:1114
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:180
unsigned int avio_rb24(AVIOContext *s)
Definition: aviobuf.c:588
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:234
#define FFMIN(a, b)
Definition: common.h:57
int width
picture width / height.
Definition: avcodec.h:1229
int chunks
Definition: ismindex.c:72
int32_t
int audio_track
Definition: ismindex.c:87
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
version
Definition: ffv1enc.c:1080
static int write_fragments(struct Tracks *tracks, int start_index, AVIOContext *in, const char *output_prefix)
Definition: ismindex.c:129
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1807
int64_t duration
Definition: ismindex.c:67
static int get_private_data(struct Track *track, AVCodecContext *codec)
Definition: ismindex.c:241
main external API structure.
Definition: avcodec.h:1050
uint8_t * codec_private
Definition: ismindex.c:74
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
int timescale
Definition: ismindex.c:77
int extradata_size
Definition: avcodec.h:1165
struct Track ** tracks
Definition: ismindex.c:86
int index
Definition: gxfenc.c:72
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:783
static void clean_tracks(struct Tracks *tracks)
Definition: ismindex.c:529
int track_id
Definition: ismindex.c:69
int64_t duration
Definition: ismindex.c:62
int codec_private_size
Definition: ismindex.c:75
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
Definition: error.c:23
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:756
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
static int get_video_private_data(struct Track *track, AVCodecContext *codec)
Definition: ismindex.c:251
Main libavformat public API header.
int video_track
Definition: ismindex.c:87
int blocksize
Definition: ismindex.c:79
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:2052
const char * name
Definition: ismindex.c:66
int den
denominator
Definition: rational.h:45
void avformat_close_input(AVFormatContext **s)
Close an opened input AVFormatContext.
Definition: utils.c:2499
#define MKBETAG(a, b, c, d)
Definition: common.h:239
int len
int channels
number of audio channels
Definition: avcodec.h:1808
void avio_wb32(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:268
static int read_mfra(struct Tracks *tracks, int start_index, const char *file, int split, const char *output_prefix)
Definition: ismindex.c:206
static int read_tfra(struct Tracks *tracks, int start_index, AVIOContext *f)
Definition: ismindex.c:150
int nb_audio_tracks
Definition: ismindex.c:88
int main(int argc, char **argv)
Definition: ismindex.c:541
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:741
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:51
static void output_server_manifest(struct Tracks *tracks, const char *basename, const char *output_prefix, const char *path_prefix, const char *ismc_prefix)
Definition: ismindex.c:391
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
int64_t offset
Definition: ismindex.c:61
int nb_video_tracks
Definition: ismindex.c:88