Revision 6775c758 libav/img.c

View differences:

libav/img.c
29 29
#define IMGFMT_YUV     1
30 30
#define IMGFMT_PGMYUV  2
31 31
#define IMGFMT_PGM     3
32
#define IMGFMT_PPM     4
32 33

  
33 34
typedef struct {
34 35
    int width;
......
109 110
    return 0;
110 111
}
111 112

  
113
static int ppm_read(VideoData *s, ByteIOContext *f, UINT8 *buf, int size)
114
{
115
    int width, height;
116
    char buf1[32];
117
    UINT8 *picture[3];
118

  
119
    width = s->width;
120
    height = s->height;
121

  
122
    pnm_get(f, buf1, sizeof(buf1));
123
    if (strcmp(buf1, "P6")) {
124
        return -EIO;
125
    }
126
    
127
    pnm_get(f, buf1, sizeof(buf1));
128
    pnm_get(f, buf1, sizeof(buf1));
129
    pnm_get(f, buf1, sizeof(buf1));
130
    
131
    picture[0] = buf;
132
    get_buffer(f, picture[0], width * height*3);
133
    
134
    return 0;
135

  
136
}
137

  
112 138
static int yuv_read(VideoData *s, const char *filename, UINT8 *buf, int size1)
113 139
{
114 140
    ByteIOContext pb1, *pb = &pb1;
......
176 202
    case IMGFMT_YUV:
177 203
        ret = yuv_read(s, filename, pkt->data, pkt->size);
178 204
        break;
205
    case IMGFMT_PPM:
206
        ret = ppm_read(s, f, pkt->data, pkt->size);
207
        break;
179 208
    default:
180 209
        return -EIO;
181 210
    }
......
250 279
    else
251 280
        s->is_pipe = 1;
252 281
        
253
    if (s1->format == &pgmpipe_format ||
282
    if (s1->format == &pgmyuvpipe_format ||
254 283
        s1->format == &pgmyuv_format)
255 284
        s->img_fmt = IMGFMT_PGMYUV;
256
    else if (s1->format == &pgm_format)
285
    else if (s1->format == &pgmpipe_format ||
286
             s1->format == &pgm_format)
257 287
        s->img_fmt = IMGFMT_PGM;
258 288
    else if (s1->format == &imgyuv_format)
259 289
        s->img_fmt = IMGFMT_YUV;
290
    else if (s1->format == &ppmpipe_format ||
291
             s1->format == &ppm_format)
292
        s->img_fmt = IMGFMT_PPM;
260 293
    else
261 294
        goto fail;
262 295

  
......
279 312
    switch(s->img_fmt) {
280 313
    case IMGFMT_PGM:
281 314
    case IMGFMT_PGMYUV:
315
    case IMGFMT_PPM:
282 316
        pnm_get(f, buf1, sizeof(buf1));
283 317
        pnm_get(f, buf1, sizeof(buf1));
284 318
        s->width = atoi(buf1);
......
316 350
        url_fseek(f, 0, SEEK_SET);
317 351
    }
318 352
    
319
    s->img_size = (s->width * s->height * 3) / 2;
320 353

  
321 354
    st->codec.codec_type = CODEC_TYPE_VIDEO;
322 355
    st->codec.codec_id = CODEC_ID_RAWVIDEO;
323 356
    st->codec.width = s->width;
324 357
    st->codec.height = s->height;
325
    st->codec.pix_fmt = PIX_FMT_YUV420P;
358
    if (s->img_fmt == IMGFMT_PPM) {
359
        st->codec.pix_fmt = PIX_FMT_RGB24;
360
        s->img_size = (s->width * s->height * 3);
361
    } else {
362
        st->codec.pix_fmt = PIX_FMT_YUV420P;
363
        s->img_size = (s->width * s->height * 3) / 2;
364
    }
326 365
    if (!ap || !ap->frame_rate)
327 366
        st->codec.frame_rate = 25 * FRAME_RATE_BASE;
328 367
    else
......
347 386
/******************************************************/
348 387
/* image output */
349 388

  
350
int pgm_save(AVPicture *picture, int width, int height, ByteIOContext *pb, int is_yuv) 
389
static int pgm_save(AVPicture *picture, int width, int height, ByteIOContext *pb, int is_yuv) 
351 390
{
352 391
    int i, h;
353 392
    char buf[100];
......
383 422
    return 0;
384 423
}
385 424

  
425
static int ppm_save(AVPicture *picture, int width, int height, ByteIOContext *pb) 
426
{
427
    int i;
428
    char buf[100];
429
    UINT8 *ptr;
430

  
431
    snprintf(buf, sizeof(buf), 
432
             "P6\n%d %d\n%d\n",
433
             width, height, 255);
434
    put_buffer(pb, buf, strlen(buf));
435
    
436
    ptr = picture->data[0];
437
    for(i=0;i<height;i++) {
438
        put_buffer(pb, ptr, width * 3);
439
        ptr += picture->linesize[0];
440
    }
441

  
442
    put_flush_packet(pb);
443
    return 0;
444
}
445

  
386 446
static int yuv_save(AVPicture *picture, int width, int height, const char *filename)
387 447
{
388 448
    ByteIOContext pb1, *pb = &pb1;
......
434 494
    else
435 495
        img->is_pipe = 1;
436 496
        
437
    if (s->format == &pgmpipe_format ||
438
        s->format == &pgmyuv_format)
497
    if (s->format == &pgmyuvpipe_format ||
498
        s->format == &pgmyuv_format) {
439 499
        img->img_fmt = IMGFMT_PGMYUV;
440
    else if (s->format == &pgm_format)
500
    } else if (s->format == &pgmpipe_format ||
501
               s->format == &pgm_format) {
441 502
        img->img_fmt = IMGFMT_PGM;
442
    else if (s->format == &imgyuv_format)
503
    } else if (s->format == &imgyuv_format) {
443 504
        img->img_fmt = IMGFMT_YUV;
444
    else
505
    } else if (s->format == &ppmpipe_format ||
506
               s->format == &ppm_format) {
507
        img->img_fmt = IMGFMT_PPM;
508
    } else {
445 509
        goto fail;
510
    }
446 511
    return 0;
447 512
 fail:
448 513
    free(img);
......
461 526

  
462 527
    width = st->codec.width;
463 528
    height = st->codec.height;
464
    size1 = (width * height * 3) / 2;
465
    if (size != size1)
466
        return -EIO;
467 529

  
468
    picture.data[0] = buf;
469
    picture.data[1] = picture.data[0] + width * height;
470
    picture.data[2] = picture.data[1] + (width * height) / 4;
471
    picture.linesize[0] = width;
472
    picture.linesize[1] = width >> 1; 
473
    picture.linesize[2] = width >> 1;
530
    switch(st->codec.pix_fmt) {
531
    case PIX_FMT_YUV420P:
532
        size1 = (width * height * 3) / 2;
533
        if (size != size1)
534
            return -EIO;
535
        
536
        picture.data[0] = buf;
537
        picture.data[1] = picture.data[0] + width * height;
538
        picture.data[2] = picture.data[1] + (width * height) / 4;
539
        picture.linesize[0] = width;
540
        picture.linesize[1] = width >> 1; 
541
        picture.linesize[2] = width >> 1;
542
        break;
543
    case PIX_FMT_RGB24:
544
        size1 = (width * height * 3);
545
        if (size != size1)
546
            return -EIO;
547
        picture.data[0] = buf;
548
        picture.linesize[0] = width * 3;
549
        break;
550
    default:
551
        return -EIO;
552
    }
553
    
474 554
    snprintf(filename, sizeof(filename), img->path, img->img_number);
475 555

  
476 556
    if (!img->is_pipe) {
......
490 570
    case IMGFMT_YUV:
491 571
        ret = yuv_save(&picture, width, height, filename);
492 572
        break;
573
    case IMGFMT_PPM:
574
        ret = ppm_save(&picture, width, height, pb);
575
        break;
493 576
    }
494 577
    if (!img->is_pipe) {
495 578
        url_fclose(pb);
......
542 625
    AVFMT_NOFILE,
543 626
};
544 627

  
628
AVFormat ppm_format = {
629
    "ppm",
630
    "ppm image format",
631
    "",
632
    "ppm",
633
    CODEC_ID_NONE,
634
    CODEC_ID_RAWVIDEO,
635
    img_write_header,
636
    img_write_packet,
637
    img_write_trailer,
638

  
639
    img_read_header,
640
    img_read_packet,
641
    img_read_close,
642
    NULL,
643
    AVFMT_NOFILE,
644
};
645

  
545 646
AVFormat imgyuv_format = {
546 647
    ".Y.U.V",
547 648
    ".Y.U.V format",
......
576 677
    img_read_close,
577 678
    NULL,
578 679
};
680

  
681
AVFormat pgmyuvpipe_format = {
682
    "pgmyuvpipe",
683
    "PGM YUV pipe format",
684
    "",
685
    "pgm",
686
    CODEC_ID_NONE,
687
    CODEC_ID_RAWVIDEO,
688
    img_write_header,
689
    img_write_packet,
690
    img_write_trailer,
691

  
692
    img_read_header,
693
    img_read_packet,
694
    img_read_close,
695
    NULL,
696
};
697

  
698
AVFormat ppmpipe_format = {
699
    "ppmpipe",
700
    "PPM pipe format",
701
    "",
702
    "ppm",
703
    CODEC_ID_NONE,
704
    CODEC_ID_RAWVIDEO,
705
    img_write_header,
706
    img_write_packet,
707
    img_write_trailer,
708

  
709
    img_read_header,
710
    img_read_packet,
711
    img_read_close,
712
    NULL,
713
};

Also available in: Unified diff