33
33
#include "avcodec.h"
34
34
#include "dsputil.h"
35
36
#include "mpegvideo.h"
37
38
#include "mjpegenc.h"
40
static int encode_picture_lossless(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data){
41
static int encode_picture_lossless(AVCodecContext *avctx, AVPacket *pkt,
42
const AVFrame *pict, int *got_packet)
41
44
MpegEncContext * const s = avctx->priv_data;
42
45
MJpegContext * const m = s->mjpeg_ctx;
44
46
const int width= s->width;
45
47
const int height= s->height;
46
AVFrame * const p= (AVFrame*)&s->current_picture;
48
AVFrame * const p = &s->current_picture.f;
47
49
const int predictor= avctx->prediction_method+1;
49
init_put_bits(&s->pb, buf, buf_size);
50
const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
51
const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
52
int ret, max_pkt_size = FF_MIN_BUFFER_SIZE;
54
if (avctx->pix_fmt == AV_PIX_FMT_BGRA)
55
max_pkt_size += width * height * 3 * 4;
57
max_pkt_size += mb_width * mb_height * 3 * 4
58
* s->mjpeg_hsample[0] * s->mjpeg_vsample[0];
60
if ((ret = ff_alloc_packet(pkt, max_pkt_size)) < 0) {
61
av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", max_pkt_size);
65
init_put_bits(&s->pb, pkt->data, pkt->size);
52
68
p->pict_type= AV_PICTURE_TYPE_I;
57
73
s->header_bits= put_bits_count(&s->pb);
59
if(avctx->pix_fmt == PIX_FMT_BGRA){
75
if(avctx->pix_fmt == AV_PIX_FMT_BGRA){
61
77
const int linesize= p->linesize[0];
62
78
uint16_t (*buffer)[4]= (void *) s->rd_scratchpad;
106
122
int mb_x, mb_y, i;
107
const int mb_width = (width + s->mjpeg_hsample[0] - 1) / s->mjpeg_hsample[0];
108
const int mb_height = (height + s->mjpeg_vsample[0] - 1) / s->mjpeg_vsample[0];
110
124
for(mb_y = 0; mb_y < mb_height; mb_y++) {
111
125
if(s->pb.buf_end - s->pb.buf - (put_bits_count(&s->pb)>>3) < mb_width * 4 * 3 * s->mjpeg_hsample[0] * s->mjpeg_vsample[0]){
162
176
ptr = p->data[i] + (linesize * (v * mb_y + y)) + (h * mb_x + x); //FIXME optimize this crap
163
//printf("%d %d %d %d %8X\n", mb_x, mb_y, x, y, ptr);
164
177
PREDICT(pred, ptr[-linesize-1], ptr[-linesize], ptr[-1], predictor);
189
206
AVCodec ff_ljpeg_encoder = { //FIXME avoid MPV_* lossless JPEG should not need them
191
208
.type = AVMEDIA_TYPE_VIDEO,
192
.id = CODEC_ID_LJPEG,
209
.id = AV_CODEC_ID_LJPEG,
193
210
.priv_data_size = sizeof(MpegEncContext),
194
.init = MPV_encode_init,
195
.encode = encode_picture_lossless,
196
.close = MPV_encode_end,
211
.init = ff_MPV_encode_init,
212
.encode2 = encode_picture_lossless,
213
.close = ff_MPV_encode_end,
197
214
.long_name = NULL_IF_CONFIG_SMALL("Lossless JPEG"),