/*****************************************************************
 * gmerlin - a general purpose multimedia framework and applications
 *
 * Copyright (c) 2001 - 2012 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.net
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/

#include <string.h>


#include "gavf-decode.h"

#define LOG_DOMAIN "gavf-decode.load"

int load_input_file(const char * file,
                    bg_mediaconnector_t * conn,
                    bg_control_t * ctrl,
                    bg_plugin_handle_t ** hp, 
                    gavl_dictionary_t * m,
                    int force_decompress)
  {
  gavl_dictionary_t * track_info;
  int i;
  int num_audio_streams;
  int num_video_streams;
  int num_text_streams;
  int num_overlay_streams; 
  int num_msg_streams; 
  
  if(!bg_input_plugin_load_full(plugin_reg, file, hp, ctrl))
    {
    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Couldn't load %s", file);
    goto fail;
    }
  
  track_info = bg_input_plugin_get_track_info(*hp, bg_url_get_track(file));

  num_audio_streams   = gavl_track_get_num_audio_streams(track_info);
  num_video_streams   = gavl_track_get_num_video_streams(track_info);
  num_text_streams    = gavl_track_get_num_text_streams(track_info);
  num_overlay_streams = gavl_track_get_num_overlay_streams(track_info);
  num_msg_streams     = gavl_track_get_num_msg_streams(track_info);
  
  /* Extract metadata */
  gavl_dictionary_free(m);
  gavl_dictionary_init(m);
  gavl_dictionary_copy(m, gavl_track_get_metadata(track_info));

  if(!gavl_dictionary_get_string(m, GAVL_META_LABEL))
    {
    if(strcmp(file, "-"))
      {
      gavl_dictionary_set_string_nocopy(m, GAVL_META_LABEL, bg_get_track_name_default(file));
      }
    }
  
  /* Select A/V streams */
  if(!audio_actions)  
    audio_actions = gavftools_get_stream_actions(num_audio_streams,
                                                 GAVL_STREAM_AUDIO);
  if(!video_actions)
    video_actions = gavftools_get_stream_actions(num_video_streams,
                                                 GAVL_STREAM_VIDEO);
  if(!text_actions)
    text_actions = gavftools_get_stream_actions(num_text_streams,
                                                GAVL_STREAM_TEXT);
  if(!overlay_actions)
    overlay_actions = gavftools_get_stream_actions(num_overlay_streams,
                                                   GAVL_STREAM_OVERLAY);

  /* Check for compressed reading */

  for(i = 0; i < num_audio_streams; i++)
    {
    if((audio_actions[i] == BG_STREAM_ACTION_READRAW) &&
       (force_decompress || !gavl_track_get_audio_compression_info(track_info, i, NULL)))
      {
      audio_actions[i] = BG_STREAM_ACTION_DECODE;
      gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN, "Audio stream %d cannot be read compressed",
             i+1);
      }
    }

  for(i = 0; i < num_video_streams; i++)
    {
    if((video_actions[i] == BG_STREAM_ACTION_READRAW) &&
       (force_decompress ||
        !gavl_track_get_video_compression_info(track_info, i, NULL)))
      {
      video_actions[i] = BG_STREAM_ACTION_DECODE;
      gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN, "Video stream %d cannot be read compressed",
             i+1);
      }
    }

  for(i = 0; i < num_overlay_streams; i++)
    {
    if((overlay_actions[i] == BG_STREAM_ACTION_READRAW) &&
       (force_decompress || 
        !gavl_track_get_overlay_compression_info(track_info, i, NULL)))
      {
      overlay_actions[i] = BG_STREAM_ACTION_DECODE;
      gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN, "Overlay stream %d cannot be read compressed",
             i+1);
      }
    }

  /* Enable streams */
  
  for(i = 0; i < num_audio_streams; i++)
    bg_media_source_set_audio_action((*hp)->src, i, audio_actions[i]);
  for(i = 0; i < num_video_streams; i++)
    bg_media_source_set_video_action((*hp)->src, i, video_actions[i]);
  for(i = 0; i < num_text_streams; i++)
    bg_media_source_set_text_action((*hp)->src, i, text_actions[i]);
  for(i = 0; i < num_overlay_streams; i++)
    bg_media_source_set_overlay_action((*hp)->src, i, overlay_actions[i]);

  if(num_msg_streams)
    bg_media_source_set_msg_action_by_id((*hp)->src, GAVL_META_STREAM_ID_MSG_PROGRAM, BG_STREAM_ACTION_DECODE);
  
  /* Start plugin */
  bg_input_plugin_start(*hp);
  bg_mediaconnector_set_from_source(conn, (*hp)->src);
  
  return 1;
  fail:
  
  return 0;
  }
