00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include "internal.h"
00024 #include "pnm.h"
00025
00026
00027 static int pnm_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00028 const AVFrame *pict, int *got_packet)
00029 {
00030 PNMContext *s = avctx->priv_data;
00031 AVFrame * const p = &s->picture;
00032 int i, h, h1, c, n, linesize, ret;
00033 uint8_t *ptr, *ptr1, *ptr2;
00034
00035 if ((ret = ff_alloc_packet2(avctx, pkt, avpicture_get_size(avctx->pix_fmt,
00036 avctx->width,
00037 avctx->height) + 200)) < 0)
00038 return ret;
00039
00040 *p = *pict;
00041 p->pict_type = AV_PICTURE_TYPE_I;
00042 p->key_frame = 1;
00043
00044 s->bytestream_start =
00045 s->bytestream = pkt->data;
00046 s->bytestream_end = pkt->data + pkt->size;
00047
00048 h = avctx->height;
00049 h1 = h;
00050 switch (avctx->pix_fmt) {
00051 case PIX_FMT_MONOWHITE:
00052 c = '4';
00053 n = (avctx->width + 7) >> 3;
00054 break;
00055 case PIX_FMT_GRAY8:
00056 c = '5';
00057 n = avctx->width;
00058 break;
00059 case PIX_FMT_GRAY16BE:
00060 c = '5';
00061 n = avctx->width * 2;
00062 break;
00063 case PIX_FMT_RGB24:
00064 c = '6';
00065 n = avctx->width * 3;
00066 break;
00067 case PIX_FMT_RGB48BE:
00068 c = '6';
00069 n = avctx->width * 6;
00070 break;
00071 case PIX_FMT_YUV420P:
00072 if (avctx->width & 1) {
00073 av_log(avctx, AV_LOG_ERROR, "pgmyuv needs even width\n");
00074 return AVERROR(EINVAL);
00075 }
00076 c = '5';
00077 n = avctx->width;
00078 h1 = (h * 3) / 2;
00079 break;
00080 default:
00081 return -1;
00082 }
00083 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
00084 "P%c\n%d %d\n", c, avctx->width, h1);
00085 s->bytestream += strlen(s->bytestream);
00086 if (avctx->pix_fmt != PIX_FMT_MONOWHITE) {
00087 snprintf(s->bytestream, s->bytestream_end - s->bytestream,
00088 "%d\n", (avctx->pix_fmt != PIX_FMT_GRAY16BE && avctx->pix_fmt != PIX_FMT_RGB48BE) ? 255 : 65535);
00089 s->bytestream += strlen(s->bytestream);
00090 }
00091
00092 ptr = p->data[0];
00093 linesize = p->linesize[0];
00094 for (i = 0; i < h; i++) {
00095 memcpy(s->bytestream, ptr, n);
00096 s->bytestream += n;
00097 ptr += linesize;
00098 }
00099
00100 if (avctx->pix_fmt == PIX_FMT_YUV420P) {
00101 h >>= 1;
00102 n >>= 1;
00103 ptr1 = p->data[1];
00104 ptr2 = p->data[2];
00105 for (i = 0; i < h; i++) {
00106 memcpy(s->bytestream, ptr1, n);
00107 s->bytestream += n;
00108 memcpy(s->bytestream, ptr2, n);
00109 s->bytestream += n;
00110 ptr1 += p->linesize[1];
00111 ptr2 += p->linesize[2];
00112 }
00113 }
00114 pkt->size = s->bytestream - s->bytestream_start;
00115 pkt->flags |= AV_PKT_FLAG_KEY;
00116 *got_packet = 1;
00117
00118 return 0;
00119 }
00120
00121
00122 #if CONFIG_PGM_ENCODER
00123 AVCodec ff_pgm_encoder = {
00124 .name = "pgm",
00125 .type = AVMEDIA_TYPE_VIDEO,
00126 .id = CODEC_ID_PGM,
00127 .priv_data_size = sizeof(PNMContext),
00128 .init = ff_pnm_init,
00129 .encode2 = pnm_encode_frame,
00130 .pix_fmts = (const enum PixelFormat[]){
00131 PIX_FMT_GRAY8, PIX_FMT_GRAY16BE, PIX_FMT_NONE
00132 },
00133 .long_name = NULL_IF_CONFIG_SMALL("PGM (Portable GrayMap) image"),
00134 };
00135 #endif
00136
00137 #if CONFIG_PGMYUV_ENCODER
00138 AVCodec ff_pgmyuv_encoder = {
00139 .name = "pgmyuv",
00140 .type = AVMEDIA_TYPE_VIDEO,
00141 .id = CODEC_ID_PGMYUV,
00142 .priv_data_size = sizeof(PNMContext),
00143 .init = ff_pnm_init,
00144 .encode2 = pnm_encode_frame,
00145 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_YUV420P, PIX_FMT_NONE },
00146 .long_name = NULL_IF_CONFIG_SMALL("PGMYUV (Portable GrayMap YUV) image"),
00147 };
00148 #endif
00149
00150 #if CONFIG_PPM_ENCODER
00151 AVCodec ff_ppm_encoder = {
00152 .name = "ppm",
00153 .type = AVMEDIA_TYPE_VIDEO,
00154 .id = CODEC_ID_PPM,
00155 .priv_data_size = sizeof(PNMContext),
00156 .init = ff_pnm_init,
00157 .encode2 = pnm_encode_frame,
00158 .pix_fmts = (const enum PixelFormat[]){
00159 PIX_FMT_RGB24, PIX_FMT_RGB48BE, PIX_FMT_NONE
00160 },
00161 .long_name = NULL_IF_CONFIG_SMALL("PPM (Portable PixelMap) image"),
00162 };
00163 #endif
00164
00165 #if CONFIG_PBM_ENCODER
00166 AVCodec ff_pbm_encoder = {
00167 .name = "pbm",
00168 .type = AVMEDIA_TYPE_VIDEO,
00169 .id = CODEC_ID_PBM,
00170 .priv_data_size = sizeof(PNMContext),
00171 .init = ff_pnm_init,
00172 .encode2 = pnm_encode_frame,
00173 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_MONOWHITE,
00174 PIX_FMT_NONE },
00175 .long_name = NULL_IF_CONFIG_SMALL("PBM (Portable BitMap) image"),
00176 };
00177 #endif