Exemplo n.º 1
0
Arquivo: vcd.c Projeto: CSRedRat/vlc
/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
{
    access_sys_t *p_sys = p_access->p_sys;
    input_title_t ***ppp_title;
    int i;

    switch( i_query )
    {
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
            *va_arg( args, bool* ) = true;
            break;

        /* */
        case ACCESS_GET_PTS_DELAY:
            *va_arg( args, int64_t * ) = INT64_C(1000)
                * var_InheritInteger(p_access, "disc-caching");
            break;

        /* */
        case ACCESS_SET_PAUSE_STATE:
            break;

        case ACCESS_GET_TITLE_INFO:
            ppp_title = va_arg( args, input_title_t*** );
            *va_arg( args, int* ) = p_sys->i_titles;

            /* Duplicate title infos */
            *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
            for( i = 0; i < p_sys->i_titles; i++ )
            {
                (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
            }
            break;

        case ACCESS_SET_TITLE:
            i = va_arg( args, int );
            if( i != p_access->info.i_title )
            {
                /* Update info */
                p_access->info.i_update |=
                  INPUT_UPDATE_TITLE|INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_SIZE;
                p_access->info.i_title = i;
                p_access->info.i_seekpoint = 0;
                p_access->info.i_size = p_sys->title[i]->i_size;
                p_access->info.i_pos  = 0;

                /* Next sector to read */
                p_sys->i_sector = p_sys->p_sectors[1+i];
            }
            break;

        case ACCESS_SET_SEEKPOINT:
        {
            input_title_t *t = p_sys->title[p_access->info.i_title];
            i = va_arg( args, int );
            if( t->i_seekpoint > 0 )
            {
                p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
                p_access->info.i_seekpoint = i;

                p_sys->i_sector = p_sys->p_sectors[1+p_access->info.i_title] +
                    t->seekpoint[i]->i_byte_offset / VCD_DATA_SIZE;

                p_access->info.i_pos = (uint64_t)(p_sys->i_sector -
                    p_sys->p_sectors[1+p_access->info.i_title]) *VCD_DATA_SIZE;
            }
            return VLC_SUCCESS;
        }

        case ACCESS_SET_PRIVATE_ID_STATE:
        case ACCESS_GET_CONTENT_TYPE:
            return VLC_EGENERIC;

        default:
            msg_Warn( p_access, "unimplemented query in control" );
            return VLC_EGENERIC;

    }
    return VLC_SUCCESS;
}
Exemplo n.º 2
0
Arquivo: access.c Projeto: BtbN/vlc
/*****************************************************************************
  VCDRead: reads VCD_BLOCKS_ONCE from the VCD and returns that.
  NULL is returned if something went wrong.
 *****************************************************************************/
static block_t *
VCDReadBlock( access_t * p_access )
{
    vcdplayer_t *p_vcdplayer= (vcdplayer_t *)p_access->p_sys;
    const int    i_blocks   = p_vcdplayer->i_blocks_per_read;
    block_t     *p_block;
    int          i_read;
    uint8_t     *p_buf;

    dbg_print( (INPUT_DBG_LSN), "lsn: %lu",
               (long unsigned int) p_vcdplayer->i_lsn );

    /* Allocate a block for the reading */
    if( !( p_block = block_Alloc( i_blocks * M2F2_SECTOR_SIZE ) ) )
    {
        msg_Err( p_access, "cannot get a new block of size: %i",
                 i_blocks * M2F2_SECTOR_SIZE );
        block_Release( p_block );
        return NULL;
    }

    p_buf = (uint8_t *) p_block->p_buffer;
    for ( i_read = 0 ; i_read < i_blocks ; i_read++ )
    {
        vcdplayer_read_status_t read_status = vcdplayer_read(p_access, p_buf);

        p_access->info.i_pos += M2F2_SECTOR_SIZE;

        switch ( read_status ) {
        case READ_END:
            /* End reached. Return NULL to indicated this. */
            /* We also set the postion to the end so the higher level
               (demux?) doesn't try to keep reading. If everything works out
               right this shouldn't have to happen.
             */
#if 0
            if( p_access->info.i_pos != p_access->info.i_size ) {
                msg_Warn( p_access,
                    "At end but pos (%llu) is not size (%llu). Adjusting.",
                    p_access->info.i_pos, p_access->info.i_size );
                p_access->info.i_pos = p_access->info.i_size;
            }
#endif

            block_Release( p_block );
            return NULL;

        case READ_ERROR:
            /* Some sort of error. Should we increment lsn? to skip block? */
            block_Release( p_block );
            return NULL;
        case READ_STILL_FRAME:
          /* FIXME The below should be done in an event thread.
             Until then...
           */
#if 1
            msleep( INT64_C(1000) * *p_buf );
            VCDSetOrigin(p_access, p_vcdplayer->origin_lsn,
                         p_vcdplayer->i_track, &(p_vcdplayer->play_item));
            // p_vcd->in_still = false;
            dbg_print(INPUT_DBG_STILL, "still wait time done");
#endif

            block_Release( p_block );
            return NULL;

        default:
        case READ_BLOCK:
            /* Read buffer */
            break;
        }

        p_buf += M2F2_SECTOR_SIZE;
        /* Update seekpoint */
        if ( VCDINFO_ITEM_TYPE_ENTRY == p_vcdplayer->play_item.type )
        {
            size_t i_entry = p_vcdplayer->play_item.num+1;
            lsn_t  i_lsn   = vcdinfo_get_entry_lsn(p_vcdplayer->vcd, i_entry);
            if ( p_vcdplayer->i_lsn >= i_lsn && i_lsn != VCDINFO_NULL_LSN )
            {
                dbg_print( (INPUT_DBG_LSN|INPUT_DBG_PBC),
                           "entry change to %zu, current LSN %u >= end %u",
                           i_entry, p_vcdplayer->i_lsn, i_lsn);

                p_vcdplayer->play_item.num = i_entry;

                VCDSetOrigin( p_access,  i_lsn, p_vcdplayer->i_track,
                              &(p_vcdplayer->play_item) );
            }
        }
    }

    return p_block;
}
Exemplo n.º 3
0
/*****************************************************************************
 * sout_MuxSendBuffer:
 *****************************************************************************/
int sout_MuxSendBuffer( sout_mux_t *p_mux, sout_input_t *p_input,
                         block_t *p_buffer )
{
    block_FifoPut( p_input->p_fifo, p_buffer );

    if( p_mux->p_sout->i_out_pace_nocontrol )
    {
        mtime_t current_date = mdate();
        if ( current_date > p_buffer->i_dts )
            msg_Warn( p_mux, "late buffer for mux input (%"PRId64")",
                      current_date - p_buffer->i_dts );
    }

    if( p_mux->b_waiting_stream )
    {
        const int64_t i_caching = var_GetInteger( p_mux->p_sout, "sout-mux-caching" ) * INT64_C(1000);

        if( p_mux->i_add_stream_start < 0 )
            p_mux->i_add_stream_start = p_buffer->i_dts;

        /* Wait until we have enought data before muxing */
        if( p_mux->i_add_stream_start < 0 ||
            p_buffer->i_dts < p_mux->i_add_stream_start + i_caching )
            return VLC_SUCCESS;
        p_mux->b_waiting_stream = false;
    }
    return p_mux->pf_mux( p_mux );
}
Exemplo n.º 4
0
/**
 * Grab a frame from x11 (public device demuxer API).
 *
 * @param s1 Context from avformat core
 * @param pkt Packet holding the brabbed frame
 * @return frame size in bytes
 */
static int
x11grab_read_packet(AVFormatContext *s1, AVPacket *pkt)
{
    struct x11_grab *s = s1->priv_data;
    Display *dpy = s->dpy;
    XImage *image = s->image;
    int x_off = s->x_off;
    int y_off = s->y_off;

    int screen;
    Window root;
    int follow_mouse = s->follow_mouse;

    int64_t curtime, delay;
    struct timespec ts;

    /* Calculate the time of the next frame */
    s->time_frame += INT64_C(1000000);

    /* wait based on the frame rate */
    for(;;) {
        curtime = av_gettime();
        delay = s->time_frame * av_q2d(s->time_base) - curtime;
        if (delay <= 0) {
            if (delay < INT64_C(-1000000) * av_q2d(s->time_base)) {
                s->time_frame += INT64_C(1000000);
            }
            break;
        }
        ts.tv_sec = delay / 1000000;
        ts.tv_nsec = (delay % 1000000) * 1000;
        nanosleep(&ts, NULL);
    }

    av_init_packet(pkt);
    pkt->data = image->data;
    pkt->size = s->frame_size;
    pkt->pts = curtime;

    screen = DefaultScreen(dpy);
    root = RootWindow(dpy, screen);
    if (follow_mouse) {
        int screen_w, screen_h;
        int pointer_x, pointer_y, _;
        Window w;

        screen_w = DisplayWidth(dpy, screen);
        screen_h = DisplayHeight(dpy, screen);
        XQueryPointer(dpy, root, &w, &w, &pointer_x, &pointer_y, &_, &_, &_);
        if (follow_mouse == -1) {
            // follow the mouse, put it at center of grabbing region
            x_off += pointer_x - s->width  / 2 - x_off;
            y_off += pointer_y - s->height / 2 - y_off;
        } else {
            // follow the mouse, but only move the grabbing region when mouse
            // reaches within certain pixels to the edge.
            if (pointer_x > x_off + s->width - follow_mouse) {
                x_off += pointer_x - (x_off + s->width - follow_mouse);
            } else if (pointer_x < x_off + follow_mouse)
                x_off -= (x_off + follow_mouse) - pointer_x;
            if (pointer_y > y_off + s->height - follow_mouse) {
                y_off += pointer_y - (y_off + s->height - follow_mouse);
            } else if (pointer_y < y_off + follow_mouse)
                y_off -= (y_off + follow_mouse) - pointer_y;
        }
        // adjust grabbing region position if it goes out of screen.
        s->x_off = x_off = FFMIN(FFMAX(x_off, 0), screen_w - s->width);
        s->y_off = y_off = FFMIN(FFMAX(y_off, 0), screen_h - s->height);

        if (s->show_region && s->region_win)
            XMoveWindow(dpy, s->region_win,
                        s->x_off - REGION_WIN_BORDER,
                        s->y_off - REGION_WIN_BORDER);
    }

    if (s->show_region) {
        if (s->region_win) {
            XEvent evt;
            // clean up the events, and do the initinal draw or redraw.
            for (evt.type = NoEventMask; XCheckMaskEvent(dpy, ExposureMask | StructureNotifyMask, &evt); );
            if (evt.type)
                x11grab_draw_region_win(s);
        } else {
            x11grab_region_win_init(s);
        }
    }

    if(s->use_shm) {
        if (!XShmGetImage(dpy, root, image, x_off, y_off, AllPlanes)) {
            av_log (s1, AV_LOG_INFO, "XShmGetImage() failed\n");
        }
    } else {
        if (!xget_zpixmap(dpy, root, image, x_off, y_off)) {
            av_log (s1, AV_LOG_INFO, "XGetZPixmap() failed\n");
        }
    }

    if (s->draw_mouse) {
        paint_mouse_pointer(image, s);
    }

    return s->frame_size;
}
Exemplo n.º 5
0
Arquivo: vdr.c Projeto: Aakash-729/vlc
/*****************************************************************************
 * Control input stream
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
{
    access_sys_t *p_sys = p_access->p_sys;
    input_title_t ***ppp_title;
    int i;
    int64_t *pi64;
    vlc_meta_t *p_meta;

    switch( i_query )
    {
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
            *va_arg( args, bool* ) = true;
            break;

        case ACCESS_GET_SIZE:
            *va_arg( args, uint64_t* ) = p_sys->size;
            break;

        case ACCESS_GET_PTS_DELAY:
            pi64 = va_arg( args, int64_t * );
            *pi64 = INT64_C(1000)
                  * var_InheritInteger( p_access, "file-caching" );
            break;

        case ACCESS_SET_PAUSE_STATE:
            /* nothing to do */
            break;

        case ACCESS_GET_TITLE_INFO:
            /* return a copy of our seek points */
            if( !p_sys->p_marks )
                return VLC_EGENERIC;
            ppp_title = va_arg( args, input_title_t*** );
            *va_arg( args, int* ) = 1;
            *ppp_title = malloc( sizeof( **ppp_title ) );
            if( !*ppp_title )
                return VLC_ENOMEM;
            **ppp_title = vlc_input_title_Duplicate( p_sys->p_marks );
            break;

        case ACCESS_GET_TITLE:
            *va_arg( args, unsigned * ) = 0;
            break;

        case ACCESS_GET_SEEKPOINT:
            *va_arg( args, unsigned * ) = p_sys->cur_seekpoint;
            break;

        case ACCESS_SET_TITLE:
            /* ignore - only one title */
            break;

        case ACCESS_SET_SEEKPOINT:
            i = va_arg( args, int );
            return Seek( p_access, p_sys->p_marks->seekpoint[i]->i_byte_offset );

        case ACCESS_GET_META:
            if( !p_sys->p_meta )
                return VLC_EGENERIC;
            p_meta = va_arg( args, vlc_meta_t* );
            vlc_meta_Merge( p_meta, p_sys->p_meta );
            break;

        default:
            return VLC_EGENERIC;
    }
    return VLC_SUCCESS;
}
Exemplo n.º 6
0
// Load data from constant file and check for exact match.
TEST(bits, reverseBytes)
{
	{
		uint8_t val = 0x88, val_r;
		const uint8_t val_r_ok = 0x88;

		mrpt::reverseBytes(val, val_r);
		EXPECT_EQ(val_r, val_r_ok);
		mrpt::reverseBytesInPlace(val);
		EXPECT_EQ(val, val_r_ok);
	}
	{
		int8_t val = 0x66, val_r;
		const int8_t val_r_ok = 0x66;

		mrpt::reverseBytes(val, val_r);
		EXPECT_EQ(val_r, val_r_ok);
		mrpt::reverseBytesInPlace(val);
		EXPECT_EQ(val, val_r_ok);
	}
	{
		uint16_t val = 0x1122, val_r;
		const uint16_t val_r_ok = 0x2211;

		mrpt::reverseBytes(val, val_r);
		EXPECT_EQ(val_r, val_r_ok);
		mrpt::reverseBytesInPlace(val);
		EXPECT_EQ(val, val_r_ok);
	}
	{
		int16_t val = 0x1122, val_r;
		const int16_t val_r_ok = 0x2211;

		mrpt::reverseBytes(val, val_r);
		EXPECT_EQ(val_r, val_r_ok);
		mrpt::reverseBytesInPlace(val);
		EXPECT_EQ(val, val_r_ok);
	}
	{
		uint32_t val = UINT32_C(0x11223344), val_r;
		const uint32_t val_r_ok = UINT32_C(0x44332211);

		mrpt::reverseBytes(val, val_r);
		EXPECT_EQ(val_r, val_r_ok);
		mrpt::reverseBytesInPlace(val);
		EXPECT_EQ(val, val_r_ok);
	}
	{
		int32_t val = INT32_C(0x11223344), val_r;
		const int32_t val_r_ok = INT32_C(0x44332211);

		mrpt::reverseBytes(val, val_r);
		EXPECT_EQ(val_r, val_r_ok);
		mrpt::reverseBytesInPlace(val);
		EXPECT_EQ(val, val_r_ok);
	}
	{
		uint64_t val = UINT64_C(0x1122334455667788), val_r;
		const uint64_t val_r_ok = UINT64_C(0x8877665544332211);

		mrpt::reverseBytes(val, val_r);
		EXPECT_EQ(val_r, val_r_ok);
		mrpt::reverseBytesInPlace(val);
		EXPECT_EQ(val, val_r_ok);
	}
	{
		int64_t val = INT64_C(0x1122334455667766), val_r;
		const int64_t val_r_ok = INT64_C(0x6677665544332211);

		mrpt::reverseBytes(val, val_r);
		EXPECT_EQ(val_r, val_r_ok);
		mrpt::reverseBytesInPlace(val);
		EXPECT_EQ(val, val_r_ok);
	}
	{
		bool valTrue  = true;
		bool valFalse = false;
		mrpt::reverseBytesInPlace(valTrue);
		mrpt::reverseBytesInPlace(valFalse);
		EXPECT_TRUE(valTrue);
		EXPECT_FALSE(valFalse);
	}
	{
		uint8_t val = UINT8_C(0xc1);

		mrpt::reverseBytesInPlace(val);
		EXPECT_EQ(val, 0xc1);
	}
	{
		//1.0 == 0x3F800000
		float val = 1;
		const uint32_t val_r_ok = UINT32_C(0x803f);

		mrpt::reverseBytesInPlace(val);
		uint32_t val_check = *reinterpret_cast<uint32_t*>(&val);
		EXPECT_EQ(val_check, val_r_ok);
	}
	{
		//1.0 == 0x3ff0000000000000
		double val = 1;
		const uint64_t val_r_ok = UINT32_C(0xf03f);

		mrpt::reverseBytesInPlace(val);
		uint32_t val_check = *reinterpret_cast<uint32_t*>(&val);
		EXPECT_EQ(val_check, val_r_ok);
	}
}
Exemplo n.º 7
0
static int avi_write_header(AVFormatContext *s)
{
    AVIContext *avi = s->priv_data;
    AVIOContext *pb = s->pb;
    int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
    AVCodecContext *stream, *video_enc;
    int64_t list1, list2, strh, strf;
    AVDictionaryEntry *t = NULL;

    if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
        av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
               AVI_MAX_STREAM_COUNT);
        return -1;
    }

    for(n=0;n<s->nb_streams;n++) {
        s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
        if(!s->streams[n]->priv_data)
            return AVERROR(ENOMEM);
    }

    /* header list */
    avi->riff_id = 0;
    list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");

    /* avi header */
    ffio_wfourcc(pb, "avih");
    avio_wl32(pb, 14 * 4);
    bitrate = 0;

    video_enc = NULL;
    for(n=0;n<s->nb_streams;n++) {
        stream = s->streams[n]->codec;
        bitrate += stream->bit_rate;
        if (stream->codec_type == AVMEDIA_TYPE_VIDEO)
            video_enc = stream;
    }

    nb_frames = 0;

    if(video_enc){
        avio_wl32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
    } else {
        avio_wl32(pb, 0);
    }
    avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
    avio_wl32(pb, 0); /* padding */
    if (!pb->seekable)
        avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
    else
        avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
    avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
    avio_wl32(pb, nb_frames); /* nb frames, filled later */
    avio_wl32(pb, 0); /* initial frame */
    avio_wl32(pb, s->nb_streams); /* nb streams */
    avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
    if(video_enc){
        avio_wl32(pb, video_enc->width);
        avio_wl32(pb, video_enc->height);
    } else {
        avio_wl32(pb, 0);
        avio_wl32(pb, 0);
    }
    avio_wl32(pb, 0); /* reserved */
    avio_wl32(pb, 0); /* reserved */
    avio_wl32(pb, 0); /* reserved */
    avio_wl32(pb, 0); /* reserved */

    /* stream list */
    for(i=0;i<n;i++) {
        AVIStream *avist= s->streams[i]->priv_data;
        list2 = ff_start_tag(pb, "LIST");
        ffio_wfourcc(pb, "strl");

        stream = s->streams[i]->codec;

        /* stream generic header */
        strh = ff_start_tag(pb, "strh");
        switch(stream->codec_type) {
        case AVMEDIA_TYPE_SUBTITLE:
            // XSUB subtitles behave like video tracks, other subtitles
            // are not (yet) supported.
            if (stream->codec_id != CODEC_ID_XSUB) {
                av_log(s, AV_LOG_ERROR, "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
                return AVERROR_PATCHWELCOME;
            }
        case AVMEDIA_TYPE_VIDEO: ffio_wfourcc(pb, "vids"); break;
        case AVMEDIA_TYPE_AUDIO: ffio_wfourcc(pb, "auds"); break;
//      case AVMEDIA_TYPE_TEXT : ffio_wfourcc(pb, "txts"); break;
        case AVMEDIA_TYPE_DATA : ffio_wfourcc(pb, "dats"); break;
        }
        if(stream->codec_type == AVMEDIA_TYPE_VIDEO ||
           stream->codec_id == CODEC_ID_XSUB)
            avio_wl32(pb, stream->codec_tag);
        else
            avio_wl32(pb, 1);
        avio_wl32(pb, 0); /* flags */
        avio_wl16(pb, 0); /* priority */
        avio_wl16(pb, 0); /* language */
        avio_wl32(pb, 0); /* initial frame */

        ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);

        avio_wl32(pb, au_scale); /* scale */
        avio_wl32(pb, au_byterate); /* rate */
        av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);

        avio_wl32(pb, 0); /* start */
        avist->frames_hdr_strm = avio_tell(pb); /* remember this offset to fill later */
        if (!pb->seekable)
            avio_wl32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
        else
            avio_wl32(pb, 0); /* length, XXX: filled later */

        /* suggested buffer size */ //FIXME set at the end to largest chunk
        if(stream->codec_type == AVMEDIA_TYPE_VIDEO)
            avio_wl32(pb, 1024 * 1024);
        else if(stream->codec_type == AVMEDIA_TYPE_AUDIO)
            avio_wl32(pb, 12 * 1024);
        else
            avio_wl32(pb, 0);
        avio_wl32(pb, -1); /* quality */
        avio_wl32(pb, au_ssize); /* sample size */
        avio_wl32(pb, 0);
        avio_wl16(pb, stream->width);
        avio_wl16(pb, stream->height);
        ff_end_tag(pb, strh);

      if(stream->codec_type != AVMEDIA_TYPE_DATA){
        strf = ff_start_tag(pb, "strf");
        switch(stream->codec_type) {
        case AVMEDIA_TYPE_SUBTITLE:
            // XSUB subtitles behave like video tracks, other subtitles
            // are not (yet) supported.
            if (stream->codec_id != CODEC_ID_XSUB) break;
        case AVMEDIA_TYPE_VIDEO:
            ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
            break;
        case AVMEDIA_TYPE_AUDIO:
            if (ff_put_wav_header(pb, stream) < 0) {
                return -1;
            }
            break;
        default:
            return -1;
        }
        ff_end_tag(pb, strf);
        if ((t = av_dict_get(s->streams[i]->metadata, "title", NULL, 0))) {
            avi_write_info_tag(s->pb, "strn", t->value);
            t = NULL;
        }
      }

        if (pb->seekable) {
            unsigned char tag[5];
            int j;

            /* Starting to lay out AVI OpenDML master index.
             * We want to make it JUNK entry for now, since we'd
             * like to get away without making AVI an OpenDML one
             * for compatibility reasons.
             */
            avist->indexes.entry = avist->indexes.ents_allocated = 0;
            avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
            avio_wl16(pb, 4);        /* wLongsPerEntry */
            avio_w8(pb, 0);          /* bIndexSubType (0 == frame index) */
            avio_w8(pb, 0);          /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
            avio_wl32(pb, 0);        /* nEntriesInUse (will fill out later on) */
            ffio_wfourcc(pb, avi_stream2fourcc(tag, i, stream->codec_type));
                                    /* dwChunkId */
            avio_wl64(pb, 0);        /* dwReserved[3]
            avio_wl32(pb, 0);           Must be 0.    */
            for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
                 avio_wl64(pb, 0);
            ff_end_tag(pb, avist->indexes.indx_start);
        }

        if(   stream->codec_type == AVMEDIA_TYPE_VIDEO
           && s->streams[i]->sample_aspect_ratio.num>0
           && s->streams[i]->sample_aspect_ratio.den>0){
            int vprp= ff_start_tag(pb, "vprp");
            AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
                                      (AVRational){stream->width, stream->height});
            int num, den;
            av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);

            avio_wl32(pb, 0); //video format  = unknown
            avio_wl32(pb, 0); //video standard= unknown
            avio_wl32(pb, lrintf(1.0/av_q2d(stream->time_base)));
            avio_wl32(pb, stream->width );
            avio_wl32(pb, stream->height);
            avio_wl16(pb, den);
            avio_wl16(pb, num);
            avio_wl32(pb, stream->width );
            avio_wl32(pb, stream->height);
            avio_wl32(pb, 1); //progressive FIXME

            avio_wl32(pb, stream->height);
            avio_wl32(pb, stream->width );
            avio_wl32(pb, stream->height);
            avio_wl32(pb, stream->width );
            avio_wl32(pb, 0);
            avio_wl32(pb, 0);

            avio_wl32(pb, 0);
            avio_wl32(pb, 0);
            ff_end_tag(pb, vprp);
        }

        ff_end_tag(pb, list2);
    }

    if (pb->seekable) {
        /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
        avi->odml_list = ff_start_tag(pb, "JUNK");
        ffio_wfourcc(pb, "odml");
        ffio_wfourcc(pb, "dmlh");
        avio_wl32(pb, 248);
        for (i = 0; i < 248; i+= 4)
             avio_wl32(pb, 0);
        ff_end_tag(pb, avi->odml_list);
    }

    ff_end_tag(pb, list1);

    list2 = ff_start_tag(pb, "LIST");
    ffio_wfourcc(pb, "INFO");
    ff_metadata_conv(&s->metadata, ff_avi_metadata_conv, NULL);
    for (i = 0; *ff_avi_tags[i]; i++) {
        if ((t = av_dict_get(s->metadata, ff_avi_tags[i], NULL, AV_DICT_MATCH_CASE)))
            avi_write_info_tag(s->pb, t->key, t->value);
    }
    ff_end_tag(pb, list2);

    /* some padding for easier tag editing */
    list2 = ff_start_tag(pb, "JUNK");
    for (i = 0; i < 1016; i += 4)
        avio_wl32(pb, 0);
    ff_end_tag(pb, list2);

    avi->movi_list = ff_start_tag(pb, "LIST");
    ffio_wfourcc(pb, "movi");

    avio_flush(pb);

    return 0;
}
Exemplo n.º 8
0
int64_t AudioInjector::injectNextFrame() {
    if (_state == AudioInjector::State::Finished) {
        qDebug() << "AudioInjector::injectNextFrame called but AudioInjector has finished and was not restarted. Returning.";
        return NEXT_FRAME_DELTA_ERROR_OR_FINISHED;
    }

    // if we haven't setup the packet to send then do so now
    static int positionOptionOffset = -1;
    static int volumeOptionOffset = -1;
    static int audioDataOffset = -1;

    if (!_currentPacket) {
        if (_currentSendOffset < 0 ||
                _currentSendOffset >= _audioData.size()) {
            _currentSendOffset = 0;
        }

        // make sure we actually have samples downloaded to inject
        if (_audioData.size()) {

            int sampleSize = (_options.stereo ? 2 : 1) * sizeof(AudioConstants::AudioSample);
            auto numSamples = static_cast<int>(_audioData.size() / sampleSize);
            auto targetSize = numSamples * sampleSize;
            if (targetSize != _audioData.size()) {
                qDebug() << "Resizing audio that doesn't end at multiple of sample size, resizing from "
                         << _audioData.size() << " to " << targetSize;
                _audioData.resize(targetSize);
            }

            _outgoingSequenceNumber = 0;
            _nextFrame = 0;

            if (!_frameTimer) {
                _frameTimer = std::unique_ptr<QElapsedTimer>(new QElapsedTimer);
            }

            _frameTimer->restart();

            _currentPacket = NLPacket::create(PacketType::InjectAudio);

            // setup the packet for injected audio
            QDataStream audioPacketStream(_currentPacket.get());

            // pack some placeholder sequence number for now
            audioPacketStream << (quint16) 0;

            // pack stream identifier (a generated UUID)
            audioPacketStream << QUuid::createUuid();

            // pack the stereo/mono type of the stream
            audioPacketStream << _options.stereo;

            // pack the flag for loopback
            uchar loopbackFlag = (uchar)true;
            audioPacketStream << loopbackFlag;

            // pack the position for injected audio
            positionOptionOffset = _currentPacket->pos();
            audioPacketStream.writeRawData(reinterpret_cast<const char*>(&_options.position),
                                           sizeof(_options.position));

            // pack our orientation for injected audio
            audioPacketStream.writeRawData(reinterpret_cast<const char*>(&_options.orientation),
                                           sizeof(_options.orientation));

            // pack zero for radius
            float radius = 0;
            audioPacketStream << radius;

            // pack 255 for attenuation byte
            volumeOptionOffset = _currentPacket->pos();
            quint8 volume = MAX_INJECTOR_VOLUME;
            audioPacketStream << volume;

            audioPacketStream << _options.ignorePenumbra;

            audioDataOffset = _currentPacket->pos();

        } else {
            // no samples to inject, return immediately
            qDebug() << "AudioInjector::injectNextFrame() called with no samples to inject. Returning.";
            return NEXT_FRAME_DELTA_ERROR_OR_FINISHED;
        }
    }

    if (!_frameTimer->isValid()) {
        // in the case where we have been restarted, the frame timer will be invalid and we need to start it back over here
        _frameTimer->restart();
    }

    int totalBytesLeftToCopy = (_options.stereo ? 2 : 1) * AudioConstants::NETWORK_FRAME_BYTES_PER_CHANNEL;
    if (!_options.loop) {
        // If we aren't looping, let's make sure we don't read past the end
        totalBytesLeftToCopy = std::min(totalBytesLeftToCopy, _audioData.size() - _currentSendOffset);
    }

    //  Measure the loudness of this frame
    _loudness = 0.0f;
    for (int i = 0; i < totalBytesLeftToCopy; i += sizeof(int16_t)) {
        _loudness += abs(*reinterpret_cast<int16_t*>(_audioData.data() + ((_currentSendOffset + i) % _audioData.size()))) /
                     (AudioConstants::MAX_SAMPLE_VALUE / 2.0f);
    }
    _loudness /= (float)(totalBytesLeftToCopy/ sizeof(int16_t));

    _currentPacket->seek(0);

    // pack the sequence number
    _currentPacket->writePrimitive(_outgoingSequenceNumber);

    _currentPacket->seek(positionOptionOffset);
    _currentPacket->writePrimitive(_options.position);
    _currentPacket->writePrimitive(_options.orientation);

    quint8 volume = MAX_INJECTOR_VOLUME * _options.volume;
    _currentPacket->seek(volumeOptionOffset);
    _currentPacket->writePrimitive(volume);

    _currentPacket->seek(audioDataOffset);

    while (totalBytesLeftToCopy > 0) {
        int bytesToCopy = std::min(totalBytesLeftToCopy, _audioData.size() - _currentSendOffset);

        _currentPacket->write(_audioData.data() + _currentSendOffset, bytesToCopy);
        _currentSendOffset += bytesToCopy;
        totalBytesLeftToCopy -= bytesToCopy;
        if (_options.loop && _currentSendOffset >= _audioData.size()) {
            _currentSendOffset = 0;
        }
    }

    // set the correct size used for this packet
    _currentPacket->setPayloadSize(_currentPacket->pos());

    // grab our audio mixer from the NodeList, if it exists
    auto nodeList = DependencyManager::get<NodeList>();
    SharedNodePointer audioMixer = nodeList->soloNodeOfType(NodeType::AudioMixer);

    if (audioMixer) {
        // send off this audio packet
        nodeList->sendUnreliablePacket(*_currentPacket, *audioMixer);
        _outgoingSequenceNumber++;
    }

    if (_currentSendOffset >= _audioData.size() && !_options.loop) {
        finish();
        return NEXT_FRAME_DELTA_ERROR_OR_FINISHED;
    }

    if (!_hasSentFirstFrame) {
        _hasSentFirstFrame = true;
        // ask AudioInjectorManager to call us right away again to
        // immediately send the first two frames so the mixer can start using the audio right away
        return NEXT_FRAME_DELTA_IMMEDIATELY;
    }

    const int MAX_ALLOWED_FRAMES_TO_FALL_BEHIND = 7;
    int64_t currentTime = _frameTimer->nsecsElapsed() / 1000;
    auto currentFrameBasedOnElapsedTime = currentTime / AudioConstants::NETWORK_FRAME_USECS;

    if (currentFrameBasedOnElapsedTime - _nextFrame > MAX_ALLOWED_FRAMES_TO_FALL_BEHIND) {
        // If we are falling behind by more frames than our threshold, let's skip the frames ahead
        qDebug() << this << "injectNextFrame() skipping ahead, fell behind by " << (currentFrameBasedOnElapsedTime - _nextFrame) << " frames";
        _nextFrame = currentFrameBasedOnElapsedTime;
        _currentSendOffset = _nextFrame * AudioConstants::NETWORK_FRAME_BYTES_PER_CHANNEL * (_options.stereo ? 2 : 1) % _audioData.size();
    }

    int64_t playNextFrameAt = ++_nextFrame * AudioConstants::NETWORK_FRAME_USECS;

    return std::max(INT64_C(0), playNextFrameAt - currentTime);
}
Exemplo n.º 9
0
insn *parse_line(char *buffer, insn *result)
{
    int pass = 1; /* This used to be an argument. it's hardcoded now because we always want it as 1 */ 
    bool insn_is_label = false;
    struct eval_hints hints;
    int operand;
    int critical;
    bool first;
    bool recover;
    int j;

restart_parse:
    first               = true;
    result->forw_ref    = false;

    stdscan_reset();
    stdscan_set(buffer);
    i = stdscan(NULL, &tokval);

    result->label       = NULL; /* Assume no label */
    result->eops        = NULL; /* must do this, whatever happens */
    result->operands    = 0;    /* must initialize this */

    /* Ignore blank lines */
    if (i == TOKEN_EOS) {
        result->opcode = I_none;
        return result;
    }

    if (i != TOKEN_ID       &&
        i != TOKEN_INSN     &&
        i != TOKEN_PREFIX   &&
        (i != TOKEN_REG || !IS_SREG(tokval.t_integer))) {
        nasm_error(ERR_NONFATAL,
                   "label or instruction expected at start of line");
        result->opcode = I_none;
        return result;
    }

    if (i == TOKEN_ID || (insn_is_label && i == TOKEN_INSN)) {
        /* there's a label here */
        first = false;
        result->label = tokval.t_charptr;
        i = stdscan(NULL, &tokval);
        if (i == ':') {         /* skip over the optional colon */
            i = stdscan(NULL, &tokval);
        } else if (i == 0) {
            nasm_error(ERR_WARNING | ERR_WARN_OL | ERR_PASS1,
                  "label alone on a line without a colon might be in error");
        }
    }

    /* Just a label here */
    if (i == TOKEN_EOS) {
        result->opcode = I_none;
        return result;
    }

    for (j = 0; j < MAXPREFIX; j++)
        result->prefixes[j] = P_none;
    result->times = 1L;

    while (i == TOKEN_PREFIX ||
           (i == TOKEN_REG && IS_SREG(tokval.t_integer))) {
        first = false;

        /*
         * Handle special case: the TIMES prefix.
         */
        if (i == TOKEN_PREFIX && tokval.t_integer == P_TIMES) {
            expr *value;

            i = stdscan(NULL, &tokval);
            value = evaluate(stdscan, NULL, &tokval, NULL, pass0, nasm_error, NULL);
            i = tokval.t_type;
            if (!value) {       /* but, error in evaluator */
                result->opcode = I_none;    /* unrecoverable parse error: */
                return result;  /* ignore this instruction */
            }
            if (!is_simple(value)) {
                nasm_error(ERR_NONFATAL,
                      "non-constant argument supplied to TIMES");
                result->times = 1L;
            } else {
                result->times = value->value;
                if (value->value < 0 && pass0 == 2) {
                    nasm_error(ERR_NONFATAL, "TIMES value %"PRId64" is negative",
                          value->value);
                    result->times = 0;
                }
            }
        } else {
            int slot = prefix_slot(tokval.t_integer);
            if (result->prefixes[slot]) {
               if (result->prefixes[slot] == tokval.t_integer)
                    nasm_error(ERR_WARNING | ERR_PASS1,
                               "instruction has redundant prefixes");
               else
                    nasm_error(ERR_NONFATAL,
                               "instruction has conflicting prefixes");
            }
            result->prefixes[slot] = tokval.t_integer;
            i = stdscan(NULL, &tokval);
        }
    }

    if (i != TOKEN_INSN) {
        int j;
        enum prefixes pfx;

        for (j = 0; j < MAXPREFIX; j++) {
            if ((pfx = result->prefixes[j]) != P_none)
                break;
        }

        if (i == 0 && pfx != P_none) {
            /*
             * Instruction prefixes are present, but no actual
             * instruction. This is allowed: at this point we
             * invent a notional instruction of RESB 0.
             */
            result->opcode          = I_RESB;
            result->operands        = 1;
            result->oprs[0].type    = IMMEDIATE;
            result->oprs[0].offset  = 0L;
            result->oprs[0].segment = result->oprs[0].wrt = NO_SEG;
            return result;
        } else {
            nasm_error(ERR_NONFATAL, "parser: instruction expected");
            result->opcode = I_none;
            return result;
        }
    }

    result->opcode = tokval.t_integer;
    result->condition = tokval.t_inttwo;

    /*
     * INCBIN cannot be satisfied with incorrectly
     * evaluated operands, since the correct values _must_ be known
     * on the first pass. Hence, even in pass one, we set the
     * `critical' flag on calling evaluate(), so that it will bomb
     * out on undefined symbols.
     */
    if (result->opcode == I_INCBIN) {
        critical = (pass0 < 2 ? 1 : 2);

    } else
        critical = (pass == 2 ? 2 : 0);

    if (result->opcode == I_DB || result->opcode == I_DW ||
        result->opcode == I_DD || result->opcode == I_DQ ||
        result->opcode == I_DT || result->opcode == I_DO ||
        result->opcode == I_DY || result->opcode == I_INCBIN) {
        extop *eop, **tail = &result->eops, **fixptr;
        int oper_num = 0;
        int32_t sign;

        result->eops_float = false;

        /*
         * Begin to read the DB/DW/DD/DQ/DT/DO/INCBIN operands.
         */
        while (1) {
            i = stdscan(NULL, &tokval);
            if (i == TOKEN_EOS)
                break;
            else if (first && i == ':') {
                insn_is_label = true;
                goto restart_parse;
            }
            first = false;
            fixptr = tail;
            eop = *tail = nasm_malloc(sizeof(extop));
            tail = &eop->next;
            eop->next = NULL;
            eop->type = EOT_NOTHING;
            oper_num++;
            sign = +1;

            /*
             * is_comma_next() here is to distinguish this from
             * a string used as part of an expression...
             */
            if (i == TOKEN_STR && is_comma_next()) {
                eop->type       = EOT_DB_STRING;
                eop->stringval  = tokval.t_charptr;
                eop->stringlen  = tokval.t_inttwo;
                i = stdscan(NULL, &tokval);     /* eat the comma */
            } else if (i == TOKEN_STRFUNC) {
                bool parens = false;
                const char *funcname = tokval.t_charptr;
                enum strfunc func = tokval.t_integer;
                i = stdscan(NULL, &tokval);
                if (i == '(') {
                    parens = true;
                    i = stdscan(NULL, &tokval);
                }
                if (i != TOKEN_STR) {
                    nasm_error(ERR_NONFATAL,
                               "%s must be followed by a string constant",
                               funcname);
                        eop->type = EOT_NOTHING;
                } else {
                    eop->type = EOT_DB_STRING_FREE;
                    eop->stringlen =
                        string_transform(tokval.t_charptr, tokval.t_inttwo,
                                         &eop->stringval, func);
                    if (eop->stringlen == (size_t)-1) {
                        nasm_error(ERR_NONFATAL, "invalid string for transform");
                        eop->type = EOT_NOTHING;
                    }
                }
                if (parens && i && i != ')') {
                    i = stdscan(NULL, &tokval);
                    if (i != ')') {
                        nasm_error(ERR_NONFATAL, "unterminated %s function",
                                   funcname);
                    }
                }
                if (i && i != ',')
                    i = stdscan(NULL, &tokval);
            } else if (i == '-' || i == '+') {
                char *save = stdscan_get();
                int token = i;
                sign = (i == '-') ? -1 : 1;
                i = stdscan(NULL, &tokval);
                if (i != TOKEN_FLOAT) {
                    stdscan_set(save);
                    i = tokval.t_type = token;
                    goto is_expression;
                } else {
                    goto is_float;
                }
            } else if (i == TOKEN_FLOAT) {
is_float:
                eop->type = EOT_DB_STRING;
                result->eops_float = true;

                eop->stringlen = idata_bytes(result->opcode);
                if (eop->stringlen > 16) {
                    nasm_error(ERR_NONFATAL, "floating-point constant"
                               " encountered in DY instruction");
                    eop->stringlen = 0;
                } else if (eop->stringlen < 1) {
                    nasm_error(ERR_NONFATAL, "floating-point constant"
                               " encountered in unknown instruction");
                    /*
                     * fix suggested by Pedro Gimeno... original line was:
                     * eop->type = EOT_NOTHING;
                     */
                    eop->stringlen = 0;
                }

                eop = nasm_realloc(eop, sizeof(extop) + eop->stringlen);
                tail = &eop->next;
                *fixptr = eop;
                eop->stringval = (char *)eop + sizeof(extop);
                if (!eop->stringlen ||
                    !float_const(tokval.t_charptr, sign,
                                 (uint8_t *)eop->stringval,
                                 eop->stringlen, nasm_error))
                    eop->type = EOT_NOTHING;
                i = stdscan(NULL, &tokval); /* eat the comma */
            } else {
                /* anything else, assume it is an expression */
                expr *value;

is_expression:
                value = evaluate(stdscan, NULL, &tokval, NULL,
                                 critical, nasm_error, NULL);
                i = tokval.t_type;
                if (!value) {   /* error in evaluator */
                    result->opcode = I_none;        /* unrecoverable parse error: */
                    return result;      /* ignore this instruction */
                }
                if (is_unknown(value)) {
                    eop->type = EOT_DB_NUMBER;
                    eop->offset = 0;    /* doesn't matter what we put */
                    eop->segment = eop->wrt = NO_SEG;   /* likewise */
                } else if (is_reloc(value)) {
                    eop->type = EOT_DB_NUMBER;
                    eop->offset = reloc_value(value);
                    eop->segment = reloc_seg(value);
                    eop->wrt = reloc_wrt(value);
                } else {
                    nasm_error(ERR_NONFATAL,
                          "operand %d: expression is not simple"
                          " or relocatable", oper_num);
                }
            }

            /*
             * We're about to call stdscan(), which will eat the
             * comma that we're currently sitting on between
             * arguments. However, we'd better check first that it
             * _is_ a comma.
             */
            if (i == TOKEN_EOS) /* also could be EOL */
                break;
            if (i != ',') {
                nasm_error(ERR_NONFATAL, "comma expected after operand %d",
                           oper_num);
                result->opcode = I_none;/* unrecoverable parse error: */
                return result;          /* ignore this instruction */
            }
        }

        if (result->opcode == I_INCBIN) {
            /*
             * Correct syntax for INCBIN is that there should be
             * one string operand, followed by one or two numeric
             * operands.
             */
            if (!result->eops || result->eops->type != EOT_DB_STRING)
                nasm_error(ERR_NONFATAL, "`incbin' expects a file name");
            else if (result->eops->next &&
                     result->eops->next->type != EOT_DB_NUMBER)
                nasm_error(ERR_NONFATAL, "`incbin': second parameter is"
                           " non-numeric");
            else if (result->eops->next && result->eops->next->next &&
                     result->eops->next->next->type != EOT_DB_NUMBER)
                nasm_error(ERR_NONFATAL, "`incbin': third parameter is"
                           " non-numeric");
            else if (result->eops->next && result->eops->next->next &&
                     result->eops->next->next->next)
                nasm_error(ERR_NONFATAL,
                           "`incbin': more than three parameters");
            else
                return result;
            /*
             * If we reach here, one of the above errors happened.
             * Throw the instruction away.
             */
            result->opcode = I_none;
            return result;
        } else /* DB ... */ if (oper_num == 0)
            nasm_error(ERR_WARNING | ERR_PASS1,
                  "no operand for data declaration");
        else
            result->operands = oper_num;

        return result;
    }

    /*
     * Now we begin to parse the operands. There may be up to four
     * of these, separated by commas, and terminated by a zero token.
     */

    for (operand = 0; operand < MAX_OPERANDS; operand++) {
        expr *value;            /* used most of the time */
        int mref;               /* is this going to be a memory ref? */
        int bracket;            /* is it a [] mref, or a & mref? */
        int setsize = 0;

        result->oprs[operand].disp_size = 0;    /* have to zero this whatever */
        result->oprs[operand].eaflags   = 0;    /* and this */
        result->oprs[operand].opflags   = 0;

        i = stdscan(NULL, &tokval);
        if (i == TOKEN_EOS)
            break;              /* end of operands: get out of here */
        else if (first && i == ':') {
            insn_is_label = true;
            goto restart_parse;
        }
        first = false;
        result->oprs[operand].type = 0; /* so far, no override */
        while (i == TOKEN_SPECIAL) {    /* size specifiers */
            switch ((int)tokval.t_integer) {
            case S_BYTE:
                if (!setsize)   /* we want to use only the first */
                    result->oprs[operand].type |= BITS8;
                setsize = 1;
                break;
            case S_WORD:
                if (!setsize)
                    result->oprs[operand].type |= BITS16;
                setsize = 1;
                break;
            case S_DWORD:
            case S_LONG:
                if (!setsize)
                    result->oprs[operand].type |= BITS32;
                setsize = 1;
                break;
            case S_QWORD:
                if (!setsize)
                    result->oprs[operand].type |= BITS64;
                setsize = 1;
                break;
            case S_TWORD:
                if (!setsize)
                    result->oprs[operand].type |= BITS80;
                setsize = 1;
                break;
            case S_OWORD:
                if (!setsize)
                    result->oprs[operand].type |= BITS128;
                setsize = 1;
                break;
            case S_YWORD:
                if (!setsize)
                    result->oprs[operand].type |= BITS256;
                setsize = 1;
                break;
            case S_TO:
                result->oprs[operand].type |= TO;
                break;
            case S_STRICT:
                result->oprs[operand].type |= STRICT;
                break;
            case S_FAR:
                result->oprs[operand].type |= FAR;
                break;
            case S_NEAR:
                result->oprs[operand].type |= NEAR;
                break;
            case S_SHORT:
                result->oprs[operand].type |= SHORT;
                break;
            default:
                nasm_error(ERR_NONFATAL, "invalid operand size specification");
            }
            i = stdscan(NULL, &tokval);
        }

        if (i == '[' || i == '&') {     /* memory reference */
            mref = true;
            bracket = (i == '[');
            i = stdscan(NULL, &tokval); /* then skip the colon */
            while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
                process_size_override(result, operand);
                i = stdscan(NULL, &tokval);
            }
        } else {                /* immediate operand, or register */
            mref = false;
            bracket = false;    /* placate optimisers */
        }

        if ((result->oprs[operand].type & FAR) && !mref &&
            result->opcode != I_JMP && result->opcode != I_CALL) {
            nasm_error(ERR_NONFATAL, "invalid use of FAR operand specifier");
        }

        value = evaluate(stdscan, NULL, &tokval,
                         &result->oprs[operand].opflags,
                         critical, nasm_error, &hints);
        i = tokval.t_type;
        if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
            result->forw_ref = true;
        }
        if (!value) {           /* nasm_error in evaluator */
            result->opcode = I_none;        /* unrecoverable parse error: */
            return result;      /* ignore this instruction */
        }
        if (i == ':' && mref) { /* it was seg:offset */
            /*
             * Process the segment override.
             */
            if (value[1].type   != 0    ||
                value->value    != 1    ||
                !IS_SREG(value->type))
                nasm_error(ERR_NONFATAL, "invalid segment override");
            else if (result->prefixes[PPS_SEG])
                nasm_error(ERR_NONFATAL,
                      "instruction has conflicting segment overrides");
            else {
                result->prefixes[PPS_SEG] = value->type;
                if (IS_FSGS(value->type))
                    result->oprs[operand].eaflags |= EAF_FSGS;
            }

            i = stdscan(NULL, &tokval); /* then skip the colon */
            while (i == TOKEN_SPECIAL || i == TOKEN_PREFIX) {
                process_size_override(result, operand);
                i = stdscan(NULL, &tokval);
            }
            value = evaluate(stdscan, NULL, &tokval,
                             &result->oprs[operand].opflags,
                             critical, nasm_error, &hints);
            i = tokval.t_type;
            if (result->oprs[operand].opflags & OPFLAG_FORWARD) {
                result->forw_ref = true;
            }
            /* and get the offset */
            if (!value) {       /* but, error in evaluator */
                result->opcode = I_none;    /* unrecoverable parse error: */
                return result;  /* ignore this instruction */
            }
        }

        recover = false;
        if (mref && bracket) {  /* find ] at the end */
            if (i != ']') {
                nasm_error(ERR_NONFATAL, "parser: expecting ]");
                recover = true;
            } else {            /* we got the required ] */
                i = stdscan(NULL, &tokval);
                if (i != 0 && i != ',') {
                    nasm_error(ERR_NONFATAL, "comma or end of line expected");
                    recover = true;
                }
            }
        } else {                /* immediate operand */
            if (i != 0 && i != ',' && i != ':') {
                nasm_error(ERR_NONFATAL, "comma, colon or end of line expected");
                recover = true;
            } else if (i == ':') {
                result->oprs[operand].type |= COLON;
            }
        }
        if (recover) {
            do {                /* error recovery */
                i = stdscan(NULL, &tokval);
            } while (i != 0 && i != ',');
        }

        /*
         * now convert the exprs returned from evaluate()
         * into operand descriptions...
         */

        if (mref) {             /* it's a memory reference */
            expr *e = value;
            int b, i, s;        /* basereg, indexreg, scale */
            int64_t o;          /* offset */

            b = i = -1, o = s = 0;
            result->oprs[operand].hintbase = hints.base;
            result->oprs[operand].hinttype = hints.type;

            if (e->type && e->type <= EXPR_REG_END) {   /* this bit's a register */
                if (e->value == 1)      /* in fact it can be basereg */
                    b = e->type;
                else            /* no, it has to be indexreg */
                    i = e->type, s = e->value;
                e++;
            }
            if (e->type && e->type <= EXPR_REG_END) {   /* it's a 2nd register */
                if (b != -1)    /* If the first was the base, ... */
                    i = e->type, s = e->value;  /* second has to be indexreg */

                else if (e->value != 1) {       /* If both want to be index */
                    nasm_error(ERR_NONFATAL,
                          "beroset-p-592-invalid effective address");
                    result->opcode = I_none;
                    return result;
                } else
                    b = e->type;
                e++;
            }
            if (e->type != 0) { /* is there an offset? */
                if (e->type <= EXPR_REG_END) {  /* in fact, is there an error? */
                    nasm_error(ERR_NONFATAL,
                          "beroset-p-603-invalid effective address");
                    result->opcode = I_none;
                    return result;
                } else {
                    if (e->type == EXPR_UNKNOWN) {
                        result->oprs[operand].opflags |= OPFLAG_UNKNOWN;
                        o = 0;  /* doesn't matter what */
                        result->oprs[operand].wrt = NO_SEG;     /* nor this */
                        result->oprs[operand].segment = NO_SEG; /* or this */
                        while (e->type)
                            e++;        /* go to the end of the line */
                    } else {
                        if (e->type == EXPR_SIMPLE) {
                            o = e->value;
                            e++;
                        }
                        if (e->type == EXPR_WRT) {
                            result->oprs[operand].wrt = e->value;
                            e++;
                        } else
                            result->oprs[operand].wrt = NO_SEG;
                        /*
                         * Look for a segment base type.
                         */
                        if (e->type && e->type < EXPR_SEGBASE) {
                            nasm_error(ERR_NONFATAL,
                                  "beroset-p-630-invalid effective address");
                            result->opcode = I_none;
                            return result;
                        }
                        while (e->type && e->value == 0)
                            e++;
                        if (e->type && e->value != 1) {
                            nasm_error(ERR_NONFATAL,
                                  "beroset-p-637-invalid effective address");
                            result->opcode = I_none;
                            return result;
                        }
                        if (e->type) {
                            result->oprs[operand].segment =
                                e->type - EXPR_SEGBASE;
                            e++;
                        } else
                            result->oprs[operand].segment = NO_SEG;
                        while (e->type && e->value == 0)
                            e++;
                        if (e->type) {
                            nasm_error(ERR_NONFATAL,
                                  "beroset-p-650-invalid effective address");
                            result->opcode = I_none;
                            return result;
                        }
                    }
                }
            } else {
                o = 0;
                result->oprs[operand].wrt = NO_SEG;
                result->oprs[operand].segment = NO_SEG;
            }

            if (e->type != 0) { /* there'd better be nothing left! */
                nasm_error(ERR_NONFATAL,
                      "beroset-p-663-invalid effective address");
                result->opcode = I_none;
                return result;
            }

            /* It is memory, but it can match any r/m operand */
            result->oprs[operand].type |= MEMORY_ANY;

            if (b == -1 && (i == -1 || s == 0)) {
                int is_rel = globalbits == 64 &&
                    !(result->oprs[operand].eaflags & EAF_ABS) &&
                    ((globalrel &&
                      !(result->oprs[operand].eaflags & EAF_FSGS)) ||
                     (result->oprs[operand].eaflags & EAF_REL));

                result->oprs[operand].type |= is_rel ? IP_REL : MEM_OFFS;
            }
            result->oprs[operand].basereg = b;
            result->oprs[operand].indexreg = i;
            result->oprs[operand].scale = s;
            result->oprs[operand].offset = o;
        } else {                /* it's not a memory reference */
            if (is_just_unknown(value)) {       /* it's immediate but unknown */
                result->oprs[operand].type      |= IMMEDIATE;
                result->oprs[operand].opflags   |= OPFLAG_UNKNOWN;
                result->oprs[operand].offset    = 0;        /* don't care */
                result->oprs[operand].segment   = NO_SEG;   /* don't care again */
                result->oprs[operand].wrt       = NO_SEG;   /* still don't care */

                if(optimizing >= 0 && !(result->oprs[operand].type & STRICT)) {
                    /* Be optimistic */
                    result->oprs[operand].type |=
                            SBYTE16 | SBYTE32 | SBYTE64 | UDWORD64 | SDWORD64;
                }
            } else if (is_reloc(value)) {       /* it's immediate */
                result->oprs[operand].type      |= IMMEDIATE;
                result->oprs[operand].offset    = reloc_value(value);
                result->oprs[operand].segment   = reloc_seg(value);
                result->oprs[operand].wrt       = reloc_wrt(value);

                if (is_simple(value)) {
                    if (reloc_value(value) == 1)
                        result->oprs[operand].type |= UNITY;
                    if (optimizing >= 0 &&
                        !(result->oprs[operand].type & STRICT)) {
                        int64_t v64 = reloc_value(value);
                        int32_t v32 = (int32_t)v64;
                        int16_t v16 = (int16_t)v32;

                        if (v64 >= -128 && v64 <= 127)
                            result->oprs[operand].type |= SBYTE64;
                        if (v32 >= -128 && v32 <= 127)
                            result->oprs[operand].type |= SBYTE32;
                        if (v16 >= -128 && v16 <= 127)
                            result->oprs[operand].type |= SBYTE16;
                        if ((uint64_t)v64 <= UINT64_C(0xffffffff))
                            result->oprs[operand].type |= UDWORD64;
                        if (v64 >= -INT64_C(0x80000000) &&
                            v64 <=  INT64_C(0x7fffffff))
                            result->oprs[operand].type |= SDWORD64;
                    }
                }
            } else {            /* it's a register */
                unsigned int rs;

                if (value->type >= EXPR_SIMPLE || value->value != 1) {
                    nasm_error(ERR_NONFATAL, "invalid operand type");
                    result->opcode = I_none;
                    return result;
                }

                /*
                 * check that its only 1 register, not an expression...
                 */
                for (i = 1; value[i].type; i++)
                    if (value[i].value) {
                        nasm_error(ERR_NONFATAL, "invalid operand type");
                        result->opcode = I_none;
                        return result;
                    }

                /* clear overrides, except TO which applies to FPU regs */
                if (result->oprs[operand].type & ~TO) {
                    /*
                     * we want to produce a warning iff the specified size
                     * is different from the register size
                     */
                    rs = result->oprs[operand].type & SIZE_MASK;
                } else
                    rs = 0;

                result->oprs[operand].type      &= TO;
                result->oprs[operand].type      |= REGISTER;
                result->oprs[operand].type      |= nasm_reg_flags[value->type];
                result->oprs[operand].basereg   = value->type;

                if (rs && (result->oprs[operand].type & SIZE_MASK) != rs)
                    nasm_error(ERR_WARNING | ERR_PASS1,
                          "register size specification ignored");
            }
        }
    }

    result->operands = operand; /* set operand count */

    /* clear remaining operands */
    while (operand < MAX_OPERANDS)
        result->oprs[operand++].type = 0;

    /*
     * Transform RESW, RESD, RESQ, REST, RESO, RESY into RESB.
     */
    switch (result->opcode) {
    case I_RESW:
        result->opcode = I_RESB;
        result->oprs[0].offset *= 2;
        break;
    case I_RESD:
        result->opcode = I_RESB;
        result->oprs[0].offset *= 4;
        break;
    case I_RESQ:
        result->opcode = I_RESB;
        result->oprs[0].offset *= 8;
        break;
    case I_REST:
        result->opcode = I_RESB;
        result->oprs[0].offset *= 10;
        break;
    case I_RESO:
        result->opcode = I_RESB;
        result->oprs[0].offset *= 16;
        break;
    case I_RESY:
        result->opcode = I_RESB;
        result->oprs[0].offset *= 32;
        break;
    default:
        break;
    }

    return result;
}
Exemplo n.º 10
0
static void
test_virtio_serial (void)
{
  int fd, r, eh;
  char tmpfile[] = "/tmp/speedtestXXXXXX";
  struct sigaction sa, old_sa;

  if (!virtio_serial_upload && !virtio_serial_download)
    return;

  /* Create a sparse file.  We could upload from /dev/zero, but we
   * won't get progress messages because libguestfs tests if the
   * source file is a regular file.
   */
  fd = mkstemp (tmpfile);
  if (fd == -1)
    error (EXIT_FAILURE, errno, "mkstemp: %s", tmpfile);
  if (ftruncate (fd, TEST_SERIAL_MAX_SIZE) == -1)
    error (EXIT_FAILURE, errno, "ftruncate");
  if (close (fd) == -1)
    error (EXIT_FAILURE, errno, "close");

  g = guestfs_create ();
  if (!g)
    error (EXIT_FAILURE, errno, "guestfs_create");

  if (guestfs_add_drive_scratch (g, INT64_C (100*1024*1024), -1) == -1)
    exit (EXIT_FAILURE);

  if (guestfs_launch (g) == -1)
    exit (EXIT_FAILURE);

  /* Make and mount a filesystem which will be used by the download test. */
  if (guestfs_mkfs (g, "ext4", "/dev/sda") == -1)
    exit (EXIT_FAILURE);
  if (guestfs_mount (g, "/dev/sda", "/") == -1)
    exit (EXIT_FAILURE);

  /* Time out the upload after TEST_SERIAL_MAX_TIME seconds have passed. */
  memset (&sa, 0, sizeof sa);
  sa.sa_handler = stop_transfer;
  sa.sa_flags = SA_RESTART;
  sigaction (SIGALRM, &sa, &old_sa);

  /* Get progress messages, which will tell us how much data has been
   * transferred.
   */
  eh = guestfs_set_event_callback (g, progress_cb, GUESTFS_EVENT_PROGRESS,
                                   0, NULL);
  if (eh == -1)
    exit (EXIT_FAILURE);

  if (virtio_serial_upload) {
    gettimeofday (&start, NULL);
    rate = -1;
    operation = "upload";
    alarm (max_time_override > 0 ? max_time_override : TEST_SERIAL_MAX_TIME);

    /* For the upload test, upload the sparse file to /dev/null in the
     * appliance.  Hopefully this is mostly testing just virtio-serial.
     */
    guestfs_push_error_handler (g, NULL, NULL);
    r = guestfs_upload (g, tmpfile, "/dev/null");
    alarm (0);
    unlink (tmpfile);
    guestfs_pop_error_handler (g);

    /* It's possible that the upload will finish before the alarm fires,
     * or that the upload will be stopped by the alarm.
     */
    if (r == -1 && guestfs_last_errno (g) != EINTR) {
      fprintf (stderr,
               "%s: expecting upload command to return EINTR\n%s\n",
               guestfs_int_program_name, guestfs_last_error (g));
      exit (EXIT_FAILURE);
    }

    if (rate == -1) {
    rate_error:
      fprintf (stderr, "%s: internal error: progress callback was not called! (r=%d, errno=%d)\n",
               guestfs_int_program_name,
               r, guestfs_last_errno (g));
      exit (EXIT_FAILURE);
    }

    print_rate ("virtio-serial upload rate:", rate);
  }

  if (virtio_serial_download) {
    /* For the download test, download a sparse file within the
     * appliance to /dev/null on the host.
     */
    if (guestfs_touch (g, "/sparse") == -1)
      exit (EXIT_FAILURE);
    if (guestfs_truncate_size (g, "/sparse", TEST_SERIAL_MAX_SIZE) == -1)
      exit (EXIT_FAILURE);

    gettimeofday (&start, NULL);
    rate = -1;
    operation = "download";
    alarm (max_time_override > 0 ? max_time_override : TEST_SERIAL_MAX_TIME);
    guestfs_push_error_handler (g, NULL, NULL);
    r = guestfs_download (g, "/sparse", "/dev/null");
    alarm (0);
    guestfs_pop_error_handler (g);

    if (r == -1 && guestfs_last_errno (g) != EINTR) {
      fprintf (stderr,
               "%s: expecting download command to return EINTR\n%s\n",
               guestfs_int_program_name, guestfs_last_error (g));
      exit (EXIT_FAILURE);
    }

    if (rate == -1)
      goto rate_error;

    print_rate ("virtio-serial download rate:", rate);
  }

  if (guestfs_shutdown (g) == -1)
    exit (EXIT_FAILURE);

  guestfs_close (g);

  /* Restore SIGALRM signal handler. */
  sigaction (SIGALRM, &old_sa, NULL);
}
Exemplo n.º 11
0
static void
test_block_device (void)
{
  int fd;
  char tmpfile[] = "/tmp/speedtestXXXXXX";
  CLEANUP_FREE char **devices = NULL;
  char *r;
  const char *argv[4];
  int t = max_time_override > 0 ? max_time_override : TEST_BLOCK_DEVICE_TIME;
  char tbuf[64];
  int64_t bytes_written, bytes_read;

  if (!block_device_write && !block_device_read)
    return;

  snprintf (tbuf, sizeof tbuf, "%d", t);

  g = guestfs_create ();
  if (!g)
    error (EXIT_FAILURE, errno, "guestfs_create");

  /* Create a fully allocated backing file.  Note we are not testing
   * the speed of allocation on the host.
   */
  fd = mkstemp (tmpfile);
  if (fd == -1)
    error (EXIT_FAILURE, errno, "mkstemp: %s", tmpfile);
  close (fd);

  if (guestfs_disk_create (g, tmpfile, "raw",
                           INT64_C (1024*1024*1024),
                           GUESTFS_DISK_CREATE_PREALLOCATION, "full",
                           -1) == -1)
    exit (EXIT_FAILURE);

  if (guestfs_add_drive (g, tmpfile) == -1)
    exit (EXIT_FAILURE);

  if (guestfs_launch (g) == -1)
    exit (EXIT_FAILURE);

  devices = guestfs_list_devices (g);
  if (devices == NULL)
    exit (EXIT_FAILURE);
  if (devices[0] == NULL) {
    fprintf (stderr, "%s: expected guestfs_list_devices to return at least 1 device\n",
             guestfs_int_program_name);
    exit (EXIT_FAILURE);
  }

  if (block_device_write) {
    /* Test write speed. */
    argv[0] = devices[0];
    argv[1] = "w";
    argv[2] = tbuf;
    argv[3] = NULL;
    r = guestfs_debug (g, "device_speed", (char **) argv);
    if (r == NULL)
      exit (EXIT_FAILURE);

    if (sscanf (r, "%" SCNi64, &bytes_written) != 1) {
      fprintf (stderr, "%s: could not parse device_speed output\n",
               guestfs_int_program_name);
      exit (EXIT_FAILURE);
    }

    print_rate ("block device writes:", bytes_written / t);
  }

  if (block_device_read) {
    /* Test read speed. */
    argv[0] = devices[0];
    argv[1] = "r";
    argv[2] = tbuf;
    argv[3] = NULL;
    r = guestfs_debug (g, "device_speed", (char **) argv);
    if (r == NULL)
      exit (EXIT_FAILURE);

    if (sscanf (r, "%" SCNi64, &bytes_read) != 1) {
      fprintf (stderr, "%s: could not parse device_speed output\n",
               guestfs_int_program_name);
      exit (EXIT_FAILURE);
    }

    print_rate ("block device reads:", bytes_read / t);
  }

  if (guestfs_shutdown (g) == -1)
    exit (EXIT_FAILURE);

  guestfs_close (g);

  /* Remove temporary file. */
  unlink (tmpfile);
}
Exemplo n.º 12
0
static int avi_write_header(AVFormatContext *s)
{
    AVIContext *avi = s->priv_data;
    AVIOContext *pb = s->pb;
    int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
    AVCodecContext *video_enc;
    AVStream *video_st = NULL;
    int64_t list1, list2, strh, strf;
    AVDictionaryEntry *t = NULL;
    int padding;

    if (s->nb_streams > AVI_MAX_STREAM_COUNT) {
        av_log(s, AV_LOG_ERROR, "AVI does not support >%d streams\n",
               AVI_MAX_STREAM_COUNT);
        return AVERROR(EINVAL);
    }

    for (n = 0; n < s->nb_streams; n++) {
        s->streams[n]->priv_data = av_mallocz(sizeof(AVIStream));
        if (!s->streams[n]->priv_data)
            return AVERROR(ENOMEM);
    }

    /* header list */
    avi->riff_id = 0;
    list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");

    /* avi header */
    ffio_wfourcc(pb, "avih");
    avio_wl32(pb, 14 * 4);
    bitrate = 0;

    video_enc = NULL;
    for (n = 0; n < s->nb_streams; n++) {
        AVCodecContext *codec = s->streams[n]->codec;
        bitrate += codec->bit_rate;
        if (codec->codec_type == AVMEDIA_TYPE_VIDEO) {
            video_enc = codec;
            video_st = s->streams[n];
        }
    }

    nb_frames = 0;

    // TODO: should be avg_frame_rate
    if (video_st)
        avio_wl32(pb, (uint32_t) (INT64_C(1000000) * video_st->time_base.num /
                                  video_st->time_base.den));
    else
        avio_wl32(pb, 0);
    avio_wl32(pb, bitrate / 8); /* XXX: not quite exact */
    avio_wl32(pb, 0); /* padding */
    if (!pb->seekable)
        avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED);  /* flags */
    else
        avio_wl32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED);  /* flags */
    avi->frames_hdr_all = avio_tell(pb); /* remember this offset to fill later */
    avio_wl32(pb, nb_frames); /* nb frames, filled later */
    avio_wl32(pb, 0); /* initial frame */
    avio_wl32(pb, s->nb_streams); /* nb streams */
    avio_wl32(pb, 1024 * 1024); /* suggested buffer size */
    if (video_enc) {
        avio_wl32(pb, video_enc->width);
        avio_wl32(pb, video_enc->height);
    } else {
        avio_wl32(pb, 0);
        avio_wl32(pb, 0);
    }
    avio_wl32(pb, 0); /* reserved */
    avio_wl32(pb, 0); /* reserved */
    avio_wl32(pb, 0); /* reserved */
    avio_wl32(pb, 0); /* reserved */

    /* stream list */
    for (i = 0; i < n; i++) {
        AVStream *st = s->streams[i];
        AVCodecContext *enc = st->codec;
        AVIStream *avist = st->priv_data;
        list2 = ff_start_tag(pb, "LIST");
        ffio_wfourcc(pb, "strl");

        /* stream generic header */
        strh = ff_start_tag(pb, "strh");
        switch (enc->codec_type) {
        case AVMEDIA_TYPE_SUBTITLE:
            // XSUB subtitles behave like video tracks, other subtitles
            // are not (yet) supported.
            if (enc->codec_id != AV_CODEC_ID_XSUB) {
                av_log(s, AV_LOG_ERROR,
                       "Subtitle streams other than DivX XSUB are not supported by the AVI muxer.\n");
                return AVERROR_PATCHWELCOME;
            }
        case AVMEDIA_TYPE_VIDEO:
            ffio_wfourcc(pb, "vids");
            break;
        case AVMEDIA_TYPE_AUDIO:
            ffio_wfourcc(pb, "auds");
            break;
//      case AVMEDIA_TYPE_TEXT:
//          ffio_wfourcc(pb, "txts");
//          break;
        case AVMEDIA_TYPE_DATA:
            ffio_wfourcc(pb, "dats");
            break;
        }
        if (enc->codec_type == AVMEDIA_TYPE_VIDEO ||
            enc->codec_id == AV_CODEC_ID_XSUB)
            avio_wl32(pb, enc->codec_tag);
        else
            avio_wl32(pb, 1);
        avio_wl32(pb, 0); /* flags */
        avio_wl16(pb, 0); /* priority */
        avio_wl16(pb, 0); /* language */
        avio_wl32(pb, 0); /* initial frame */

        ff_parse_specific_params(st, &au_byterate, &au_ssize, &au_scale);

        if (   enc->codec_type == AVMEDIA_TYPE_VIDEO
            && enc->codec_id != AV_CODEC_ID_XSUB
            && au_byterate > 1000LL*au_scale) {
            au_byterate = 600;
            au_scale    = 1;
        }
        avpriv_set_pts_info(st, 64, au_scale, au_byterate);
        if (enc->codec_id == AV_CODEC_ID_XSUB)
            au_scale = au_byterate = 0;

        avio_wl32(pb, au_scale); /* scale */
        avio_wl32(pb, au_byterate); /* rate */

        avio_wl32(pb, 0); /* start */
        /* remember this offset to fill later */
        avist->frames_hdr_strm = avio_tell(pb);
        if (!pb->seekable)
            /* FIXME: this may be broken, but who cares */
            avio_wl32(pb, AVI_MAX_RIFF_SIZE);
        else
            avio_wl32(pb, 0);  /* length, XXX: filled later */

        /* suggested buffer size, is set to largest chunk size in avi_write_trailer */
        if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
            avio_wl32(pb, 1024 * 1024);
        else if (enc->codec_type == AVMEDIA_TYPE_AUDIO)
            avio_wl32(pb, 12 * 1024);
        else
            avio_wl32(pb, 0);
        avio_wl32(pb, -1); /* quality */
        avio_wl32(pb, au_ssize); /* sample size */
        avio_wl32(pb, 0);
        avio_wl16(pb, enc->width);
        avio_wl16(pb, enc->height);
        ff_end_tag(pb, strh);

        if (enc->codec_type != AVMEDIA_TYPE_DATA) {
            int ret;
            enum AVPixelFormat pix_fmt;

            strf = ff_start_tag(pb, "strf");
            switch (enc->codec_type) {
            case AVMEDIA_TYPE_SUBTITLE:
                /* XSUB subtitles behave like video tracks, other subtitles
                 * are not (yet) supported. */
                if (enc->codec_id != AV_CODEC_ID_XSUB)
                    break;
            case AVMEDIA_TYPE_VIDEO:
                /* WMP expects RGB 5:5:5 rawvideo in avi to have bpp set to 16. */
                if (  !enc->codec_tag
                    && enc->codec_id == AV_CODEC_ID_RAWVIDEO
                    && enc->pix_fmt == AV_PIX_FMT_RGB555LE
                    && enc->bits_per_coded_sample == 15)
                    enc->bits_per_coded_sample = 16;
                ff_put_bmp_header(pb, enc, ff_codec_bmp_tags, 0, 0);
                pix_fmt = avpriv_find_pix_fmt(avpriv_pix_fmt_bps_avi,
                                              enc->bits_per_coded_sample);
                if (   !enc->codec_tag
                    && enc->codec_id == AV_CODEC_ID_RAWVIDEO
                    && enc->pix_fmt != pix_fmt
                    && enc->pix_fmt != AV_PIX_FMT_NONE)
                    av_log(s, AV_LOG_ERROR, "%s rawvideo cannot be written to avi, output file will be unreadable\n",
                          av_get_pix_fmt_name(enc->pix_fmt));
                break;
            case AVMEDIA_TYPE_AUDIO:
                if ((ret = ff_put_wav_header(pb, enc, 0)) < 0)
                    return ret;
                break;
            default:
                av_log(s, AV_LOG_ERROR,
                    "Invalid or not supported codec type '%s' found in the input\n",
                    (char *)av_x_if_null(av_get_media_type_string(enc->codec_type), "?"));
                return AVERROR(EINVAL);
            }
            ff_end_tag(pb, strf);
            if ((t = av_dict_get(st->metadata, "title", NULL, 0))) {
                ff_riff_write_info_tag(s->pb, "strn", t->value);
                t = NULL;
            }
            if (enc->codec_id == AV_CODEC_ID_XSUB
            && (t = av_dict_get(s->streams[i]->metadata, "language", NULL, 0))) {
                const char* langstr = av_convert_lang_to(t->value, AV_LANG_ISO639_1);
                t = NULL;
                if (langstr) {
                    char* str = av_asprintf("Subtitle - %s-xx;02", langstr);
                    ff_riff_write_info_tag(s->pb, "strn", str);
                    av_free(str);
                }
            }
        }

        if (pb->seekable) {
            unsigned char tag[5];
            int j;

            /* Starting to lay out AVI OpenDML master index.
             * We want to make it JUNK entry for now, since we'd
             * like to get away without making AVI an OpenDML one
             * for compatibility reasons. */
            avist->indexes.entry      = avist->indexes.ents_allocated = 0;
            avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
            avio_wl16(pb, 4);   /* wLongsPerEntry */
            avio_w8(pb, 0);     /* bIndexSubType (0 == frame index) */
            avio_w8(pb, 0);     /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
            avio_wl32(pb, 0);   /* nEntriesInUse (will fill out later on) */
            ffio_wfourcc(pb, avi_stream2fourcc(tag, i, enc->codec_type));
                                /* dwChunkId */
            avio_wl64(pb, 0);   /* dwReserved[3] */
            // avio_wl32(pb, 0);   /* Must be 0.    */
            for (j = 0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
                avio_wl64(pb, 0);
            ff_end_tag(pb, avist->indexes.indx_start);
        }

        if (enc->codec_type == AVMEDIA_TYPE_VIDEO   &&
            st->sample_aspect_ratio.num > 0 &&
            st->sample_aspect_ratio.den > 0) {
            int vprp       = ff_start_tag(pb, "vprp");
            AVRational dar = av_mul_q(st->sample_aspect_ratio,
                                      (AVRational) { enc->width,
                                                     enc->height });
            int num, den;
            av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);

            avio_wl32(pb, 0); // video format   = unknown
            avio_wl32(pb, 0); // video standard = unknown
            // TODO: should be avg_frame_rate
            avio_wl32(pb, lrintf(1.0 / av_q2d(st->time_base)));
            avio_wl32(pb, enc->width);
            avio_wl32(pb, enc->height);
            avio_wl16(pb, den);
            avio_wl16(pb, num);
            avio_wl32(pb, enc->width);
            avio_wl32(pb, enc->height);
            avio_wl32(pb, 1); // progressive FIXME

            avio_wl32(pb, enc->height);
            avio_wl32(pb, enc->width);
            avio_wl32(pb, enc->height);
            avio_wl32(pb, enc->width);
            avio_wl32(pb, 0);
            avio_wl32(pb, 0);

            avio_wl32(pb, 0);
            avio_wl32(pb, 0);
            ff_end_tag(pb, vprp);
        }

        ff_end_tag(pb, list2);
    }

    if (pb->seekable) {
        /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
        avi->odml_list = ff_start_tag(pb, "JUNK");
        ffio_wfourcc(pb, "odml");
        ffio_wfourcc(pb, "dmlh");
        avio_wl32(pb, 248);
        for (i = 0; i < 248; i += 4)
            avio_wl32(pb, 0);
        ff_end_tag(pb, avi->odml_list);
    }

    ff_end_tag(pb, list1);

    ff_riff_write_info(s);


    padding = s->metadata_header_padding;
    if (padding < 0)
        padding = 1016;

    /* some padding for easier tag editing */
    if (padding) {
        list2 = ff_start_tag(pb, "JUNK");
        for (i = padding; i > 0; i -= 4)
            avio_wl32(pb, 0);
        ff_end_tag(pb, list2);
    }

    avi->movi_list = ff_start_tag(pb, "LIST");
    ffio_wfourcc(pb, "movi");

    avio_flush(pb);

    return 0;
}
Exemplo n.º 13
0
static int _pv_resize_single(struct cmd_context *cmd,
			     struct volume_group *vg,
			     struct physical_volume *pv,
			     const uint64_t new_size)
{
	struct pv_list *pvl;
	uint64_t size = 0;
	uint32_t new_pe_count = 0;
	int r = 0;
	struct dm_list mdas;
	const char *pv_name = pv_dev_name(pv);
	const char *vg_name;
	struct lvmcache_info *info;
	int mda_count = 0;
	struct volume_group *old_vg = vg;

	dm_list_init(&mdas);

	if (is_orphan_vg(pv_vg_name(pv))) {
		vg_name = VG_ORPHANS;
		if (!lock_vol(cmd, vg_name, LCK_VG_WRITE)) {
			log_error("Can't get lock for orphans");
			return 0;
		}

		if (!(pv = pv_read(cmd, pv_name, &mdas, NULL, 1, 0))) {
			unlock_vg(cmd, vg_name);
			log_error("Unable to read PV \"%s\"", pv_name);
			return 0;
		}

		mda_count = dm_list_size(&mdas);
	} else {
		vg_name = pv_vg_name(pv);

		vg = vg_read_for_update(cmd, vg_name, NULL, 0);

		if (vg_read_error(vg))
			goto bad;

		if (!(pvl = find_pv_in_vg(vg, pv_name))) {
			log_error("Unable to find \"%s\" in volume group \"%s\"",
				  pv_name, vg->name);
			goto bad;
		}

		pv = pvl->pv;

		if (!(info = info_from_pvid(pv->dev->pvid, 0))) {
			log_error("Can't get info for PV %s in volume group %s",
				  pv_name, vg->name);
			goto bad;
		}

		mda_count = dm_list_size(&info->mdas);

		if (!archive(vg))
			goto bad;
	}

	/* FIXME Create function to test compatibility properly */
	if (mda_count > 1) {
		log_error("%s: too many metadata areas for pvresize", pv_name);
		goto bad;
	}

	if (!(pv->fmt->features & FMT_RESIZE_PV)) {
		log_error("Physical volume %s format does not support resizing.",
			  pv_name);
		goto bad;
	}

	/* Get new size */
	if (!dev_get_size(pv_dev(pv), &size)) {
		log_error("%s: Couldn't get size.", pv_name);
		goto bad;
	}

	if (new_size) {
		if (new_size > size)
			log_warn("WARNING: %s: Overriding real size. "
				  "You could lose data.", pv_name);
		log_verbose("%s: Pretending size is %" PRIu64 " not %" PRIu64
			    " sectors.", pv_name, new_size, pv_size(pv));
		size = new_size;
	}

	if (size < PV_MIN_SIZE) {
		log_error("%s: Size must exceed minimum of %ld sectors.",
			  pv_name, PV_MIN_SIZE);
		goto bad;
	}

	if (size < pv_pe_start(pv)) {
		log_error("%s: Size must exceed physical extent start of "
			  "%" PRIu64 " sectors.", pv_name, pv_pe_start(pv));
		goto bad;
	}

	pv->size = size;

	if (vg) {
		pv->size -= pv_pe_start(pv);
		new_pe_count = pv_size(pv) / vg->extent_size;

 		if (!new_pe_count) {
			log_error("%s: Size must leave space for at "
				  "least one physical extent of "
				  "%" PRIu32 " sectors.", pv_name,
				  pv_pe_size(pv));
			goto bad;
		}

		if (!pv_resize(pv, vg, new_pe_count))
			goto_bad;
	}

	log_verbose("Resizing volume \"%s\" to %" PRIu64 " sectors.",
		    pv_name, pv_size(pv));

	log_verbose("Updating physical volume \"%s\"", pv_name);
	if (!is_orphan_vg(pv_vg_name(pv))) {
		if (!vg_write(vg) || !vg_commit(vg)) {
			log_error("Failed to store physical volume \"%s\" in "
				  "volume group \"%s\"", pv_name, vg->name);
			goto bad;
		}
		backup(vg);
	} else if (!(pv_write(cmd, pv, NULL, INT64_C(-1)))) {
		log_error("Failed to store physical volume \"%s\"",
			  pv_name);
		goto bad;;
	}

	log_print("Physical volume \"%s\" changed", pv_name);
	r = 1;

bad:
	unlock_vg(cmd, vg_name);
	if (!old_vg)
		vg_release(vg);
	return r;
}
Exemplo n.º 14
0
verify_same_types (INT8_MAX, (int8_t) 0 + 0);

int16_t a2[3] = { INT16_C (17), INT16_MIN, INT16_MAX };
verify (TYPE_MINIMUM (int16_t) == INT16_MIN);
verify (TYPE_MAXIMUM (int16_t) == INT16_MAX);
verify_same_types (INT16_MIN, (int16_t) 0 + 0);
verify_same_types (INT16_MAX, (int16_t) 0 + 0);

int32_t a3[3] = { INT32_C (17), INT32_MIN, INT32_MAX };
verify (TYPE_MINIMUM (int32_t) == INT32_MIN);
verify (TYPE_MAXIMUM (int32_t) == INT32_MAX);
verify_same_types (INT32_MIN, (int32_t) 0 + 0);
verify_same_types (INT32_MAX, (int32_t) 0 + 0);

#ifdef INT64_MAX
int64_t a4[3] = { INT64_C (17), INT64_MIN, INT64_MAX };
verify (TYPE_MINIMUM (int64_t) == INT64_MIN);
verify (TYPE_MAXIMUM (int64_t) == INT64_MAX);
verify_same_types (INT64_MIN, (int64_t) 0 + 0);
verify_same_types (INT64_MAX, (int64_t) 0 + 0);
#endif

uint8_t b1[2] = { UINT8_C (17), UINT8_MAX };
verify (TYPE_MAXIMUM (uint8_t) == UINT8_MAX);
verify_same_types (UINT8_MAX, (uint8_t) 0 + 0);

uint16_t b2[2] = { UINT16_C (17), UINT16_MAX };
verify (TYPE_MAXIMUM (uint16_t) == UINT16_MAX);
verify_same_types (UINT16_MAX, (uint16_t) 0 + 0);

uint32_t b3[2] = { UINT32_C (17), UINT32_MAX };
Exemplo n.º 15
0
static void
check_mult_div (void)
{
    int i, j;
    gint64 v;
    gnc_numeric c, d;
    gnc_numeric amt_a, amt_tot, frac, val_tot, val_a;
    gnc_numeric a, b;

    a = gnc_numeric_create(-100, 100);
    b = gnc_numeric_create(1, 1);
    check_binary_op (gnc_numeric_create(-100, 100),
                     gnc_numeric_div(a, b, GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT),
                     a, b, "expected %s got %s = %s / %s div exact");

    a = gnc_numeric_create(-100, 100);
    b = gnc_numeric_create(-1, 1);
    check_binary_op (gnc_numeric_create(100, 100),
                     gnc_numeric_div(a, b, GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT),
                     a, b, "expected %s got %s = %s / %s div exact");

    a = gnc_numeric_create(-100, 100);
    b = gnc_numeric_create(-1, 1);
    check_binary_op (gnc_numeric_create(100, 100),
                     gnc_numeric_mul(a, b, GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT),
                     a, b, "expected %s got %s = %s * %s mult exact");

    a = gnc_numeric_create(2, 6);
    b = gnc_numeric_create(1, 4);

    check_binary_op (gnc_numeric_create(2, 24),
                     gnc_numeric_mul(a, b, GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT),
                     a, b, "expected %s got %s = %s * %s for mult exact");

    check_binary_op (gnc_numeric_create(1, 12),
                     gnc_numeric_mul(a, b, GNC_DENOM_AUTO, GNC_HOW_DENOM_REDUCE),
                     a, b, "expected %s got %s = %s * %s for mult reduce");

    check_binary_op (gnc_numeric_create(8, 100),
                     gnc_numeric_mul(a, b, 100, GNC_HOW_RND_ROUND),
                     a, b, "expected %s got %s = %s * %s for mult 100th's");

    check_binary_op (gnc_numeric_create(8, 6),
                     gnc_numeric_div(a, b, GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT),
                     a, b, "expected %s got %s = %s / %s for div exact");

    check_binary_op (gnc_numeric_create(4, 3),
                     gnc_numeric_div(a, b, GNC_DENOM_AUTO, GNC_HOW_DENOM_REDUCE),
                     a, b, "expected %s got %s = %s / %s for div reduce");

    check_binary_op (gnc_numeric_create(133, 100),
                     gnc_numeric_div(a, b, 100, GNC_HOW_RND_ROUND),
                     a, b, "expected %s got %s = %s * %s for div 100th's");

    /* Check for math with 2^63 < num*num < 2^64 which previously failed
     * see https://bugs.gnucash.org/show_bug.cgi?id=144980
     */
    v = 1000000;
    a = gnc_numeric_create(1 * v, v);
    b = gnc_numeric_create(10000000 * v, v);

    check_binary_op (b,
                     gnc_numeric_mul(a, b, GNC_DENOM_AUTO, GNC_HOW_DENOM_LCD),
                     a, b, "expected %s got %s = %s * %s for multiply");

    /* Multiply some random numbers.  This test presumes that
     * RAND_MAX is approx 2^32
     */
    for (i = 0; i < NREPS; i++)
    {
        gint64 deno = 1;
        gint64 na = rand();
        gint64 nb = rand();
        gint64 ne;

        /* avoid 0 */
        if (nb / 4 == 0)
        {
            i--;
            continue;
        }

        /* avoid overflow; */
        na /= 2;
        nb /= 2;
        ne = na * nb;

        a = gnc_numeric_create(na, deno);
        b = gnc_numeric_create(nb, deno);

        check_binary_op_equal (gnc_numeric_create(ne, 1),
                               gnc_numeric_mul(a, b, GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT),
                               a, b, "expected %s got %s = %s * %s for mult exact");

        /* Force 128-bit math to come into play */
        for (j = 1; j < 31; j++)
        {
            a = gnc_numeric_create(na << j, 1 << j);
            b = gnc_numeric_create(nb << j, 1 << j);
            check_binary_op (gnc_numeric_create(ne, 1),
                             gnc_numeric_mul(a, b, GNC_DENOM_AUTO, GNC_HOW_DENOM_REDUCE),
                             a, b, "expected %s got %s = %s * %s for mult reduce");
        }

        /* Do some hokey random 128-bit division too */
        b = gnc_numeric_create(deno, nb);

        check_binary_op_equal (gnc_numeric_create(ne, 1),
                               gnc_numeric_div(a, b, GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT),
                               a, b, "expected %s got %s = %s / %s for div exact");

        /* avoid overflow; */
        na /= 2;
        nb /= 2;
        ne = na * nb;
        for (j = 1; j < 16; j++)
        {
            a = gnc_numeric_create(na << j, 1 << j);
            b = gnc_numeric_create(1 << j, nb << j);
            check_binary_op (gnc_numeric_create(ne, 1),
                             gnc_numeric_div(a, b, GNC_DENOM_AUTO, GNC_HOW_DENOM_REDUCE),
                             a, b, "expected %s got %s = %s / %s for div reduce");
        }
    }

    a = gnc_numeric_create(INT64_C(1173888083434299), 93773);
    b = gnc_numeric_create(INT64_C(2222554708930978), 89579);
    /* Dividing the above pair overflows, in that after
     * the division the denominator won't fit into a
     * 64-bit quantity.  This can be seen from
     * the factorization into primes:
     * 1173888083434299 = 3 * 2283317 * 171371749
     * (yes, thats a seven and a nine digit prime)
     * 2222554708930978 = 2 * 1111277354465489
     * (yes, that's a sixteen-digit prime number)
     * 93773 = 79*1187
     * 89579 = 67*7*191
     * If the rounding method is exact/no-round, then
     * an overflow error should be signalled; else the
     * divide routine should shift down the results till
     * the overflow is eliminated.
     */

    check_binary_op (gnc_numeric_error (GNC_ERROR_OVERFLOW),
                     gnc_numeric_div(a, b, GNC_DENOM_AUTO,
                                     GNC_HOW_RND_NEVER | GNC_HOW_DENOM_EXACT),
                     a, b, "expected %s got %s = %s / %s for div exact");

    check_binary_op (gnc_numeric_create(504548, 1000000),
                     gnc_numeric_div(a, b, GNC_DENOM_AUTO,
                                     GNC_HOW_DENOM_SIGFIGS(6) | GNC_HOW_RND_ROUND),
                     a, b, "expected %s got %s = %s / %s for div round");

    /* The below is a 'typical' value calculation:
     * value_frac = value_tot * amt_frace / amt_tot
     * and has some typical potential-overflow values.
     * 82718 = 2 * 59 * 701
     * 47497125586 = 2 * 1489 * 15949337
     * 69100955 = 5 * 7 * 11 * 179483
     * 32005637020 = 4 * 5 * 7 * 43 * 71 * 103 * 727
     */
    a = gnc_numeric_create (-47497125586LL, 82718);
    b = gnc_numeric_create (-69100955LL, 55739);
    c = gnc_numeric_mul (a, b, GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT);
    d = gnc_numeric_create (-32005637020LL, 55739);

    check_binary_op (gnc_numeric_create(-102547458LL, 82718),
                     gnc_numeric_div(c, d, 82718,
                                     GNC_HOW_DENOM_EXACT),
                     c, d, "expected %s got %s = %s / %s for div round");

    /* If we specify GNC_HOW_RND_NEVER, then we shoukld get an error,
     * since the exact result won't fit into a 64-bit quantity. */
    check_binary_op (gnc_numeric_error (GNC_ERROR_REMAINDER),
                     gnc_numeric_div(c, d, 82718,
                                     GNC_HOW_DENOM_EXACT | GNC_HOW_RND_NEVER),
                     c, d, "expected %s got %s = %s / %s for div round");

    /* A simple irreducible ratio, involving negative numbers */
    amt_a = gnc_numeric_create (-6005287905LL, 40595);
    amt_tot = gnc_numeric_create (-8744187958LL, 40595);
    frac = gnc_numeric_div (amt_a, amt_tot,
                            GNC_DENOM_AUTO, GNC_HOW_DENOM_REDUCE);

    check_binary_op (gnc_numeric_create(6005287905LL, 8744187958LL),
                     frac, amt_a, amt_tot,
                     "expected %s got %s = %s / %s for div reduce");

    /* Another overflow-prone condition */
    val_tot = gnc_numeric_create (-4280656418LL, 19873);
    val_a = gnc_numeric_mul (frac, val_tot,
                             gnc_numeric_denom(val_tot),
                             GNC_HOW_RND_ROUND | GNC_HOW_DENOM_REDUCE);
    check_binary_op (gnc_numeric_create(-2939846940LL, 19873),
                     val_a, val_tot, frac,
                     "expected %s got %s = %s * %s for mult round");

    frac = gnc_numeric_create (396226789777979LL, 328758834367851752LL);
    val_tot = gnc_numeric_create (467013515494988LL, 100);
    val_a = gnc_numeric_mul (frac, val_tot,
                             gnc_numeric_denom(val_tot),
                             GNC_HOW_RND_ROUND | GNC_HOW_DENOM_REDUCE);
    check_binary_op (gnc_numeric_create(562854124919LL, 100),
                     val_a, val_tot, frac,
                     "expected %s got %s = %s * %s for mult round");

    /* Yet another bug from bugzilla ... */
    a = gnc_numeric_create (40066447153986554LL, 4518);
    b = gnc_numeric_create (26703286457229LL, 3192);
    frac = gnc_numeric_div(a, b,
                           GNC_DENOM_AUTO,
                           GNC_HOW_DENOM_SIGFIGS(6) |
                           GNC_HOW_RND_ROUND);

    check_binary_op (gnc_numeric_create(106007, 100),
                     frac, a, b,
                     "expected %s got %s = %s / %s for mult sigfigs");

}
Exemplo n.º 16
0
inline void toTimespecNs(timespec& _ts, int64_t _nsecs)
{
	_ts.tv_sec  = _nsecs/INT64_C(1000000000);
	_ts.tv_nsec = _nsecs%INT64_C(1000000000);
}
Exemplo n.º 17
0
static int avi_write_header(AVFormatContext *s)
{
    AVIContext *avi = s->priv_data;
    ByteIOContext *pb = s->pb;
    int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
    AVCodecContext *stream, *video_enc;
    int64_t list1, list2, strh, strf;
	AVRational temp, dar;
	int num, den;
    /* header list */
    avi->riff_id = 0;
    list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl");

    /* avi header */
    put_tag(pb, "avih");
    put_le32(pb, 14 * 4);
    bitrate = 0;

    video_enc = NULL;
    for(n=0;n<s->nb_streams;n++) {
        stream = s->streams[n]->codec;
        bitrate += stream->bit_rate;
        if (stream->codec_type == CODEC_TYPE_VIDEO)
            video_enc = stream;
    }

    nb_frames = 0;

    if(video_enc){
        put_le32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
    } else {
        put_le32(pb, 0);
    }
    put_le32(pb, bitrate / 8); /* XXX: not quite exact */
    put_le32(pb, 0); /* padding */
    if (url_is_streamed(pb))
        put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
    else
        put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
    avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
    put_le32(pb, nb_frames); /* nb frames, filled later */
    put_le32(pb, 0); /* initial frame */
    put_le32(pb, s->nb_streams); /* nb streams */
    put_le32(pb, 1024 * 1024); /* suggested buffer size */
    if(video_enc){
        put_le32(pb, video_enc->width);
        put_le32(pb, video_enc->height);
    } else {
        put_le32(pb, 0);
        put_le32(pb, 0);
    }
    put_le32(pb, 0); /* reserved */
    put_le32(pb, 0); /* reserved */
    put_le32(pb, 0); /* reserved */
    put_le32(pb, 0); /* reserved */

    /* stream list */
    for(i=0;i<n;i++) {
        list2 = start_tag(pb, "LIST");
        put_tag(pb, "strl");

        stream = s->streams[i]->codec;

        /* stream generic header */
        strh = start_tag(pb, "strh");
        switch(stream->codec_type) {
        case CODEC_TYPE_VIDEO: put_tag(pb, "vids"); break;
        case CODEC_TYPE_AUDIO: put_tag(pb, "auds"); break;
//        case CODEC_TYPE_TEXT : put_tag(pb, "txts"); break;
        case CODEC_TYPE_DATA : put_tag(pb, "dats"); break;
        }
        if(stream->codec_type == CODEC_TYPE_VIDEO)
            put_le32(pb, stream->codec_tag);
        else
            put_le32(pb, 1);
        put_le32(pb, 0); /* flags */
        put_le16(pb, 0); /* priority */
        put_le16(pb, 0); /* language */
        put_le32(pb, 0); /* initial frame */

        ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);

        put_le32(pb, au_scale); /* scale */
        put_le32(pb, au_byterate); /* rate */
        av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);

        put_le32(pb, 0); /* start */
        avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
        if (url_is_streamed(pb))
            put_le32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
        else
            put_le32(pb, 0); /* length, XXX: filled later */

        /* suggested buffer size */ //FIXME set at the end to largest chunk
        if(stream->codec_type == CODEC_TYPE_VIDEO)
            put_le32(pb, 1024 * 1024);
        else if(stream->codec_type == CODEC_TYPE_AUDIO)
            put_le32(pb, 12 * 1024);
        else
            put_le32(pb, 0);
        put_le32(pb, -1); /* quality */
        put_le32(pb, au_ssize); /* sample size */
        put_le32(pb, 0);
        put_le16(pb, stream->width);
        put_le16(pb, stream->height);
        end_tag(pb, strh);

      if(stream->codec_type != CODEC_TYPE_DATA){
        strf = start_tag(pb, "strf");
        switch(stream->codec_type) {
        case CODEC_TYPE_VIDEO:
            put_bmp_header(pb, stream, codec_bmp_tags, 0);
            break;
        case CODEC_TYPE_AUDIO:
            if (put_wav_header(pb, stream) < 0) {
                return -1;
            }
            break;
        default:
            return -1;
        }
        end_tag(pb, strf);
      }

        if (!url_is_streamed(pb)) {
            unsigned char tag[5];
            int j;

            /* Starting to lay out AVI OpenDML master index.
             * We want to make it JUNK entry for now, since we'd
             * like to get away without making AVI an OpenDML one
             * for compatibility reasons.
             */
            avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0;
            avi->indexes[i].indx_start = start_tag(pb, "JUNK");
            put_le16(pb, 4);        /* wLongsPerEntry */
            put_byte(pb, 0);        /* bIndexSubType (0 == frame index) */
            put_byte(pb, 0);        /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
            put_le32(pb, 0);        /* nEntriesInUse (will fill out later on) */
            put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
                                    /* dwChunkId */
            put_le64(pb, 0);        /* dwReserved[3]
            put_le32(pb, 0);           Must be 0.    */
            for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
                 put_le64(pb, 0);
            end_tag(pb, avi->indexes[i].indx_start);
        }

        if(   stream->codec_type == CODEC_TYPE_VIDEO
           && s->streams[i]->sample_aspect_ratio.num>0
           && s->streams[i]->sample_aspect_ratio.den>0){
            int vprp= start_tag(pb, "vprp");
            temp.num = stream->width; temp.den = stream->height;//{stream->width, stream->height};
			dar = av_mul_q(s->streams[i]->sample_aspect_ratio, temp);
            
            av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);

            put_le32(pb, 0); //video format  = unknown
            put_le32(pb, 0); //video standard= unknown
            put_le32(pb, lrintf(1.0/av_q2d(stream->time_base)));
            put_le32(pb, stream->width );
            put_le32(pb, stream->height);
            put_le16(pb, den);
            put_le16(pb, num);
            put_le32(pb, stream->width );
            put_le32(pb, stream->height);
            put_le32(pb, 1); //progressive FIXME

            put_le32(pb, stream->height);
            put_le32(pb, stream->width );
            put_le32(pb, stream->height);
            put_le32(pb, stream->width );
            put_le32(pb, 0);
            put_le32(pb, 0);

            put_le32(pb, 0);
            put_le32(pb, 0);
            end_tag(pb, vprp);
        }

        end_tag(pb, list2);
    }

    if (!url_is_streamed(pb)) {
        /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
        avi->odml_list = start_tag(pb, "JUNK");
        put_tag(pb, "odml");
        put_tag(pb, "dmlh");
        put_le32(pb, 248);
        for (i = 0; i < 248; i+= 4)
             put_le32(pb, 0);
        end_tag(pb, avi->odml_list);
    }

    end_tag(pb, list1);

    list2 = start_tag(pb, "LIST");
    put_tag(pb, "INFO");
    avi_write_info_tag(pb, "INAM", s->title);
    avi_write_info_tag(pb, "IART", s->author);
    avi_write_info_tag(pb, "ICOP", s->copyright);
    avi_write_info_tag(pb, "ICMT", s->comment);
    avi_write_info_tag(pb, "IPRD", s->album);
    avi_write_info_tag(pb, "IGNR", s->genre);
    if (s->track) {
        char str_track[4];
        snprintf(str_track, 4, "%d", s->track);
        avi_write_info_tag(pb, "IPRT", str_track);
    }
    if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
        avi_write_info_tag(pb, "ISFT", LIBAVFORMAT_IDENT);
    end_tag(pb, list2);

    /* some padding for easier tag editing */
    list2 = start_tag(pb, "JUNK");
    for (i = 0; i < 1016; i += 4)
        put_le32(pb, 0);
    end_tag(pb, list2);

    avi->movi_list = start_tag(pb, "LIST");
    put_tag(pb, "movi");

    put_flush_packet(pb);

    return 0;
}
Exemplo n.º 18
0
static bool read_integer(int base, bool minus, int64_t *result, bool local)
{
	uint64_t nbr2, nbr;
	int d, n, c; //, nmax;
	char thsep;
	int ndigit_thsep;
	bool first_thsep;

	thsep = LOCAL_get(local)->thousand_sep;
	ndigit_thsep = 0;
	first_thsep = FALSE;

	n = 0;
	nbr = 0;
	
	/*switch (base)
	{
		case 2: nmax = 64; break;
		case 8: nmax = 21; break;
		case 10: nmax = 19; break;
		case 16: nmax = 16; break;
	}*/

	c = last_char();

	for(;;)
	{
		if (local && base == 10)
		{
			if (c == thsep && (ndigit_thsep == 3 || (!first_thsep && ndigit_thsep >= 1 && ndigit_thsep <= 3)))
			{
				c = get_char();
				first_thsep = TRUE;
				ndigit_thsep = 0;
			}
		}

		if (c >= '0' && c <= '9')
		{
			d = c - '0';
			if (local && base == 10)
				ndigit_thsep++;
		}
		else if (c >= 'A' && c <='Z')
			d = c - 'A' + 10;
		else if (c >= 'a' && c <='z')
			d = c - 'a' + 10;
		else
			break;

		if (d >= base)
			break;

		n++;
		
		nbr2 = nbr * base + d;
		
		if ((nbr2 / base) != nbr || nbr2 > ((uint64_t)LLONG_MAX + minus))
			return TRUE;
		
		nbr = nbr2;

		c = get_char();
		if (c < 0)
			break;
	}

	c = last_char();

	if (base == 10)
	{
		if (local && first_thsep && ndigit_thsep != 3)
			return TRUE;
	}
	else
	{
		if ((c == '&' || c == 'u' || c == 'U') && base != 10)
			c = get_char();
		else
		{
			if ((base == 16 && n == 4) || (base == 2 && n == 16))
			{
				if (nbr >= 0x8000L && nbr <= 0xFFFFL)
					nbr |= INT64_C(0xFFFFFFFFFFFF0000);
			}
			else if ((base == 16 && n == 8) || (base == 2 && n == 32))
			{
				if (nbr >= 0x80000000L && nbr <= 0xFFFFFFFFL)
					nbr |= INT64_C(0xFFFFFFFF00000000);
			}
		}
	}

	if (c > 0 && !isspace(c))
		return TRUE;
	
	if (n == 0)
		return TRUE;

	*((int64_t *)result) = nbr;  
	return FALSE;
}
Exemplo n.º 19
0
static MMAL_STATUS_T mmalplay_setup_container_reader(MMALPLAY_T *ctx,
   MMAL_COMPONENT_T *reader, const char *uri)
{
   MMAL_PARAMETER_SEEK_T seek = {{MMAL_PARAMETER_SEEK, sizeof(MMAL_PARAMETER_SEEK_T)},0,0};
   MMAL_PARAMETER_STRING_T *param = 0;
   unsigned int param_size, track_audio, track_video;
   MMAL_STATUS_T status = MMAL_EINVAL;
   uint32_t encoding;
   size_t uri_len;

   if(!reader->output_num)
   {
      LOG_ERROR("%s doesn't have output ports", reader->name);
      goto error;
   }

   /* Open the given URI */
   uri_len = strlen(uri);
   param_size = sizeof(MMAL_PARAMETER_STRING_T) + uri_len;
   param = malloc(param_size);
   if(!param)
   {
      LOG_ERROR("out of memory");
      status = MMAL_ENOMEM;
      goto error;
   }
   memset(param, 0, param_size);
   param->hdr.id = MMAL_PARAMETER_URI;
   param->hdr.size = param_size;
   vcos_safe_strcpy(param->str, uri, uri_len + 1, 0);
   status = mmal_port_parameter_set(reader->control, &param->hdr);
   if(status != MMAL_SUCCESS && status != MMAL_ENOSYS)
   {
      LOG_ERROR("%s could not open file %s", reader->name, uri);
      goto error;
   }
   status = MMAL_SUCCESS;

   /* Look for a video track */
   for(track_video = 0; track_video < reader->output_num; track_video++)
      if(reader->output[track_video]->format->type == MMAL_ES_TYPE_VIDEO) break;
   if(track_video != reader->output_num)
   {
      ctx->reader_video = reader->output[track_video];
      /* Try to detect still images */
      encoding = ctx->reader_video->format->encoding;
      if (encoding == MMAL_ENCODING_JPEG ||
          encoding == MMAL_ENCODING_GIF  ||
          encoding == MMAL_ENCODING_PNG  ||
          encoding == MMAL_ENCODING_PPM  ||
          encoding == MMAL_ENCODING_TGA  ||
          encoding == MMAL_ENCODING_BMP)
         ctx->is_still_image = 1;
   }

   /* Look for an audio track */
   for(track_audio = 0; track_audio < reader->output_num; track_audio++)
      if(reader->output[track_audio]->format->type == MMAL_ES_TYPE_AUDIO) break;
   if(track_audio != reader->output_num)
      ctx->reader_audio = reader->output[track_audio];

   if(track_video == reader->output_num &&
      track_audio == reader->output_num)
   {
      LOG_ERROR("no track to decode");
      status = MMAL_EINVAL;
      goto error;
   }

   LOG_INFO("----Reader input port format-----");
   if(track_video != reader->output_num)
      log_format(reader->output[track_video]->format, 0);
   if(track_audio != reader->output_num)
      log_format(reader->output[track_audio]->format, 0);

   if(ctx->options.seeking)
   {
      LOG_DEBUG("seek to %fs", ctx->options.seeking);
      seek.offset = ctx->options.seeking * INT64_C(1000000);
      status = mmal_port_parameter_set(reader->control, &seek.hdr);
      if(status != MMAL_SUCCESS)
         LOG_ERROR("failed to seek to %fs", ctx->options.seeking);
   }

 error:
   if(param)
      free(param);
   return status;
}
Exemplo n.º 20
0
/*****************************************************************************
* OpenDecoder: probe the decoder and return score
*****************************************************************************/
static int OpenDecoder(vlc_object_t *p_this)
{
    decoder_t *p_dec = (decoder_t*)p_this;
    decoder_sys_t *p_sys;
    cedarx_info_t info;

    memset(&info, 0, sizeof(cedarx_info_t));
    /* Codec specifics */
    switch (p_dec->fmt_in.i_codec)
    {
        case VLC_CODEC_H264:
            info.stream = CEDARX_STREAM_FORMAT_H264;
            break;
        case VLC_CODEC_VC1:
        case VLC_CODEC_WMV3:
            info.stream = CEDARX_STREAM_FORMAT_VC1;
            break;
        case VLC_CODEC_H263:
            info.stream = CEDARX_STREAM_FORMAT_H263;
            break;
        case VLC_CODEC_FLV1:
            info.stream = CEDARX_STREAM_FORMAT_SORENSSON_H263;
            break;
        case VLC_CODEC_MJPG:
            info.stream = CEDARX_STREAM_FORMAT_MJPEG;
            break;
        case VLC_CODEC_VP6:
            info.stream = CEDARX_STREAM_FORMAT_VP6;
            break;
        case VLC_CODEC_VP8:
            info.stream = CEDARX_STREAM_FORMAT_VP8;
            break;
        case VLC_CODEC_WMV1:
            info.stream = CEDARX_STREAM_FORMAT_WMV1;
            break;
        case VLC_CODEC_WMV2:
            info.stream = CEDARX_STREAM_FORMAT_WMV2;
            break;
        case VLC_CODEC_RV10:
        case VLC_CODEC_RV20:
        case VLC_CODEC_RV30:
        case VLC_CODEC_RV40:
            info.stream = CEDARX_STREAM_FORMAT_REALVIDEO;
            break;
        case VLC_CODEC_CAVS:
            info.stream = CEDARX_STREAM_FORMAT_AVS;
            break;
        case VLC_CODEC_MP4V:
            switch(p_dec->fmt_in.i_original_fourcc)
            {
                case VLC_FOURCC('d','i','v','x'):
                case VLC_FOURCC('D','I','V','X'):
                    info.stream = CEDARX_STREAM_FORMAT_DIVX4;
                    break;
                case VLC_FOURCC('D','X','5','0'):
                case VLC_FOURCC('d','x','5','0'):
                    info.stream = CEDARX_STREAM_FORMAT_DIVX5;
                    break;
                default:
                    info.stream = CEDARX_STREAM_FORMAT_XVID;
                    break;
            }
            break;
        case VLC_CODEC_DIV1:
            info.stream = CEDARX_STREAM_FORMAT_DIVX1;
            break;
        case VLC_CODEC_DIV2:
            info.stream = CEDARX_STREAM_FORMAT_DIVX2;
            break;
        case VLC_CODEC_DIV3:
            switch(p_dec->fmt_in.i_original_fourcc)
            {
                case VLC_FOURCC('d','i','v','5'):
                case VLC_FOURCC('D','I','V','5'):
                    info.stream = CEDARX_STREAM_FORMAT_DIVX5;
                    break;
                case VLC_FOURCC('d','i','v','4'):
                case VLC_FOURCC('D','I','V','4'):
                case VLC_FOURCC('d','i','v','f'):
                case VLC_FOURCC('D','I','V','F'):
                  info.stream = CEDARX_STREAM_FORMAT_DIVX4;
                  break;
                default:
                  info.stream = CEDARX_STREAM_FORMAT_DIVX3;
                  break;
            }
            break;
        case VLC_CODEC_MPGV:
            switch(p_dec->fmt_in.i_original_fourcc)
            {
                case VLC_FOURCC('m','p','g','1'):
                case VLC_FOURCC('m','p','1','v'):
                  info.stream = CEDARX_STREAM_FORMAT_MPEG1;
                  break;
                default:
                  info.stream = CEDARX_STREAM_FORMAT_MPEG2;
                  break;
            }
            break;
        default:
            return VLC_EGENERIC;
    }

    info.container = CEDARX_CONTAINER_FORMAT_UNKNOW;
    info.width = p_dec->fmt_in.video.i_width;
    info.height = p_dec->fmt_in.video.i_height;
    info.data = p_dec->fmt_in.p_extra;
    info.data_size = p_dec->fmt_in.i_extra;
    if(p_dec->fmt_in.video.i_frame_rate > 0 && p_dec->fmt_in.video.i_frame_rate_base > 0) {
        info.frame_rate = INT64_C(1000) * 
                p_dec->fmt_in.video.i_frame_rate / 
                p_dec->fmt_in.video.i_frame_rate_base;
        info.frame_duration = INT64_C(1000000) * 
                p_dec->fmt_in.video.i_frame_rate_base / 
                p_dec->fmt_in.video.i_frame_rate;
    }

    /* Open the device */
    if(libcedarx_decoder_open(&info) < 0)
    {
        msg_Err(p_dec, "Couldn't find and open the Cedar device");  
        return VLC_EGENERIC;
    }

    /* Allocate the memory needed to store the decoder's structure */
    p_sys = malloc(sizeof(decoder_sys_t));
    if(!p_sys)
        return VLC_ENOMEM;

    /* Fill decoder_sys_t */
    p_dec->p_sys = p_sys;

#ifdef DECODE_USE_ASYNC_THREAD
    if(vlc_clone(&p_sys->thread, DecoderThread, p_dec, VLC_THREAD_PRIORITY_VIDEO)) {
        free(p_sys);
        libcedarx_decoder_close();
        return VLC_EGENERIC;
    }
#endif
        
    /* Set output properties */
    p_dec->fmt_out.i_cat          = VIDEO_ES;
    p_dec->fmt_out.i_codec        = VLC_CODEC_MV12;
    p_dec->fmt_out.video.i_width  = p_dec->fmt_in.video.i_width;
    p_dec->fmt_out.video.i_height = p_dec->fmt_in.video.i_height;
    p_dec->b_need_packetized      = false;//true;

    /* Set callbacks */
    p_dec->pf_decode_video = DecodeBlock;
    msg_Dbg(p_dec, "Opened Cedar device with success");

    return VLC_SUCCESS;
}
Exemplo n.º 21
0
static int Control (access_t *access, int query, va_list args)
{
    access_sys_t *sys = access->p_sys;
    dvb_device_t *dev = sys->dev;

    switch (query)
    {
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
        {
            bool *v = va_arg (args, bool *);
            *v = false;
            return VLC_SUCCESS;
        }

        case ACCESS_GET_PTS_DELAY:
        {
            int64_t *v = va_arg (args, int64_t *);
            *v = var_InheritInteger (access, "live-caching") * INT64_C(1000);
            return VLC_SUCCESS;
        }

        case ACCESS_GET_TITLE_INFO:
        case ACCESS_GET_META:
            return VLC_EGENERIC;

        case ACCESS_GET_CONTENT_TYPE:
        {
            char **pt = va_arg (args, char **);
            *pt = strdup ("video/MP2T");
            return VLC_SUCCESS;
        }

        case ACCESS_SET_PAUSE_STATE:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
            return VLC_EGENERIC;

        case ACCESS_GET_SIGNAL:
            *va_arg (args, double *) = dvb_get_snr (dev);
            *va_arg (args, double *) = dvb_get_signal_strength (dev);
            return VLC_SUCCESS;

        case ACCESS_SET_PRIVATE_ID_STATE:
        {
            unsigned pid = va_arg (args, unsigned);
            bool add = va_arg (args, unsigned);

            if (unlikely(pid > 0x1FFF))
                return VLC_EGENERIC;
            if (add)
            {
                if (dvb_add_pid (dev, pid))
                    return VLC_EGENERIC;
            }
            else
                dvb_remove_pid (dev, pid);
            return VLC_SUCCESS;
        }

        case ACCESS_SET_PRIVATE_ID_CA:
#ifdef HAVE_DVBPSI
        {
            struct dvbpsi_pmt_s *pmt = va_arg (args, struct dvbpsi_pmt_s *);

            dvb_set_ca_pmt (dev, pmt);
            return VLC_SUCCESS;
        }
#endif

        case ACCESS_GET_PRIVATE_ID_STATE:
            return VLC_EGENERIC;
    }

    msg_Warn (access, "unimplemented query %d in control", query);
    return VLC_EGENERIC;
}
static cddb_disc_t *GetCDDBInfo( access_t *p_access, int i_titles, int *p_sectors )
{
    if( var_InheritInteger( p_access, "album-art" ) == ALBUM_ART_WHEN_ASKED )
        return NULL;

    /* */
    cddb_conn_t *p_cddb = cddb_new();
    if( !p_cddb )
    {
        msg_Warn( p_access, "unable to use CDDB" );
        return NULL;
    }

    /* */

    cddb_http_enable( p_cddb );

    char *psz_tmp = var_InheritString( p_access, "cddb-server" );
    if( psz_tmp )
    {
        cddb_set_server_name( p_cddb, psz_tmp );
        free( psz_tmp );
    }

    cddb_set_server_port( p_cddb, var_InheritInteger( p_access, "cddb-port" ) );

    cddb_set_email_address( p_cddb, "*****@*****.**" );

    cddb_set_http_path_query( p_cddb, "/~cddb/cddb.cgi" );
    cddb_set_http_path_submit( p_cddb, "/~cddb/submit.cgi" );


    char *psz_cachedir;
    char *psz_temp = config_GetUserDir( VLC_CACHE_DIR );

    if( asprintf( &psz_cachedir, "%s" DIR_SEP "cddb", psz_temp ) > 0 ) {
        cddb_cache_enable( p_cddb );
        cddb_cache_set_dir( p_cddb, psz_cachedir );
        free( psz_cachedir );
    }
    free( psz_temp );

    cddb_set_timeout( p_cddb, 10 );

    /* */
    cddb_disc_t *p_disc = cddb_disc_new();
    if( !p_disc )
    {
        msg_Err( p_access, "unable to create CDDB disc structure." );
        goto error;
    }

    int64_t i_length = 0;
    for( int i = 0; i < i_titles; i++ )
    {
        cddb_track_t *t = cddb_track_new();
        cddb_track_set_frame_offset( t, p_sectors[i] );
        cddb_disc_add_track( p_disc, t );
        const int64_t i_size = ( p_sectors[i+1] - p_sectors[i] ) *
                               (int64_t)CDDA_DATA_SIZE;
        i_length += INT64_C(1000000) * i_size / 44100 / 4  ;
    }

    cddb_disc_set_length( p_disc, (int)(i_length/1000000) );

    if( !cddb_disc_calc_discid( p_disc ) )
    {
        msg_Err( p_access, "CDDB disc ID calculation failed" );
        goto error;
    }

    const int i_matches = cddb_query( p_cddb, p_disc );
    if( i_matches < 0 )
    {
        msg_Warn( p_access, "CDDB error: %s", cddb_error_str((cddb_error_t)errno) );			// sunqueen modify
        goto error;
    }
    else if( i_matches == 0 )
    {
        msg_Dbg( p_access, "Couldn't find any matches in CDDB." );
        goto error;
    }
    else if( i_matches > 1 )
        msg_Warn( p_access, "found %d matches in CDDB. Using first one.", i_matches );

    cddb_read( p_cddb, p_disc );

    cddb_destroy( p_cddb);
    return p_disc;

error:
    if( p_disc )
        cddb_disc_destroy( p_disc );
    cddb_destroy( p_cddb );
    return NULL;
}
Exemplo n.º 23
0
static int Demux( demux_t *p_demux )
{
    char       *psz_line;

    mtime_t i_duration = -1;
    char *psz_title = NULL,       *psz_genre = NULL,      *psz_tracknum = NULL,
         *psz_language = NULL,    *psz_artist = NULL,     *psz_album = NULL,
         *psz_date = NULL,        *psz_publisher = NULL,  *psz_encodedby = NULL,
         *psz_description = NULL, *psz_url = NULL,        *psz_copyright = NULL,
         *psz_mrl = NULL;

    input_item_t *p_current_input = GetCurrentItem(p_demux);

    psz_line = stream_ReadLine( p_demux->s );
    char *psz_parse = psz_line;

    /* Skip leading tabs and spaces */
    while( *psz_parse == ' '  || *psz_parse == '\t' ||
           *psz_parse == '\n' || *psz_parse == '\r' )
        psz_parse++;

    /* if the 1st line is "AC", skip it */
    /* TODO: using this information ? */
    if( !strncasecmp( psz_parse, "AC", strlen( "AC" ) ) )
    {
        free( psz_line );
        psz_line = stream_ReadLine( p_demux->s );
    }

    input_item_node_t *p_subitems = input_item_node_Create( p_current_input );

    /* Loop on all lines */
    while( psz_line )
    {
        psz_parse = psz_line;

        /* Skip leading tabs and spaces */
        while( *psz_parse == ' '  || *psz_parse == '\t' ||
               *psz_parse == '\n' || *psz_parse == '\r' )
            psz_parse++;

        /* filename */
        if( !strncasecmp( psz_parse, "NM", strlen( "NM" ) ) )
        {
            char *psz_tabvalue = ParseTabValue( psz_parse );
            if( !EMPTY_STR(psz_tabvalue) )
            {
                psz_mrl = ProcessMRL( psz_tabvalue, p_demux->p_sys->psz_prefix );
            }
            free( psz_tabvalue );
        }

        /* duration */
        else if( !strncasecmp( psz_parse, "DR", strlen( "DR" ) ) )
        {
            char *psz_tabvalue = ParseTabValue( psz_parse );
            if( !EMPTY_STR(psz_tabvalue) )
            {
                int i_parsed_duration = atoi( psz_tabvalue );
                if( i_parsed_duration >= 0 )
                    i_duration = i_parsed_duration * INT64_C(1000);
            }
            free( psz_tabvalue );
        }

#define PARSE(tag,variable)                                     \
    else if( !strncasecmp( psz_parse, tag, strlen( tag ) ) )    \
        variable = ParseTabValue( psz_parse );

        PARSE( "TT", psz_title )
        PARSE( "TG", psz_genre )
        PARSE( "TR", psz_tracknum )
        PARSE( "TL", psz_language )
        PARSE( "TA", psz_artist )
        PARSE( "TB", psz_album )
        PARSE( "TY", psz_date )
        PARSE( "TH", psz_publisher )
        PARSE( "TE", psz_encodedby )
        PARSE( "TC", psz_description )
        PARSE( "TU", psz_url )
        PARSE( "TO", psz_copyright )

#undef PARSE

        /* force a duration ? */
        else if( !strncasecmp( psz_parse, "FD", strlen( "FD" ) ) )
        {}

        /* end of file entry */
        else if( !strncasecmp( psz_parse, "BR!", strlen( "BR!" ) ) )
        {
            /* create the input item */
            input_item_t *p_input = input_item_NewExt( p_demux, psz_mrl,
                                        psz_title, 0, NULL, 0, i_duration );
            input_item_node_AppendItem( p_subitems, p_input );
            FREENULL( psz_mrl );
            FREENULL( psz_title );
            i_duration = -1;

#define SET(variable, type)                             \
    if( !EMPTY_STR(variable) )                          \
    {                                                   \
        input_item_Set##type( p_input, variable );      \
        FREENULL( variable );                           \
    }
            /* set the meta */
            SET( psz_genre, Genre );
            SET( psz_tracknum, TrackNum );
            SET( psz_language, Language );
            SET( psz_artist, Artist );
            SET( psz_album, Album );
            SET( psz_date, Date );
            SET( psz_encodedby, EncodedBy );
            SET( psz_description, Description );
            SET( psz_copyright, Copyright );
#undef SET

            vlc_gc_decref( p_input );
        }
        else
            msg_Warn( p_demux, "invalid line '%s'", psz_parse );

        /* Fetch another line */
        free( psz_line );
        psz_line = stream_ReadLine( p_demux->s );
    }

    input_item_node_PostAndDelete( p_subitems );

    vlc_gc_decref(p_current_input);
    var_Destroy( p_demux, "zpl-extvlcopt" );
    return 0; /* Needed for correct operation of go back */
}
Exemplo n.º 24
0
static inline void write_pred_entry_depth(int64_t* loc, uint16_t depth) {
  *loc = (*loc & INT64_C(0xFFFFFFFFFFFF)) | ((int64_t)(depth & 0xFFFF) << 48);
}
Exemplo n.º 25
0
/* Keep-alive service
 * If the 8080 detects the 'force reload' bit, it initiates a disk
 * boot.  IO is reset, but memory is preserved.
 *
 * If the keep-alive enable bit is set, the -10 updates the keep-alive
 * count field every second.  The 8080 also checks the word every second.
 * If the 8080 finds that the count hasn't changed for 15 consecutive seconds,
 * a Keep-Alive Failure is declared.  This forces the -10 to execute the
 * contents of exec location 71 to collect status and initiate error recovery.
 */
static t_stat kaf_svc (UNIT *uptr)
{
if (M[FE_KEEPA] & INT64_C(0020000000000)) {              /* KSRLD - "Forced" (actually, requested) reload */
    uint32 oldsw = sim_switches;
    DEVICE *bdev = NULL;
    int32 i;

    sim_switches &= ~SWMASK ('P');
    reset_all (4);                                      /* RESET IO starting with UBA */
    sim_switches = oldsw;

    M[FE_KEEPA] &= ~INT64_C(0030000177777);             /* Clear KAF, RLD, KPALIV & reason
                                                         * 8080 ucode actually clears HW 
                                                         * status too, but that's a bug. */
    M[FE_KEEPA] |= 02;                                  /* Reason = FORREL */
    fei_unit.buf = feo_unit.buf = 0;
    M[FE_CTYIN] = M[FE_CTYOUT] = 0;
    M[FE_KLININ] = M[FE_KLINOUT] = 0;

    /* The 8080 has the disk RH address & unit in its memory, even if
     * the previous boot was from tape.  It has no NVM, so the last opr
     * selection will do here.  The case of DS MT <rld> would require a
     * SET FE command.  It's not a common case.
     */

    /* The device may have been detached, disabled or reconfigured since boot time.
     * Therefore, search for it by CSR address & validate that it's bootable.
     * If there are problems, the processor is halted.
     */

    for (i = 0; fe_bootrh && (bdev = sim_devices[i]) != NULL; i++ ) {
        DIB *dibp = (DIB *)bdev->ctxt;
        if (dibp && (fe_bootrh >= dibp->ba) &&
           (fe_bootrh < (dibp->ba + dibp->lnt))) {
            break;
            }
        }

    fe_xct = 2;
    if ((bdev != NULL) && (fe_bootunit >= 0) && (fe_bootunit < (int32) bdev->numunits)) {
        UNIT *bunit = bdev->units + fe_bootunit;

        if (!(bunit->flags & UNIT_DIS) && (bunit->flags & UNIT_ATTABLE) && (bunit->flags & UNIT_ATT)) {
            if (bdev->boot (fe_bootunit, bdev) == SCPE_OK) /* boot the device */
                fe_xct = 1;
            }
        }
    }
else if (M[FE_KEEPA] & INT64_C(0010000000000)) {        /* KPACT */
    d10 kav = M[FE_KEEPA] & INT64_C(0000000177400);     /* KPALIV */
    if (kaf_unit.u3 != (int32)kav) {
        kaf_unit.u3 = (int32)kav;
        kaf_unit.u4 = 0;
        }
    else if (++kaf_unit.u4 >= 15) {
        kaf_unit.u4 = 0;
        M[FE_KEEPA] = (M[FE_KEEPA] & ~INT64_C(0000000000377)) | 01; /* RSN = KAF (leaves enabled) */
        fei_unit.buf = feo_unit.buf = 0;
        M[FE_CTYIN] = M[FE_CTYOUT] = 0;
        M[FE_KLININ] = M[FE_KLINOUT] = 0;
        fe_xct = 071;
        }
    }

sim_activate_after (&kaf_unit, kaf_unit.wait);
if (fe_xct == 2) {
    fe_xct = 0;
    return STOP_CONSOLE;
    }
return SCPE_OK;
}
/**
 * Enables the Hyper-V TSC page.
 *
 * @returns VBox status code.
 * @param   pVM                Pointer to the VM.
 * @param   GCPhysTscPage      Where to map the TSC page.
 * @param   fUseThisTscSeq     Whether to set the TSC sequence number to the one
 *                             specified in @a uTscSeq.
 * @param   uTscSeq            The TSC sequence value to use. Ignored if
 *                             @a fUseThisTscSeq is false.
 */
VMMR3_INT_DECL(int) gimR3HvEnableTscPage(PVM pVM, RTGCPHYS GCPhysTscPage, bool fUseThisTscSeq, uint32_t uTscSeq)
{
    PPDMDEVINSR3    pDevIns = pVM->gim.s.pDevInsR3;
    PGIMMMIO2REGION pRegion = &pVM->gim.s.u.Hv.aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
    AssertPtrReturn(pDevIns, VERR_GIM_DEVICE_NOT_REGISTERED);

    int rc;
    if (pRegion->fMapped)
    {
        /*
         * Is it already enabled at the given guest-address?
         */
        if (pRegion->GCPhysPage == GCPhysTscPage)
            return VINF_SUCCESS;

        /*
         * If it's mapped at a different address, unmap the previous address.
         */
        rc = gimR3HvDisableTscPage(pVM);
        AssertRC(rc);
    }

    /*
     * Map the TSC-page at the specified address.
     */
    Assert(!pRegion->fMapped);
    rc = GIMR3Mmio2Map(pVM, pRegion, GCPhysTscPage);
    if (RT_SUCCESS(rc))
    {
        Assert(pRegion->GCPhysPage == GCPhysTscPage);

        /*
         * Update the TSC scale. Windows guests expect a non-zero TSC sequence, otherwise
         * they fallback to using the reference count MSR which is not ideal in terms of VM-exits.
         *
         * Also, Hyper-V normalizes the time in 10 MHz, see:
         * http://technet.microsoft.com/it-it/sysinternals/dn553408%28v=vs.110%29
         */
        PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)pRegion->pvPageR3;
        Assert(pRefTsc);

        uint64_t const u64TscKHz = TMCpuTicksPerSecond(pVM) / UINT64_C(1000);
        uint32_t       u32TscSeq = 1;
        if (   fUseThisTscSeq
            && uTscSeq < UINT32_C(0xfffffffe))
            u32TscSeq = uTscSeq + 1;
        pRefTsc->u32TscSequence  = u32TscSeq;
        pRefTsc->u64TscScale     = ((INT64_C(10000) << 32) / u64TscKHz) << 32;
        pRefTsc->i64TscOffset    = 0;

        LogRel(("GIM: HyperV: Enabled TSC page at %#RGp - u64TscScale=%#RX64 u64TscKHz=%#RX64 (%'RU64) Seq=%#RU32\n",
                GCPhysTscPage, pRefTsc->u64TscScale, u64TscKHz, u64TscKHz, pRefTsc->u32TscSequence));

        TMR3CpuTickParavirtEnable(pVM);
        return VINF_SUCCESS;
    }
    else
        LogRelFunc(("GIMR3Mmio2Map failed. rc=%Rrc\n", rc));

    return VERR_GIM_OPERATION_FAILED;
}
Exemplo n.º 27
0
/*****************************************************************************
 * DecodeVideo: Called to decode one or more frames
 *****************************************************************************/
picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    int b_drawpicture;
    int b_null_size = false;
    block_t *p_block;

    if( !pp_block || !*pp_block )
        return NULL;

    if( !p_sys->p_context->extradata_size && p_dec->fmt_in.i_extra )
    {
        ffmpeg_InitCodec( p_dec );
        if( p_sys->b_delayed_open )
        {
            if( ffmpeg_OpenCodec( p_dec ) )
                msg_Err( p_dec, "cannot open codec (%s)", p_sys->psz_namecodec );
        }
    }

    p_block = *pp_block;
    if( p_sys->b_delayed_open )
    {
        block_Release( p_block );
        return NULL;
    }

    if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
    {
        p_sys->i_pts = 0; /* To make sure we recover properly */

        p_sys->input_pts = p_sys->input_dts = 0;
        p_sys->i_late_frames = 0;

        if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
            avcodec_flush_buffers( p_sys->p_context );

        block_Release( p_block );
        return NULL;
    }

    if( p_block->i_flags & BLOCK_FLAG_PREROLL )
    {
        /* Do not care about late frames when prerolling
         * TODO avoid decoding of non reference frame
         * (ie all B except for H264 where it depends only on nal_ref_idc) */
        p_sys->i_late_frames = 0;
    }

    if( !p_dec->b_pace_control && (p_sys->i_late_frames > 0) &&
        (mdate() - p_sys->i_late_frames_start > INT64_C(5000000)) )
    {
        if( p_sys->i_pts )
        {
            msg_Err( p_dec, "more than 5 seconds of late video -> "
                     "dropping frame (computer too slow ?)" );
            p_sys->i_pts = 0; /* To make sure we recover properly */
        }
        block_Release( p_block );
        p_sys->i_late_frames--;
        return NULL;
    }

    if( p_block->i_pts > 0 || p_block->i_dts > 0 )
    {
        p_sys->input_pts = p_block->i_pts;
        p_sys->input_dts = p_block->i_dts;

        /* Make sure we don't reuse the same timestamps twice */
        p_block->i_pts = p_block->i_dts = 0;
    }

    /* A good idea could be to decode all I pictures and see for the other */
    if( !p_dec->b_pace_control &&
        p_sys->b_hurry_up &&
        (p_sys->i_late_frames > 4) )
    {
        b_drawpicture = 0;
        if( p_sys->i_late_frames < 12 )
        {
            p_sys->p_context->skip_frame =
                    (p_sys->i_skip_frame <= AVDISCARD_NONREF) ?
                    AVDISCARD_NONREF : p_sys->i_skip_frame;
        }
        else
        {
            /* picture too late, won't decode
             * but break picture until a new I, and for mpeg4 ...*/
            p_sys->i_late_frames--; /* needed else it will never be decrease */
            block_Release( p_block );
            return NULL;
        }
    }
    else
    {
        if( p_sys->b_hurry_up )
            p_sys->p_context->skip_frame = p_sys->i_skip_frame;
        if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
            b_drawpicture = 1;
        else
            b_drawpicture = 0;
    }

    if( p_sys->p_context->width <= 0 || p_sys->p_context->height <= 0 )
    {
        if( p_sys->b_hurry_up )
            p_sys->p_context->skip_frame = p_sys->i_skip_frame;
        b_null_size = true;
    }
    else if( !b_drawpicture )
    {
        /* It creates broken picture
         * FIXME either our parser or ffmpeg is broken */
#if 0
        if( p_sys->b_hurry_up )
            p_sys->p_context->skip_frame = __MAX( p_sys->p_context->skip_frame,
                                                  AVDISCARD_NONREF );
#endif
    }

    /*
     * Do the actual decoding now
     */

    /* Don't forget that ffmpeg requires a little more bytes
     * that the real frame size */
    if( p_block->i_buffer > 0 )
    {
        p_sys->b_flush = ( p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE ) != 0;

        p_block = block_Realloc( p_block, 0,
                            p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
        if( !p_block )
            return NULL;
        p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
        *pp_block = p_block;
        memset( p_block->p_buffer + p_block->i_buffer, 0,
                FF_INPUT_BUFFER_PADDING_SIZE );
    }

    while( p_block->i_buffer > 0 || p_sys->b_flush )
    {
        int i_used, b_gotpicture;
        picture_t *p_pic;

        p_sys->p_ff_pic->pts = p_sys->input_pts;
        i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
                                       &b_gotpicture,
                                       p_block->i_buffer <= 0 && p_sys->b_flush ? NULL : p_block->p_buffer, p_block->i_buffer );

        if( b_null_size && p_sys->p_context->width > 0 &&
            p_sys->p_context->height > 0 &&
            !p_sys->b_flush )
        {
            /* Reparse it to not drop the I frame */
            b_null_size = false;
            if( p_sys->b_hurry_up )
                p_sys->p_context->skip_frame = p_sys->i_skip_frame;
            i_used = avcodec_decode_video( p_sys->p_context, p_sys->p_ff_pic,
                                           &b_gotpicture, p_block->p_buffer,
                                           p_block->i_buffer );
        }

        if( p_sys->b_flush )
            p_sys->b_first_frame = true;

        if( p_block->i_buffer <= 0 )
            p_sys->b_flush = false;

        if( i_used < 0 )
        {
            if( b_drawpicture )
                msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
                          p_block->i_buffer );
            block_Release( p_block );
            return NULL;
        }
        else if( i_used > p_block->i_buffer )
        {
            i_used = p_block->i_buffer;
        }

        /* Consumed bytes */
        p_block->i_buffer -= i_used;
        p_block->p_buffer += i_used;

        /* Nothing to display */
        if( !b_gotpicture )
        {
            if( i_used == 0 ) break;
            continue;
        }

        /* Set the PTS */
        if( p_sys->p_ff_pic->pts )
            p_sys->i_pts = p_sys->p_ff_pic->pts;

        /* Update frame late count (except when doing preroll) */
        mtime_t i_display_date = 0;
        if( !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
            i_display_date = decoder_GetDisplayDate( p_dec, p_sys->i_pts );

        if( i_display_date > 0 && i_display_date <= mdate() )
        {
            p_sys->i_late_frames++;
            if( p_sys->i_late_frames == 1 )
                p_sys->i_late_frames_start = mdate();
        }
        else
        {
            p_sys->i_late_frames = 0;
        }

        if( !b_drawpicture || ( !p_sys->p_va && !p_sys->p_ff_pic->linesize[0] ) )
        {
            /* Do not display the picture */
            p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
            if( !b_drawpicture && p_pic )
                decoder_DeletePicture( p_dec, p_pic );

            ffmpeg_NextPts( p_dec );
            continue;
        }

        if( !p_sys->p_ff_pic->opaque )
        {
            /* Get a new picture */
            p_pic = ffmpeg_NewPictBuf( p_dec, p_sys->p_context );
            if( !p_pic )
            {
                block_Release( p_block );
                return NULL;
            }

            /* Fill p_picture_t from AVVideoFrame and do chroma conversion
             * if needed */
            ffmpeg_CopyPicture( p_dec, p_pic, p_sys->p_ff_pic );
        }
        else
        {
            p_pic = (picture_t *)p_sys->p_ff_pic->opaque;
        }

        /* Sanity check (seems to be needed for some streams) */
        if( p_sys->p_ff_pic->pict_type == FF_B_TYPE )
        {
            p_sys->b_has_b_frames = true;
        }

        if( !p_dec->fmt_in.video.i_sar_num || !p_dec->fmt_in.video.i_sar_den )
        {
            /* Fetch again the aspect ratio in case it changed */
            p_dec->fmt_out.video.i_sar_num
                = p_sys->p_context->sample_aspect_ratio.num;
            p_dec->fmt_out.video.i_sar_den
                = p_sys->p_context->sample_aspect_ratio.den;

            if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den )
            {
                p_dec->fmt_out.video.i_sar_num = 1;
                p_dec->fmt_out.video.i_sar_den = 1;
            }
        }

        /* Send decoded frame to vout */
        if( p_sys->i_pts )
        {
            p_pic->date = p_sys->i_pts;

            ffmpeg_NextPts( p_dec );

            if( p_sys->b_first_frame )
            {
                /* Hack to force display of still pictures */
                p_sys->b_first_frame = false;
                p_pic->b_force = true;
            }

            p_pic->i_nb_fields = 2 + p_sys->p_ff_pic->repeat_pict;
            p_pic->b_progressive = !p_sys->p_ff_pic->interlaced_frame;
            p_pic->b_top_field_first = p_sys->p_ff_pic->top_field_first;

            p_pic->i_qstride = p_sys->p_ff_pic->qstride;
            int i_mb_h = ( p_pic->format.i_height + 15 ) / 16;
            p_pic->p_q = malloc( p_pic->i_qstride * i_mb_h );
            memcpy( p_pic->p_q, p_sys->p_ff_pic->qscale_table,
                    p_pic->i_qstride * i_mb_h );
            switch( p_sys->p_ff_pic->qscale_type )
            {
                case FF_QSCALE_TYPE_MPEG1:
                    p_pic->i_qtype = QTYPE_MPEG1;
                    break;
                case FF_QSCALE_TYPE_MPEG2:
                    p_pic->i_qtype = QTYPE_MPEG2;
                    break;
                case FF_QSCALE_TYPE_H264:
                    p_pic->i_qtype = QTYPE_H264;
                    break;
            }

            return p_pic;
        }
        else
        {
            decoder_DeletePicture( p_dec, p_pic );
        }
    }

    block_Release( p_block );
    return NULL;
}
Exemplo n.º 28
0
void printf_system_info() {
	LOG_INFO("BEGIN-----SYSTEM INFO-----BEGIN");
	LOG_INFO("BOOST_LIB_VERSION="BOOST_LIB_VERSION);

#ifdef NDEBUG
	LOG_INFO("RELEASE");
#else
	LOG_INFO("DEBUG");
#endif

#ifdef BOOST_WINDOWS
	LOG_INFO("BOOST_WINDOWS");
#endif

#ifdef UNIX
	LOG_INFO("UNIX");
#endif

#ifdef _WIN32
	LOG_INFO("PLATFORM=WIN32");
#endif

#ifdef _WIN64
	LOG_INFO("PLATFORM=WIN64");
#endif

#ifdef __WORDSIZE
	LOG_INFO("WORDSIZE=" << __WORDSIZE);
#endif

	LOG_INFO("sizeof(point)=" << sizeof(void*));
	LOG_INFO("sizeof(char)=" << sizeof(char));
	LOG_INFO("sizeof(short)=" << sizeof(short));
	LOG_INFO("sizeof(int)=" << sizeof(int));
	LOG_INFO("sizeof(long)=" << sizeof(long));
	LOG_INFO("sizeof(long long)=" << sizeof(long long));
	LOG_INFO("sizeof(time_t)=" << sizeof(time_t));

	LOG_INFO("PRId32=" << PRId32);
	LOG_INFO("PRId64=" << PRId64);
	LOG_INFO("PRIu32=" << PRIu32);
	LOG_INFO("PRIu64=" << PRIu64);
	LOG_INFO("PRIx32=" << PRIx32);
	LOG_INFO("PRIx64=" << PRIx64);

	char tmp[30];
	int32_t id32 = INT32_C(0x7FFFFFFF);
	_snprintf(tmp, sizeof(tmp), "%"PRId32, id32);
	LOG_INFO("max_i32=" << id32);

	int64_t id64 = INT64_C(0x7FFFFFFFFFFFFFFF);
	_snprintf(tmp, sizeof(tmp), "%"PRId64, id64);
	LOG_INFO("max_i64=" << id64);

	uint32_t ui32 = UINT32_C(0xFFFFFFFF);
	_snprintf(tmp, sizeof(tmp), "%"PRIu32, ui32);
	LOG_INFO("max_ui32=" << ui32);

	uint64_t ui64 = UINT64_C(0xFFFFFFFFFFFFFFFF);
	_snprintf(tmp, sizeof(tmp), "%"PRIu64, ui64);
	LOG_INFO("max_ui64=" << ui64);

#ifdef ENDIAN_LITTLE
	LOG_INFO("LITTLE_ENDIAN=true");
#else
	LOG_INFO("LITTLE_ENDIAN=false");
#endif
	LOG_INFO("END-----SYSTEM INFO-----END");
}
Exemplo n.º 29
0
Arquivo: video.c Projeto: bobwxb/vlc
/*****************************************************************************
 * DecodeVideo: Called to decode one or more frames
 *****************************************************************************/
static picture_t *DecodeVideo( decoder_t *p_dec, block_t **pp_block )
{
    decoder_sys_t *p_sys = p_dec->p_sys;
    AVCodecContext *p_context = p_sys->p_context;
    int b_drawpicture;
    block_t *p_block;

    if( !pp_block )
        return NULL;

    if( !p_context->extradata_size && p_dec->fmt_in.i_extra )
    {
        ffmpeg_InitCodec( p_dec );
        if( p_sys->b_delayed_open )
            OpenVideoCodec( p_dec );
    }

    p_block = *pp_block;
    if(!p_block && !(p_sys->p_codec->capabilities & CODEC_CAP_DELAY) )
        return NULL;

    if( p_sys->b_delayed_open )
    {
        if( p_block )
            block_Release( p_block );
        return NULL;
    }

    if( p_block)
    {
        if( p_block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
        {
            p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */

            p_sys->i_late_frames = 0;

            post_mt( p_sys );
            if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY )
                avcodec_flush_buffers( p_context );
            wait_mt( p_sys );

            block_Release( p_block );
            return NULL;
        }

        if( p_block->i_flags & BLOCK_FLAG_PREROLL )
        {
            /* Do not care about late frames when prerolling
             * TODO avoid decoding of non reference frame
             * (ie all B except for H264 where it depends only on nal_ref_idc) */
            p_sys->i_late_frames = 0;
        }
    }

    if( !p_dec->b_pace_control && (p_sys->i_late_frames > 0) &&
        (mdate() - p_sys->i_late_frames_start > INT64_C(5000000)) )
    {
        if( p_sys->i_pts > VLC_TS_INVALID )
        {
            p_sys->i_pts = VLC_TS_INVALID; /* To make sure we recover properly */
        }
        if( p_block )
            block_Release( p_block );
        p_sys->i_late_frames--;
        msg_Err( p_dec, "more than 5 seconds of late video -> "
                 "dropping frame (computer too slow ?)" );
        return NULL;
    }

    /* A good idea could be to decode all I pictures and see for the other */
    if( !p_dec->b_pace_control &&
        p_sys->b_hurry_up &&
        (p_sys->i_late_frames > 4) )
    {
        b_drawpicture = 0;
        if( p_sys->i_late_frames < 12 )
        {
            p_context->skip_frame =
                    (p_sys->i_skip_frame <= AVDISCARD_NONREF) ?
                    AVDISCARD_NONREF : p_sys->i_skip_frame;
        }
        else
        {
            /* picture too late, won't decode
             * but break picture until a new I, and for mpeg4 ...*/
            p_sys->i_late_frames--; /* needed else it will never be decrease */
            if( p_block )
                block_Release( p_block );
            msg_Warn( p_dec, "More than 4 late frames, dropping frame" );
            return NULL;
        }
    }
    else
    {
        if( p_sys->b_hurry_up )
            p_context->skip_frame = p_sys->i_skip_frame;
        if( !p_block || !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
            b_drawpicture = 1;
        else
            b_drawpicture = 0;
    }

    if( p_context->width <= 0 || p_context->height <= 0 )
    {
        if( p_sys->b_hurry_up )
            p_context->skip_frame = p_sys->i_skip_frame;
    }
    else if( !b_drawpicture )
    {
        /* It creates broken picture
         * FIXME either our parser or ffmpeg is broken */
#if 0
        if( p_sys->b_hurry_up )
            p_context->skip_frame = __MAX( p_context->skip_frame,
                                                  AVDISCARD_NONREF );
#endif
    }

    /*
     * Do the actual decoding now */

    /* Don't forget that libavcodec requires a little more bytes
     * that the real frame size */
    if( p_block && p_block->i_buffer > 0 )
    {
        p_sys->b_flush = ( p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE ) != 0;

        p_block = block_Realloc( p_block, 0,
                            p_block->i_buffer + FF_INPUT_BUFFER_PADDING_SIZE );
        if( !p_block )
            return NULL;
        p_block->i_buffer -= FF_INPUT_BUFFER_PADDING_SIZE;
        *pp_block = p_block;
        memset( p_block->p_buffer + p_block->i_buffer, 0,
                FF_INPUT_BUFFER_PADDING_SIZE );
    }

    while( !p_block || p_block->i_buffer > 0 || p_sys->b_flush )
    {
        int i_used, b_gotpicture;
        AVPacket pkt;

        AVFrame *frame = av_frame_alloc();
        if (unlikely(frame == NULL))
        {
            p_dec->b_error = true;
            break;
        }

        post_mt( p_sys );

        av_init_packet( &pkt );
        if( p_block )
        {
            pkt.data = p_block->p_buffer;
            pkt.size = p_block->i_buffer;
            pkt.pts = p_block->i_pts;
            pkt.dts = p_block->i_dts;
        }
        else
        {
            /* Return delayed frames if codec has CODEC_CAP_DELAY */
            pkt.data = NULL;
            pkt.size = 0;
        }

        if( !p_sys->palette_sent )
        {
            uint8_t *pal = av_packet_new_side_data(&pkt, AV_PKT_DATA_PALETTE, AVPALETTE_SIZE);
            if (pal) {
                memcpy(pal, p_dec->fmt_in.video.p_palette->palette, AVPALETTE_SIZE);
                p_sys->palette_sent = true;
            }
        }

        /* Make sure we don't reuse the same timestamps twice */
        if( p_block )
        {
            p_block->i_pts =
            p_block->i_dts = VLC_TS_INVALID;
        }

        i_used = avcodec_decode_video2( p_context, frame, &b_gotpicture,
                                        &pkt );
        av_free_packet( &pkt );

        wait_mt( p_sys );

        if( p_sys->b_flush )
            p_sys->b_first_frame = true;

        if( p_block )
        {
            if( p_block->i_buffer <= 0 )
                p_sys->b_flush = false;

            if( i_used < 0 )
            {
                av_frame_unref(frame);
                if( b_drawpicture )
                    msg_Warn( p_dec, "cannot decode one frame (%zu bytes)",
                            p_block->i_buffer );
                break;
            }
            else if( (unsigned)i_used > p_block->i_buffer ||
                    p_context->thread_count > 1 )
            {
                i_used = p_block->i_buffer;
            }

            /* Consumed bytes */
            p_block->i_buffer -= i_used;
            p_block->p_buffer += i_used;
        }

        /* Nothing to display */
        if( !b_gotpicture )
        {
            av_frame_unref(frame);
            if( i_used == 0 ) break;
            continue;
        }

        /* Compute the PTS */
        mtime_t i_pts = frame->pkt_pts;
        if (i_pts <= VLC_TS_INVALID)
            i_pts = frame->pkt_dts;

        if( i_pts <= VLC_TS_INVALID )
            i_pts = p_sys->i_pts;

        /* Interpolate the next PTS */
        if( i_pts > VLC_TS_INVALID )
            p_sys->i_pts = i_pts;
        if( p_sys->i_pts > VLC_TS_INVALID )
        {
            /* interpolate the next PTS */
            if( p_dec->fmt_in.video.i_frame_rate > 0 &&
                p_dec->fmt_in.video.i_frame_rate_base > 0 )
            {
                p_sys->i_pts += CLOCK_FREQ * (2 + frame->repeat_pict) *
                    p_dec->fmt_in.video.i_frame_rate_base /
                    (2 * p_dec->fmt_in.video.i_frame_rate);
            }
            else if( p_context->time_base.den > 0 )
            {
                int i_tick = p_context->ticks_per_frame;
                if( i_tick <= 0 )
                    i_tick = 1;

                p_sys->i_pts += CLOCK_FREQ * (2 + frame->repeat_pict) *
                    i_tick * p_context->time_base.num /
                    (2 * p_context->time_base.den);
            }
        }

        /* Update frame late count (except when doing preroll) */
        mtime_t i_display_date = 0;
        if( !p_block || !(p_block->i_flags & BLOCK_FLAG_PREROLL) )
            i_display_date = decoder_GetDisplayDate( p_dec, i_pts );

        if( i_display_date > 0 && i_display_date <= mdate() )
        {
            p_sys->i_late_frames++;
            if( p_sys->i_late_frames == 1 )
                p_sys->i_late_frames_start = mdate();
        }
        else
        {
            p_sys->i_late_frames = 0;
        }

        if( !b_drawpicture || ( !p_sys->p_va && !frame->linesize[0] ) )
        {
            av_frame_unref(frame);
            continue;
        }

        picture_t *p_pic = frame->opaque;
        if( p_pic == NULL )
        {
            /* Get a new picture */
            if( p_sys->p_va == NULL )
                p_pic = ffmpeg_NewPictBuf( p_dec, p_context );
            if( !p_pic )
            {
                av_frame_unref(frame);
                break;
            }

            /* Fill picture_t from AVFrame */
            lavc_CopyPicture(p_dec, p_pic, frame);
        }
        else
        {
            if( p_sys->p_va != NULL )
                vlc_va_Extract( p_sys->p_va, p_pic, frame->data[3] );
            picture_Hold( p_pic );
        }

        if( !p_dec->fmt_in.video.i_sar_num || !p_dec->fmt_in.video.i_sar_den )
        {
            /* Fetch again the aspect ratio in case it changed */
            p_dec->fmt_out.video.i_sar_num
                = p_context->sample_aspect_ratio.num;
            p_dec->fmt_out.video.i_sar_den
                = p_context->sample_aspect_ratio.den;

            if( !p_dec->fmt_out.video.i_sar_num || !p_dec->fmt_out.video.i_sar_den )
            {
                p_dec->fmt_out.video.i_sar_num = 1;
                p_dec->fmt_out.video.i_sar_den = 1;
            }
        }

        p_pic->date = i_pts;
        /* Hack to force display of still pictures */
        p_pic->b_force = p_sys->b_first_frame;
        p_pic->i_nb_fields = 2 + frame->repeat_pict;
        p_pic->b_progressive = !frame->interlaced_frame;
        p_pic->b_top_field_first = frame->top_field_first;

        av_frame_unref(frame);

        /* Send decoded frame to vout */
        if (i_pts > VLC_TS_INVALID)
        {
            p_sys->b_first_frame = false;
            return p_pic;
        }
        else
            picture_Release( p_pic );
    }

    if( p_block )
        block_Release( p_block );
    return NULL;
}
static char* log_reader_aggregates_into_single_histogram()
{
    const char* file_name = "histogram.log";
    struct timespec timestamp;
    struct timespec interval;

    hdr_gettime(&timestamp);
    interval.tv_sec = 5;
    interval.tv_nsec = 2000000;

    struct hdr_log_writer writer;
    struct hdr_log_reader reader;
    hdr_log_writer_init(&writer);
    hdr_log_reader_init(&reader);
    int rc = 0;

    FILE* log_file = fopen(file_name, "w+");

    hdr_log_write_header(&writer, log_file, "Test log", &timestamp);
    hdr_log_write(&writer, log_file, &timestamp, &interval, cor_histogram);
    hdr_log_write(&writer, log_file, &timestamp, &interval, raw_histogram);
    fflush(log_file);
    fclose(log_file);

    log_file = fopen(file_name, "r");

    struct hdr_histogram* histogram;
    hdr_alloc(INT64_C(3600) * 1000 * 1000, 3, &histogram);

    rc = hdr_log_read_header(&reader, log_file);
    mu_assert("Failed header read", validate_return_code(rc));
    rc = hdr_log_read(&reader, log_file, &histogram, NULL, NULL);
    mu_assert("Failed corrected read", validate_return_code(rc));
    rc = hdr_log_read(&reader, log_file, &histogram, NULL, NULL);
    mu_assert("Failed raw read", validate_return_code(rc));

    struct hdr_iter iter;
    hdr_iter_recorded_init(&iter, histogram);
    int64_t expected_total_count =
        raw_histogram->total_count + cor_histogram->total_count;

    mu_assert(
        "Total counts incorrect",
        compare_int64(histogram->total_count, expected_total_count));

    while (hdr_iter_next(&iter))
    {
        int64_t count = iter.count;
        int64_t value = iter.value;

        int64_t expected_count =
            hdr_count_at_value(raw_histogram, value) +
            hdr_count_at_value(cor_histogram, value);

        mu_assert("Incorrect count", compare_int64(count, expected_count));
    }

    fclose(log_file);
    remove(file_name);
    free(histogram);

    return 0;
}