- AVFormatContext *out_file;
- AVFormatContext *in_file;
- AVMetadataTag *mtag;
-
- int out_file_index = meta_data_maps[i].out_file;
- int in_file_index = meta_data_maps[i].in_file;
- if (out_file_index < 0 || out_file_index >= nb_output_files) {
- snprintf(error, sizeof(error), "Invalid output file index %d map_meta_data(%d,%d)",
- out_file_index, out_file_index, in_file_index);
+ AVFormatContext *files[2];
+ AVMetadata **meta[2];
+ int j;
+
+#define METADATA_CHECK_INDEX(index, nb_elems, desc)\
+ if ((index) < 0 || (index) >= (nb_elems)) {\
+ snprintf(error, sizeof(error), "Invalid %s index %d while processing metadata maps\n",\
+ (desc), (index));\
+ ret = AVERROR(EINVAL);\
+ goto dump_format;\
+ }
+
+ int out_file_index = meta_data_maps[i][0].file;
+ int in_file_index = meta_data_maps[i][1].file;
+ if (in_file_index < 0 || out_file_index < 0)
+ continue;
+ METADATA_CHECK_INDEX(out_file_index, nb_output_files, "output file")
+ METADATA_CHECK_INDEX(in_file_index, nb_input_files, "input file")
+
+ files[0] = output_files[out_file_index];
+ files[1] = input_files[in_file_index];
+
+ for (j = 0; j < 2; j++) {
+ AVMetaDataMap *map = &meta_data_maps[i][j];
+
+ switch (map->type) {
+ case 'g':
+ meta[j] = &files[j]->metadata;
+ break;
+ case 's':
+ METADATA_CHECK_INDEX(map->index, files[j]->nb_streams, "stream")
+ meta[j] = &files[j]->streams[map->index]->metadata;
+ break;
+ case 'c':
+ METADATA_CHECK_INDEX(map->index, files[j]->nb_chapters, "chapter")
+ meta[j] = &files[j]->chapters[map->index]->metadata;
+ break;
+ case 'p':
+ METADATA_CHECK_INDEX(map->index, files[j]->nb_programs, "program")
+ meta[j] = &files[j]->programs[map->index]->metadata;
+ break;
+ }
+ }
+
+ av_metadata_copy(meta[0], *meta[1], AV_METADATA_DONT_OVERWRITE);
+ }
+
+ /* copy global metadata by default */
+ if (metadata_global_autocopy) {
+
+ for (i = 0; i < nb_output_files; i++)
+ av_metadata_copy(&output_files[i]->metadata, input_files[0]->metadata,
+ AV_METADATA_DONT_OVERWRITE);
+ }
+
+ /* copy chapters according to chapter maps */
+ for (i = 0; i < nb_chapter_maps; i++) {
+ int infile = chapter_maps[i].in_file;
+ int outfile = chapter_maps[i].out_file;
+
+ if (infile < 0 || outfile < 0)
+ continue;
+ if (infile >= nb_input_files) {
+ snprintf(error, sizeof(error), "Invalid input file index %d in chapter mapping.\n", infile);