CMpeg3AudioByteStream::CMpeg3AudioByteStream (mpeg2ps_t *file, int stream)
  : COurInByteStream("mpeg3 audio")
{
#ifdef OUTPUT_TO_FILE
  m_output_file = fopen("raw.mp3a", "w");
#endif
  m_file = file;
  m_stream = stream;
  m_eof = 0;
  m_freq = mpeg2ps_get_audio_stream_sample_freq(m_file, m_stream);
  m_max_time = mpeg2ps_get_max_time_msec(m_file);
  m_max_time /= 1000.0;
  mpeg3f_message(LOG_DEBUG, "audio max time is %g", m_max_time);
  m_changed_time = 0;
  m_frame_on = 0;
}
Esempio n. 2
0
static MP4TrackId AudioCreate (MP4FileHandle mp4file, 
			       mpeg2ps_t *file, 
			       int astream,
			       bool doEncrypt)
{
  uint16_t freq;
  int type;
  MP4TrackId id;
  uint16_t samples_per_frame;
  uint8_t *buf = NULL;
  uint32_t blen = 0;
  uint32_t frame_num = 1;
  ismacryp_session_id_t ismaCrypSId;
  mp4v2_ismacrypParams *icPp =  (mp4v2_ismacrypParams *) malloc(sizeof(mp4v2_ismacrypParams));
  MP4AV_Mp3Header hdr;
  u_int8_t mpegVersion;
  memset(icPp, 0, sizeof(mp4v2_ismacrypParams));

  type = mpeg2ps_get_audio_stream_type(file, astream);

  if (type != MPEG_AUDIO_MPEG) {
    fprintf(stderr, "Unsupported audio format %d in audio stream %d\n", 
	    type, astream);
    return MP4_INVALID_TRACK_ID;
  }

  freq = mpeg2ps_get_audio_stream_sample_freq(file, astream);

  if (mpeg2ps_get_audio_frame(file, 
			      astream,
			      &buf, 
			      &blen,
			      TS_90000,
			      NULL, 
			      NULL) == false) {
    fprintf(stderr, "No audio tracks in audio stream %d\n", astream);
    return MP4_INVALID_TRACK_ID;
  }
  
  hdr = MP4AV_Mp3HeaderFromBytes(buf);
  mpegVersion = MP4AV_Mp3GetHdrVersion(hdr);
  samples_per_frame = MP4AV_Mp3GetHdrSamplingWindow(hdr);

  u_int8_t audioType = MP4AV_Mp3ToMp4AudioType(mpegVersion);
  
  if (audioType == MP4_INVALID_AUDIO_TYPE
      || samples_per_frame == 0) {
    fprintf(stderr,	
	    "%s: data in file doesn't appear to be valid audio\n",
	    ProgName);
    return MP4_INVALID_TRACK_ID;
  }

  MP4Duration duration = (90000 * samples_per_frame) / freq;

  if (doEncrypt) {
    // initialize the ismacryp session
    if (ismacrypInitSession(&ismaCrypSId,KeyTypeAudio) != 0) {
      fprintf(stderr, 
	      "%s: could not initialize the ISMAcryp session\n",
	      ProgName);
      return MP4_INVALID_TRACK_ID;
    }
    if (ismacrypGetScheme(ismaCrypSId, &(icPp->scheme_type)) != ismacryp_rc_ok) {
       fprintf(stderr, "%s: could not get ismacryp scheme type. sid %d\n", 
               ProgName, ismaCrypSId);
       ismacrypEndSession(ismaCrypSId);
       return MP4_INVALID_TRACK_ID;
    }
    if (ismacrypGetSchemeVersion(ismaCrypSId, &(icPp->scheme_version)) != ismacryp_rc_ok) {
       fprintf(stderr, "%s: could not get ismacryp scheme ver. sid %d\n",
               ProgName, ismaCrypSId);
       ismacrypEndSession(ismaCrypSId);
       return MP4_INVALID_TRACK_ID;
    }
    if (ismacrypGetKMSUri(ismaCrypSId, &(icPp->kms_uri)) != ismacryp_rc_ok) {
       fprintf(stderr, "%s: could not get ismacryp kms uri. sid %d\n",
               ProgName, ismaCrypSId);
       if (icPp->kms_uri != NULL) free(icPp->kms_uri);
       ismacrypEndSession(ismaCrypSId);
       return MP4_INVALID_TRACK_ID;
    }
    if ( ismacrypGetSelectiveEncryption(ismaCrypSId, &(icPp->selective_enc)) != ismacryp_rc_ok ) {
       fprintf(stderr, "%s: could not get ismacryp selec enc. sid %d\n",
               ProgName, ismaCrypSId);
       ismacrypEndSession(ismaCrypSId);
       return MP4_INVALID_TRACK_ID;
    }
    if (ismacrypGetKeyIndicatorLength(ismaCrypSId, &(icPp->key_ind_len)) != ismacryp_rc_ok) {
       fprintf(stderr, "%s: could not get ismacryp key ind len. sid %d\n",
               ProgName, ismaCrypSId);
       ismacrypEndSession(ismaCrypSId);
       return MP4_INVALID_TRACK_ID;
    }
    if (ismacrypGetIVLength(ismaCrypSId, &(icPp->iv_len)) != ismacryp_rc_ok) {
       fprintf(stderr, "%s: could not get ismacryp iv len. sid %d\n",
               ProgName, ismaCrypSId);
       ismacrypEndSession(ismaCrypSId);
       return MP4_INVALID_TRACK_ID;
    }
    id = MP4AddEncAudioTrack(mp4file, 
			     90000, 
			     duration,
                             icPp,
			     audioType);
  } else {
    id = MP4AddAudioTrack(mp4file, 
			  90000, 
			  duration,
			  audioType);
  }
  
  if (id == MP4_INVALID_TRACK_ID) {
    fprintf(stderr, 
	    "%s: can't create audio track from stream %d\n", 
	    ProgName, astream);
    return MP4_INVALID_TRACK_ID;
  }

  if (MP4GetNumberOfTracks(mp4file, MP4_AUDIO_TRACK_TYPE) == 1) {
    MP4SetAudioProfileLevel(mp4file, 0xFE);
  }

  do {
    // encrypt if needed
     if (doEncrypt) {
       u_int8_t* encSampleData = NULL;
       u_int32_t encSampleLen = 0;
       if (ismacrypEncryptSampleAddHeader(ismaCrypSId, blen, buf,
					  &encSampleLen, &encSampleData) != 0) {
	 fprintf(stderr,	
		 "%s: can't encrypt audio sample and add header %u\n", ProgName, id);
       }
       // now write the sample
       if (!MP4WriteSample(mp4file, id, encSampleData, encSampleLen)) {
	 fprintf(stderr, "%s: can't write audio track %u, stream %d",
		 ProgName, frame_num, astream);
	 MP4DeleteTrack(mp4file, id);
	 return MP4_INVALID_TRACK_ID;
       }
       if (encSampleData != NULL) {
	 free(encSampleData);
       }
    }
     // now write the sample
    if (!MP4WriteSample(mp4file, id, buf, blen)) {
      fprintf(stderr, "%s: can't write audio track %u, stream %d",
	      ProgName, frame_num, astream);
      MP4DeleteTrack(mp4file, id);
      return MP4_INVALID_TRACK_ID;
    }
    frame_num++;
#if 0
    if ((frame_num % 100) == 0) printf("Audio frame %d\n", frame_num);
#endif
  }  while (mpeg2ps_get_audio_frame(file, 
				    astream, 
				    &buf, 
				    &blen,
				    TS_90000,
				    NULL, NULL));
  
  // if encrypting, terminate the ismacryp session
  if (doEncrypt) {
    if (ismacrypEndSession(ismaCrypSId) != 0) {
      fprintf(stderr, 
	      "%s: could not end the ISMAcryp session\n",
	      ProgName);
      return MP4_INVALID_TRACK_ID;
    }
  }

  return id;
}
Esempio n. 3
0
int create_media_for_mpeg_file (CPlayerSession *psptr,
                                const char *name,
                                int have_audio_driver,
                                control_callback_vft_t *cc_vft)
{
    mpeg2ps_t *file;
    int video_streams, audio_streams;
    int video_cnt, audio_cnt;
    int ix;
    codec_plugin_t *plugin;
    int video_offset, audio_offset;
    int ret;
    int sdesc;

    file = mpeg2ps_init(name);
    if (file == NULL) {
        psptr->set_message("file %s is not a valid .mpg file",
                           name);
        return -1;
    }

    psptr->set_media_close_callback(close_mpeg3_file, (void *)file);
    video_streams = mpeg2ps_get_video_stream_count(file);
    audio_streams = mpeg2ps_get_audio_stream_count(file);

    video_cnt = 0;
    if (video_streams > 0) {
        plugin = check_for_video_codec(STREAM_TYPE_MPEG_FILE,
                                       "mp2v",
                                       NULL,
                                       mpeg2ps_get_video_stream_type(file, 0),
                                       -1,
                                       NULL,
                                       0,
                                       &config);
        if (plugin != NULL) video_cnt = video_streams;
    }

    for (ix = 0, audio_cnt = 0; ix < audio_streams; ix++) {
        plugin = check_for_audio_codec(STREAM_TYPE_MPEG_FILE,
                                       NULL,
                                       NULL,
                                       mpeg2ps_get_audio_stream_type(file, ix),
                                       -1,
                                       NULL,
                                       0,
                                       &config);
        if (plugin != NULL) audio_cnt++;
    }

    video_query_t *vq;
    audio_query_t *aq;

    if (video_cnt > 0) {
        vq = (video_query_t *)malloc(sizeof(video_query_t) * video_cnt);
    } else {
        vq = NULL;
    }
    if (have_audio_driver && audio_cnt > 0) {
        aq = (audio_query_t *)malloc(sizeof(audio_query_t) * audio_cnt);
    } else {
        aq = NULL;
    }
    video_offset = 0;
    for (ix = 0; ix < video_cnt; ix++) {
        vq[video_offset].track_id = ix;
        vq[video_offset].stream_type = STREAM_TYPE_MPEG_FILE;
        vq[video_offset].compressor = "mp2v";
        vq[video_offset].type = mpeg2ps_get_video_stream_type(file, ix);
        vq[video_offset].profile = -1;
        vq[video_offset].fptr = NULL;
        vq[video_offset].h = mpeg2ps_get_video_stream_height(file, ix);
        vq[video_offset].w = mpeg2ps_get_video_stream_width(file, ix);
        vq[video_offset].frame_rate = mpeg2ps_get_video_stream_framerate(file, ix);
        vq[video_offset].config = NULL;
        vq[video_offset].config_len = 0;
        vq[video_offset].enabled = 0;
        vq[video_offset].reference = NULL;
        video_offset++;
    }
    audio_offset = 0;
    if (have_audio_driver) {
        for (ix = 0; ix < audio_streams; ix++) {
            plugin = check_for_audio_codec(STREAM_TYPE_MPEG_FILE,
                                           NULL,
                                           NULL,
                                           mpeg2ps_get_audio_stream_type(file, ix),
                                           -1,
                                           NULL,
                                           0,
                                           &config);
            if (plugin != NULL) {
                aq[audio_offset].track_id = ix;
                aq[audio_offset].stream_type = STREAM_TYPE_MPEG_FILE;
                aq[audio_offset].compressor = NULL;
                aq[audio_offset].type = mpeg2ps_get_audio_stream_type(file, ix);
                aq[audio_offset].profile = -1;
                aq[audio_offset].fptr = NULL;
                aq[audio_offset].config = NULL;
                aq[audio_offset].config_len = 0;
                aq[audio_offset].sampling_freq =
                    mpeg2ps_get_audio_stream_sample_freq(file, ix);
                aq[audio_offset].chans = mpeg2ps_get_audio_stream_channels(file, ix);
                aq[audio_offset].enabled = 0;
                aq[audio_offset].reference = NULL;
                audio_offset++;
            } else {
                mpeg3f_message(LOG_ERR, "Unsupported audio type %s in track %d",
                               mpeg2ps_get_audio_stream_name(file, ix), ix);
            }
        }
    }

    if (audio_offset == 0 && video_offset == 0) {
        psptr->set_message("No playable streams in file");
        CHECK_AND_FREE(aq);
        CHECK_AND_FREE(vq);
        return -1;
    }
    if (cc_vft && cc_vft->media_list_query != NULL) {
        (cc_vft->media_list_query)(psptr, video_offset, vq, audio_offset, aq,
                                   0, NULL);
    } else {
        if (video_offset > 0) vq[0].enabled = 1;
        if (audio_offset > 0) aq[0].enabled = 1;
    }

    ret = 0;
    sdesc = 1;
    for (ix = 0; ret >= 0 && ix < video_offset; ix++) {
        if (vq[ix].enabled) {
            ret = create_mpeg3_video(&vq[ix], file, psptr, sdesc);
            if (ret <= 0) {
            }
        }
    }
    if (ret >= 0) {
        for (ix = 0; ix < audio_offset && ret >= 0; ix++) {
            if (aq[ix].enabled) {
                ret = create_mpeg3_audio(&aq[ix], file, psptr, sdesc);
                if (ret <= 0) {
                }
            }
        }
    }

    free(vq);
    free(aq);
    if (ret < 0) {
        mpeg2ps_close(file);
        return ret;
    }
    psptr->session_set_seekable(1);
    return 0;
}
Esempio n. 4
0
static GtkWidget* CreateMpeg2TrackMenu(
	GtkWidget* menu,
	char type,
	const char* source,
	u_int32_t* pIndex,
	u_int32_t* pNumber,
	u_int32_t** ppValues)
{
	*pIndex = 0;

	u_int32_t newTrackNumber = 1;

	mpeg2ps_t* mpeg2File = mpeg2ps_init(source);

	if (mpeg2File) {
		if (type == 'V') {
		  newTrackNumber = mpeg2ps_get_video_stream_count(mpeg2File);
		} else {
		  newTrackNumber = mpeg2ps_get_audio_stream_count(mpeg2File);
		}
	}

	u_int32_t* newTrackValues = 
		(u_int32_t*)malloc(sizeof(u_int32_t) * newTrackNumber);

	char** newTrackNames = 
		(char**)malloc(sizeof(char*) * newTrackNumber);

	if (!mpeg2File) {
		newTrackValues[0] = 0;
		newTrackNames[0] = strdup("");
	} else {
		for (u_int8_t i = 0; i < newTrackNumber; i++) {
			newTrackValues[i] = i;

			char buf[64];
			if (type == 'V') {
			  snprintf(buf, sizeof(buf), 
				   "%u - %u x %u @ %.2f fps", 
				   i + 1,
				   mpeg2ps_get_video_stream_width(mpeg2File, i),
				   mpeg2ps_get_video_stream_height(mpeg2File, i),
				   mpeg2ps_get_video_stream_framerate(mpeg2File, i));
			} else {
			  const char* afmt =
			    mpeg2ps_get_audio_stream_name(mpeg2File, i);
			  
			  // use more familar though less accurate name
			  snprintf(buf, sizeof(buf), 
				   "%u - %s  %u channels @ %u Hz", 
				   i + 1,
				   afmt,
				   mpeg2ps_get_audio_stream_channels(mpeg2File, i),
				   mpeg2ps_get_audio_stream_sample_freq(mpeg2File, i));
			}
			newTrackNames[i] = strdup(buf);
		}
		mpeg2ps_close(mpeg2File);
	}

	// (re)create the menu
	menu = CreateOptionMenu(
		menu,
		newTrackNames, 
		newTrackNumber,
		*pIndex,
		GTK_SIGNAL_FUNC(on_track_menu_activate));

	// free up old names
	for (u_int8_t i = 0; i < *pNumber; i++) {
		free(trackNames[i]);
	}
	free(trackNames);
	free(*ppValues);

	*pNumber = newTrackNumber;
	trackNames = newTrackNames;
	*ppValues = newTrackValues;
	return menu;
}