📄 img.c
字号:
if (i == 1) { width >>= 1; height >>= 1; } p[1] = ext[i]; if (url_fopen(pb, fname, URL_WRONLY) < 0) return -EIO; ptr = picture->data[i]; for(j=0;j<height;j++) { put_buffer(pb, ptr, width); ptr += picture->linesize[i]; } put_flush_packet(pb); url_fclose(pb); } return 0;}static int yuv4mpeg_save(AVPicture *picture, int width, int height, ByteIOContext *pb, int need_stream_header, int is_yuv, int raten, int rated, int aspectn, int aspectd) { int i, n, m; char buf[Y4M_LINE_MAX+1], buf1[20]; UINT8 *ptr, *ptr1, *ptr2; /* construct stream header, if this is the first frame */ if(need_stream_header) { n = snprintf(buf, sizeof(buf), "%s W%d H%d F%d:%d I%s A%d:%d\n", Y4M_MAGIC, width, height, raten, rated, "p", /* ffmpeg seems to only output progressive video */ aspectn, aspectd); if (n < 0) { fprintf(stderr, "Error. YUV4MPEG stream header write failed.\n"); } else { fprintf(stderr, "YUV4MPEG stream header written. FPS is %d\n", raten); put_buffer(pb, buf, strlen(buf)); } } /* construct frame header */ m = snprintf(buf1, sizeof(buf1), "%s \n", Y4M_FRAME_MAGIC); if (m < 0) { fprintf(stderr, "Error. YUV4MPEG frame header write failed.\n"); } else { /* fprintf(stderr, "YUV4MPEG frame header written.\n"); */ put_buffer(pb, buf1, strlen(buf1)); } ptr = picture->data[0]; for(i=0;i<height;i++) { put_buffer(pb, ptr, width); ptr += picture->linesize[0]; } if (is_yuv) { height >>= 1; width >>= 1; ptr1 = picture->data[1]; ptr2 = picture->data[2]; for(i=0;i<height;i++) { /* Cb */ put_buffer(pb, ptr1, width); ptr1 += picture->linesize[1]; } for(i=0;i<height;i++) { /* Cr */ put_buffer(pb, ptr2, width); ptr2 += picture->linesize[2]; } } put_flush_packet(pb); return 0;}static int img_write_header(AVFormatContext *s){ VideoData *img = s->priv_data; img->img_number = 1; strcpy(img->path, s->filename); /* find format */ if (s->oformat->flags & AVFMT_NOFILE) img->is_pipe = 0; else img->is_pipe = 1; if (s->oformat == &pgmyuvpipe_oformat || s->oformat == &pgmyuv_oformat) { img->img_fmt = IMGFMT_PGMYUV; } else if (s->oformat == &pgmpipe_oformat || s->oformat == &pgm_oformat) { img->img_fmt = IMGFMT_PGM; } else if (s->oformat == &imgyuv_oformat) { img->img_fmt = IMGFMT_YUV; } else if (s->oformat == &ppmpipe_oformat || s->oformat == &ppm_oformat) { img->img_fmt = IMGFMT_PPM; } else if (s->oformat == &yuv4mpegpipe_oformat) { img->img_fmt = IMGFMT_YUV4MPEG; img->header_written = 0; } else { goto fail; } return 0; fail: av_free(img); return -EIO;}static int img_write_packet(AVFormatContext *s, int stream_index, UINT8 *buf, int size, int force_pts){ VideoData *img = s->priv_data; AVStream *st = s->streams[stream_index]; ByteIOContext pb1, *pb; AVPicture picture; int width, height, need_stream_header, ret, size1, raten, rated, aspectn, aspectd, fps, fps1; char filename[1024]; width = st->codec.width; height = st->codec.height; if (img->img_number == 1) { need_stream_header = 1; } else { need_stream_header = 0; } fps = st->codec.frame_rate; fps1 = (((float)fps / FRAME_RATE_BASE) * 1000); /* Sorry about this messy code, but mpeg2enc is very picky about * the framerates it accepts. */ switch(fps1) { case 23976: raten = 24000; /* turn the framerate into a ratio */ rated = 1001; break; case 29970: raten = 30000; rated = 1001; break; case 25000: raten = 25; rated = 1; break; case 30000: raten = 30; rated = 1; break; case 24000: raten = 24; rated = 1; break; case 50000: raten = 50; rated = 1; break; case 59940: raten = 60000; rated = 1001; break; case 60000: raten = 60; rated = 1; break; default: raten = fps1; /* this setting should work, but often doesn't */ rated = 1000; break; } aspectn = 1; aspectd = 1; /* ffmpeg always uses a 1:1 aspect ratio */ switch(st->codec.pix_fmt) { case PIX_FMT_YUV420P: size1 = (width * height * 3) / 2; if (size != size1) return -EIO; picture.data[0] = buf; picture.data[1] = picture.data[0] + width * height; picture.data[2] = picture.data[1] + (width * height) / 4; picture.linesize[0] = width; picture.linesize[1] = width >> 1; picture.linesize[2] = width >> 1; break; case PIX_FMT_RGB24: size1 = (width * height * 3); if (size != size1) return -EIO; picture.data[0] = buf; picture.linesize[0] = width * 3; break; default: return -EIO; } /* This if-statement destroys pipes - I do not see why it is necessary if (get_frame_filename(filename, sizeof(filename), img->path, img->img_number) < 0) return -EIO;*/ get_frame_filename(filename, sizeof(filename), img->path, img->img_number); if (!img->is_pipe) { pb = &pb1; if (url_fopen(pb, filename, URL_WRONLY) < 0) return -EIO; } else { pb = &s->pb; } switch(img->img_fmt) { case IMGFMT_PGMYUV: ret = pgm_save(&picture, width, height, pb, 1); break; case IMGFMT_PGM: ret = pgm_save(&picture, width, height, pb, 0); break; case IMGFMT_YUV: ret = yuv_save(&picture, width, height, filename); break; case IMGFMT_PPM: ret = ppm_save(&picture, width, height, pb); break; case IMGFMT_YUV4MPEG: ret = yuv4mpeg_save(&picture, width, height, pb, need_stream_header, 1, raten, rated, aspectn, aspectd); break; } if (!img->is_pipe) { url_fclose(pb); } img->img_number++; return 0;}static int img_write_trailer(AVFormatContext *s){ return 0;}static AVInputFormat pgm_iformat = { "pgm", "pgm image format", sizeof(VideoData), NULL, img_read_header, img_read_packet, img_read_close, NULL, AVFMT_NOFILE | AVFMT_NEEDNUMBER, .extensions = "pgm",};static AVOutputFormat pgm_oformat = { "pgm", "pgm image format", "", "pgm", sizeof(VideoData), CODEC_ID_NONE, CODEC_ID_RAWVIDEO, img_write_header, img_write_packet, img_write_trailer, AVFMT_NOFILE | AVFMT_NEEDNUMBER,};static AVInputFormat pgmyuv_iformat = { "pgmyuv", "pgm with YUV content image format", sizeof(VideoData), NULL, /* no probe */ img_read_header, img_read_packet, img_read_close, NULL, AVFMT_NOFILE | AVFMT_NEEDNUMBER,};static AVOutputFormat pgmyuv_oformat = { "pgmyuv", "pgm with YUV content image format", "", "pgm", sizeof(VideoData), CODEC_ID_NONE, CODEC_ID_RAWVIDEO, img_write_header, img_write_packet, img_write_trailer, AVFMT_NOFILE | AVFMT_NEEDNUMBER,};static AVInputFormat ppm_iformat = { "ppm", "ppm image format", sizeof(VideoData), NULL, img_read_header, img_read_packet, img_read_close, NULL, AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RGB24, .extensions = "ppm",};static AVOutputFormat ppm_oformat = { "ppm", "ppm image format", "", "ppm", sizeof(VideoData), CODEC_ID_NONE, CODEC_ID_RAWVIDEO, img_write_header, img_write_packet, img_write_trailer, AVFMT_NOFILE | AVFMT_NEEDNUMBER | AVFMT_RGB24,};static AVInputFormat imgyuv_iformat = { ".Y.U.V", ".Y.U.V format", sizeof(VideoData), NULL, img_read_header, img_read_packet, img_read_close, NULL, AVFMT_NOFILE | AVFMT_NEEDNUMBER, .extensions = "Y",};static AVOutputFormat imgyuv_oformat = { ".Y.U.V", ".Y.U.V format", "", "Y", sizeof(VideoData), CODEC_ID_NONE, CODEC_ID_RAWVIDEO, img_write_header, img_write_packet, img_write_trailer, AVFMT_NOFILE | AVFMT_NEEDNUMBER,};static AVInputFormat pgmpipe_iformat = { "pgmpipe", "PGM pipe format", sizeof(VideoData), NULL, /* no probe */ img_read_header, img_read_packet, img_read_close, NULL,};static AVOutputFormat pgmpipe_oformat = { "pgmpipe", "PGM pipe format", "", "pgm", sizeof(VideoData), CODEC_ID_NONE, CODEC_ID_RAWVIDEO, img_write_header, img_write_packet, img_write_trailer,};static AVInputFormat pgmyuvpipe_iformat = { "pgmyuvpipe", "PGM YUV pipe format", sizeof(VideoData), NULL, /* no probe */ img_read_header, img_read_packet, img_read_close, NULL,};static AVOutputFormat pgmyuvpipe_oformat = { "pgmyuvpipe", "PGM YUV pipe format", "", "pgm", sizeof(VideoData), CODEC_ID_NONE, CODEC_ID_RAWVIDEO, img_write_header, img_write_packet, img_write_trailer,};static AVInputFormat ppmpipe_iformat = { "ppmpipe", "PPM pipe format", sizeof(VideoData), NULL, /* no probe */ img_read_header, img_read_packet, img_read_close, NULL, .flags = AVFMT_RGB24,};static AVOutputFormat ppmpipe_oformat = { "ppmpipe", "PPM pipe format", "", "ppm", sizeof(VideoData), CODEC_ID_NONE, CODEC_ID_RAWVIDEO, img_write_header, img_write_packet, img_write_trailer, .flags = AVFMT_RGB24,};static AVOutputFormat yuv4mpegpipe_oformat = { "yuv4mpegpipe", "YUV4MPEG pipe format", "", "yuv4mpeg", sizeof(VideoData), CODEC_ID_NONE, CODEC_ID_RAWVIDEO, img_write_header, img_write_packet, img_write_trailer,};int img_init(void){ av_register_input_format(&pgm_iformat); av_register_output_format(&pgm_oformat); av_register_input_format(&pgmyuv_iformat); av_register_output_format(&pgmyuv_oformat); av_register_input_format(&ppm_iformat); av_register_output_format(&ppm_oformat); av_register_input_format(&imgyuv_iformat); av_register_output_format(&imgyuv_oformat); av_register_input_format(&pgmpipe_iformat); av_register_output_format(&pgmpipe_oformat); av_register_input_format(&pgmyuvpipe_iformat); av_register_output_format(&pgmyuvpipe_oformat); av_register_input_format(&ppmpipe_iformat); av_register_output_format(&ppmpipe_oformat); av_register_output_format(&yuv4mpegpipe_oformat); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -