/*
 *			GPAC - MPEG-4 Systems C Development Kit
 *
 *			Copyright (c) Cyril Concolato 2005
 *					All rights reserved
 *
 *  This file is part of GPAC / command-line mp21 toolbox
 *
 *  GPAC is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  GPAC is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 */

#include <gpac/intern/m4_isomedia_dev.h>
#include <stdio.h>

void DumpItemInfo(M4File *mp4, u32 itemID, Bool full_dump) 
{
	u32 item_count = M4_GetMetaItemCount(mp4, 1, 0);
	if (item_count) {
		u32 i;
		for (i = 0; i < item_count; i++) {
			ItemInfoEntryAtom *item_entry = ChainGetEntry(mp4->meta->item_infos->item_infos, i);
			if (item_entry->item_ID == itemID) {
				fprintf(stdout, "Item #%d Info \n\tItemID %d - %s\n\tContentType %s ", i, itemID, item_entry->item_name, item_entry->content_type);
				if(item_entry->content_encoding){
					fprintf(stdout, "ContentEncoding %s\n", item_entry->content_encoding);
				} else {
					fprintf(stdout, "- no content encoding specified\n");
				}
			}
		}
		for (i = 0; i < item_count; i++) {
			ItemLocationEntry *item_location_entry = ChainGetEntry(mp4->meta->item_locations->location_entries, i);
			if (item_location_entry->item_ID == itemID) {
				u32 j;
				if (item_location_entry->data_reference_index) {
					fprintf(stdout, "Not stored in this file.\n");
				} else {
					u64 length = 0;
					for (j = 0; j < ChainGetCount(item_location_entry->extent_entries); j++) {
						ItemExtentEntry *entry = ChainGetEntry(item_location_entry->extent_entries, j);
						length += entry->extent_length;
					}
					fprintf(stdout, "\tItem length %ld bytes\n", length);
				}
			}
		}
	}
}

void DumpFileInfo(M4File *mp4)
{
	u32 brand, min;
	char sType[5];
	u32 item_count = M4_GetMetaItemCount(mp4, 1, 0);
	
	fprintf(stdout, "* ISO Metadata File Info *\n");

	if (M4_GetMovieVersionInfo(mp4, &brand, &min, NULL) == M4OK) {
		sType[4] = 0;
		FCC_TO_STR(brand, sType);
		fprintf(stdout, "\tFile Brand %s - version %d\n", sType, min);
	}

	switch(mp4->meta->handler->handlerType) {
	case MP21_File:
		fprintf(stdout, "\tThis is an MPEG-21 File.\n");
		break;
	default:
		fprintf(stdout, "\tUnknown handler type\n");
	}

	if (mp4->mdat) fprintf(stdout, "\tFile has mdat - ");
	else fprintf(stdout, "\tFile has no mdat - ");

	fprintf(stdout, "%d Item(s)\n", item_count);
	if (item_count) {
		u32 i;
		for (i = 0; i < item_count; i++) {
			ItemInfoEntryAtom *item_entry = ChainGetEntry(mp4->meta->item_infos->item_infos, i);
			DumpItemInfo(mp4, item_entry->item_ID, 1);
		}
	}

}
