rename libimagedata to libcinelerradata
[cinelerra_cv/ct.git] / libmpeg3 / mpeg3protos.h
blob027365407c2a98ecd367e7a4d39839cb3e8b8f96
1 #ifndef MPEG3PROTOS_H
2 #define MPEG3PROTOS_H
8 #ifndef CLAMP
9 #define CLAMP(x, y, z) ((x) = ((x) < (y) ? (y) : ((x) > (z) ? (z) : (x))))
10 #endif
13 /* CSS */
15 mpeg3_css_t* mpeg3_new_css();
18 /* Workarounds */
20 int64_t mpeg3io_tell_gcc(mpeg3_fs_t *fs);
21 double mpeg3_add_double_gcc(double x, double y);
22 double mpeg3_divide_double_gcc(double x, double y);
23 int64_t mpeg3_total_bytes_gcc(mpeg3_title_t *title);
24 int64_t mpeg3io_path_total_bytes(char *path);
25 int64_t mpeg3io_get_total_bytes(mpeg3_fs_t *fs);
30 /* TITLE */
32 mpeg3_title_t* mpeg3_new_title(mpeg3_t *file, char *path);
33 void mpeg3_new_cell(mpeg3_title_t *title,
34 int64_t program_start,
35 int64_t program_end,
36 int64_t title_start,
37 int64_t title_end,
38 int program);
39 /* Called by mpeg3_open for a single file */
40 int mpeg3demux_create_title(mpeg3_demuxer_t *demuxer,
41 FILE *toc);
44 /* ATRACK */
46 mpeg3_atrack_t* mpeg3_new_atrack(mpeg3_t *file,
47 int custom_id,
48 int is_ac3,
49 mpeg3_demuxer_t *demuxer,
50 int number);
51 int mpeg3_delete_atrack(mpeg3_t *file, mpeg3_atrack_t *atrack);
53 void mpeg3_append_samples(mpeg3_atrack_t *atrack, int64_t offset);
56 /* These return 1 on failure and 0 on success */
57 int mpeg3_next_header();
59 /* VTRACK */
61 mpeg3_vtrack_t* mpeg3_new_vtrack(mpeg3_t *file,
62 int custom_id,
63 mpeg3_demuxer_t *demuxer,
64 int number);
65 int mpeg3_delete_vtrack(mpeg3_t *file, mpeg3_vtrack_t *vtrack);
67 void mpeg3_append_frame(mpeg3_vtrack_t *vtrack, int64_t offset, int is_keyframe);
71 /* STRACK */
72 mpeg3_strack_t* mpeg3_new_strack(int id);
73 void mpeg3_delete_strack(mpeg3_strack_t *ptr);
74 void mpeg3_copy_strack(mpeg3_strack_t *dst, mpeg3_strack_t *src);
76 /* Get matching subtitle track based on ID or return 0 if it doesn't exist. */
77 mpeg3_strack_t* mpeg3_get_strack_id(mpeg3_t *file, int id);
78 /* get the subtitle track based on number starting from 0 */
79 mpeg3_strack_t* mpeg3_get_strack(mpeg3_t *file, int number);
80 /* Create new subtitle track and add to table in right order. */
81 mpeg3_strack_t* mpeg3_create_strack(mpeg3_t *file, int id);
82 /* Append program offset of a subtitle to the track */
83 void mpeg3_append_subtitle_offset(mpeg3_strack_t *dst, int64_t program_offset);
84 /* Delete a subtitle object */
85 void mpeg3_delete_subtitle(mpeg3_subtitle_t *subtitle);
86 /* Store subtitle object as current subtitle. */
87 /* The object is deleted by the subtitle track. */
88 void mpeg3_append_subtitle(mpeg3_strack_t *strack, mpeg3_subtitle_t *subtitle);
89 /* Get the first subtitle in the track which is not currently being drawn. */
90 mpeg3_subtitle_t* mpeg3_get_subtitle(mpeg3_strack_t *strack);
91 /* Remove the pointer and delete the first subtitle from the track. */
92 void mpeg3_pop_subtitle(mpeg3_strack_t *strack, int number, int delete_it);
93 /* Remove all subtitles from track. */
94 void mpeg3_pop_all_subtitles(mpeg3_strack_t *strack);
95 /* Remove all subtitles from all buffers */
96 void mpeg3_reset_subtitles(mpeg3_t *file);
101 /* AUDIO */
102 mpeg3audio_t* mpeg3audio_new(mpeg3_t *file,
103 mpeg3_atrack_t *track,
104 int is_ac3);
105 int mpeg3audio_delete(mpeg3audio_t *audio);
107 /* Decode up to requested number of samples. */
108 /* Return 0 on success and 1 on failure. */
109 int mpeg3audio_decode_audio(mpeg3audio_t *audio,
110 float *output_f,
111 short *output_i,
112 int channel,
113 int len);
115 /* Shift the audio by the number of samples */
116 /* Used by table of contents routines and decode_audio */
117 void mpeg3_shift_audio(mpeg3audio_t *audio, int diff);
120 /* Audio consists of many possible formats, each packetized into frames. */
121 /* Each format has a constructor, header decoder, frame decoder, and destructor. */
122 /* The function set is determined by the audio format. */
124 /* To decode audio, for each frame read the header sized number of bytes. */
125 /* Call the header decoder for the format. */
126 /* Call the frame decoder for the format. */
128 int mpeg3_new_decode_tables(mpeg3_layer_t *audio);
129 int mpeg3_init_layer3(mpeg3_layer_t *audio);
130 int mpeg3_init_layer2(mpeg3_layer_t *audio);
132 /* Create a new layer decoder */
133 mpeg3_layer_t* mpeg3_new_layer();
135 /* Create a new ac3 decoder */
136 mpeg3_ac3_t* mpeg3_new_ac3();
138 /* Create a new pcm decoder */
139 mpeg3_pcm_t* mpeg3_new_pcm();
142 /* Delete a new layer decoder */
143 void mpeg3_delete_layer(mpeg3_layer_t *audio);
145 /* Delete a new ac3 decoder */
146 void mpeg3_delete_ac3(mpeg3_ac3_t *audio);
148 /* Delete a new pcm decoder */
149 void mpeg3_delete_pcm(mpeg3_pcm_t *audio);
152 /* Return 1 if the data isn't a header */
153 int mpeg3_layer_check(unsigned char *data);
154 int mpeg3_ac3_check(unsigned char *header);
155 int mpeg3_pcm_check(unsigned char *header);
157 /* These return the size of the next frame including the header */
158 /* or 0 if it wasn't a header. */
159 /* Decode a layer header */
160 int mpeg3_layer_header(mpeg3_layer_t *audio, unsigned char *data);
163 /* Decode an AC3 header */
164 int mpeg3_ac3_header(mpeg3_ac3_t *audio, unsigned char *header);
166 /* Decode an PCM header */
167 int mpeg3_pcm_header(mpeg3_pcm_t *audio, unsigned char *header);
170 /* Reset after a seek */
171 void mpeg3_layer_reset(mpeg3_layer_t *audio);
173 /* Decode a frame of layer 3 audio. */
174 /* The output is linear, one buffer for every channel. */
175 /* The user should get the channel count from one of the header commands */
176 /* The output must be big enough to hold the largest frame of audio */
177 /* These functions return the number of samples rendered */
178 int mpeg3audio_dolayer3(mpeg3_layer_t *audio,
179 char *frame,
180 int frame_size,
181 float **output,
182 int render);
184 /* Decode a frame of layer 2 audio */
185 int mpeg3audio_dolayer2(mpeg3_layer_t *audio,
186 char *frame,
187 int frame_size,
188 float **output,
189 int render);
191 /* Decode a frame of ac3 audio */
192 int mpeg3audio_doac3(mpeg3_ac3_t *audio,
193 char *frame,
194 int frame_size,
195 float **output,
196 int render);
199 /* Decode a frame of ac3 audio */
200 int mpeg3audio_dopcm(mpeg3_pcm_t *audio,
201 char *frame,
202 int frame_size,
203 float **output,
204 int render);
217 /* VIDEO */
218 mpeg3video_t* mpeg3video_new(mpeg3_t *file,
219 mpeg3_vtrack_t *track);
220 int mpeg3video_delete(mpeg3video_t *video);
221 int mpeg3video_read_frame(mpeg3video_t *video,
222 unsigned char **output_rows,
223 int in_x,
224 int in_y,
225 int in_w,
226 int in_h,
227 int out_w,
228 int out_h,
229 int color_model);
230 void mpeg3video_dump(mpeg3video_t *video);
231 int mpeg3video_prev_code(mpeg3_demuxer_t *demuxer, unsigned int code);
232 int mpeg3video_next_code(mpeg3_bits_t* stream, unsigned int code);
233 void mpeg3video_toc_error();
234 int mpeg3_rewind_video(mpeg3video_t *video);
235 int mpeg3_read_yuvframe_ptr(mpeg3_t *file,
236 char **y_output,
237 char **u_output,
238 char **v_output,
239 int stream);
240 int mpeg3_read_yuvframe(mpeg3_t *file,
241 char *y_output,
242 char *u_output,
243 char *v_output,
244 int in_x,
245 int in_y,
246 int in_w,
247 int in_h,
248 int stream);
249 // cache_it - store dropped frames in cache
250 int mpeg3video_drop_frames(mpeg3video_t *video, long frames, int cache_it);
251 void mpeg3_decode_subtitle(mpeg3video_t *video);
264 /* FRAME CACHING */
265 mpeg3_cache_t* mpeg3_new_cache();
266 void mpeg3_delete_cache(mpeg3_cache_t *ptr);
267 void mpeg3_reset_cache(mpeg3_cache_t *ptr);
268 void mpeg3_cache_put_frame(mpeg3_cache_t *ptr,
269 int64_t frame_number,
270 unsigned char *y,
271 unsigned char *u,
272 unsigned char *v,
273 int y_size,
274 int u_size,
275 int v_size);
276 // Return 1 if the frame was found.
277 int mpeg3_cache_get_frame(mpeg3_cache_t *ptr,
278 int64_t frame_number,
279 unsigned char **y,
280 unsigned char **u,
281 unsigned char **v);
282 int mpeg3_ceche_has_frame(mpeg3_cache_t *ptr,
283 int64_t frame_number);
284 int64_t mpeg3_cache_usage(mpeg3_cache_t *ptr);
296 /* FILESYSTEM */
298 mpeg3_fs_t* mpeg3_new_fs(char *path);
299 int mpeg3_delete_fs(mpeg3_fs_t *fs);
300 int mpeg3io_open_file(mpeg3_fs_t *fs);
301 int mpeg3io_close_file(mpeg3_fs_t *fs);
302 int mpeg3io_seek(mpeg3_fs_t *fs, int64_t byte);
303 int mpeg3io_seek_relative(mpeg3_fs_t *fs, int64_t bytes);
304 int mpeg3io_read_data(unsigned char *buffer, int64_t bytes, mpeg3_fs_t *fs);
311 /* MAIN */
312 mpeg3_t* mpeg3_new(char *path);
313 mpeg3_index_t* mpeg3_new_index();
314 void mpeg3_delete_index(mpeg3_index_t *index);
315 int mpeg3_delete(mpeg3_t *file);
316 /* Returns 1 on error. */
317 /* Returns 2 if TOC is wrong version. */
318 int mpeg3_get_file_type(mpeg3_t *file,
319 mpeg3_t *old_file,
320 int *toc_atracks,
321 int *toc_vtracks);
323 int mpeg3_read_toc(mpeg3_t *file, int *atracks_return, int *vtracks_return);
335 /* DEMUXER */
339 mpeg3_demuxer_t* mpeg3_new_demuxer(mpeg3_t *file,
340 int do_audio,
341 int do_video,
342 int custom_id);
343 int mpeg3_delete_demuxer(mpeg3_demuxer_t *demuxer);
344 mpeg3_demuxer_t* mpeg3_get_demuxer(mpeg3_t *file);
345 int mpeg3demux_read_data(mpeg3_demuxer_t *demuxer,
346 unsigned char *output,
347 int size);
349 /* Append elementary stream data */
350 /* Used by streaming mode. */
351 void mpeg3demux_append_data(mpeg3_demuxer_t *demuxer,
352 unsigned char *data,
353 /* Bytes of data */
354 int bytes);
356 /* Shift elementary data out */
357 /* Used by streaming mode */
358 void mpeg3demux_shift_data(mpeg3_demuxer_t *demuxer,
359 int bytes);
361 /* Convert absolute byte position to position in program */
362 int64_t mpeg3_absolute_to_program(mpeg3_demuxer_t *demuxer,
363 int64_t absolute_byte);
366 int mpeg3_read_next_packet(mpeg3_demuxer_t *demuxer);
367 int mpeg3_read_prev_packet(mpeg3_demuxer_t *demuxer);
370 unsigned int mpeg3demux_read_int32(mpeg3_demuxer_t *demuxer);
371 unsigned int mpeg3demux_read_int24(mpeg3_demuxer_t *demuxer);
372 unsigned int mpeg3demux_read_int16(mpeg3_demuxer_t *demuxer);
375 /* Give total number of bytes in all titles which belong to the current program. */
376 int64_t mpeg3demux_movie_size(mpeg3_demuxer_t *demuxer);
378 /* Give byte offset relative to start of movie */
379 int64_t mpeg3demux_tell_byte(mpeg3_demuxer_t *demuxer);
381 /* Give program the current packet belongs to */
382 int mpeg3demux_tell_program(mpeg3_demuxer_t *demuxer);
384 double mpeg3demux_get_time(mpeg3_demuxer_t *demuxer);
385 int mpeg3demux_eof(mpeg3_demuxer_t *demuxer);
386 int mpeg3demux_bof(mpeg3_demuxer_t *demuxer);
387 void mpeg3demux_start_reverse(mpeg3_demuxer_t *demuxer);
388 void mpeg3demux_start_forward(mpeg3_demuxer_t *demuxer);
389 int mpeg3demux_open_title(mpeg3_demuxer_t *demuxer, int title_number);
390 /* Go to the absolute byte given */
391 int mpeg3demux_seek_byte(mpeg3_demuxer_t *demuxer, int64_t byte);
393 /* Seek to the title and cell containing the absolute byte of the
394 /* demuxer. */
395 /* Called at the beginning of every packet. */
396 int mpeg3_seek_phys(mpeg3_demuxer_t *demuxer);
398 unsigned char mpeg3demux_read_char_packet(mpeg3_demuxer_t *demuxer);
399 unsigned char mpeg3demux_read_prev_char_packet(mpeg3_demuxer_t *demuxer);
400 int mpeg3demux_read_program(mpeg3_demuxer_t *demuxer);
402 /* Get last pts read */
403 double mpeg3demux_audio_pts(mpeg3_demuxer_t *demuxer);
405 double mpeg3demux_video_pts(mpeg3_demuxer_t *demuxer);
407 /* Set the last pts read to -1 for audio and video */
408 void mpeg3demux_reset_pts(mpeg3_demuxer_t *demuxer);
410 /* scan forward for next pts. Used in byte seeking to synchronize */
411 double mpeg3demux_scan_pts(mpeg3_demuxer_t *demuxer);
413 /* seek using sequential search to the pts given. Used in byte seeking. */
414 int mpeg3demux_goto_pts(mpeg3_demuxer_t *demuxer, double pts);
416 /* Get number of finished subtitles in the table matching id. */
417 /* If id = -1, get all finished subtitles. */
418 int mpeg3_finished_subtitles(mpeg3_demuxer_t *demuxer, int id);
420 unsigned char mpeg3demux_read_char_packet(mpeg3_demuxer_t *demuxer);
421 unsigned char mpeg3demux_read_prev_char_packet(mpeg3_demuxer_t *demuxer);
423 #define mpeg3demux_error(demuxer) (((mpeg3_demuxer_t *)(demuxer))->error_flag)
425 static unsigned char mpeg3demux_read_char(mpeg3_demuxer_t *demuxer)
427 //printf("mpeg3demux_read_char %lx %lx\n", demuxer->data_position, demuxer->data_size);
428 if(demuxer->data_position < demuxer->data_size)
430 return demuxer->data_buffer[demuxer->data_position++];
432 else
434 return mpeg3demux_read_char_packet(demuxer);
440 static unsigned char mpeg3demux_read_prev_char(mpeg3_demuxer_t *demuxer)
442 if(demuxer->data_position != 0)
444 return demuxer->data_buffer[demuxer->data_position--];
446 else
448 return mpeg3demux_read_prev_char_packet(demuxer);
464 // Bitstream
466 mpeg3_bits_t* mpeg3bits_new_stream(mpeg3_t *file, mpeg3_demuxer_t *demuxer);
467 int mpeg3bits_delete_stream(mpeg3_bits_t* stream);
468 int mpeg3bits_seek_byte(mpeg3_bits_t* stream, int64_t position);
469 int mpeg3bits_open_title(mpeg3_bits_t* stream, int title);
470 /* Give absolute byte offset in all titles. */
471 int64_t mpeg3bits_tell(mpeg3_bits_t* stream);
472 /* Reset bit bucket */
473 void mpeg3bits_reset(mpeg3_bits_t *stream);
487 #define mpeg3bits_error(stream) mpeg3demux_error((stream)->demuxer)
489 #define mpeg3bits_eof(stream) mpeg3demux_eof((stream)->demuxer)
491 #define mpeg3bits_bof(stream) mpeg3demux_bof((stream)->demuxer)
493 /* Read bytes backward from the file until the reverse_bits is full. */
494 static void mpeg3bits_fill_reverse_bits(mpeg3_bits_t* stream, int bits)
496 // Right justify
497 while(stream->bit_number > 7)
499 stream->bfr >>= 8;
500 stream->bfr_size -= 8;
501 stream->bit_number -= 8;
504 // Insert bytes before bfr_size
505 while(stream->bfr_size - stream->bit_number < bits)
507 if(stream->input_ptr)
508 stream->bfr |= (unsigned int)(*--stream->input_ptr) << stream->bfr_size;
509 else
510 stream->bfr |= (unsigned int)mpeg3demux_read_prev_char(stream->demuxer) << stream->bfr_size;
511 stream->bfr_size += 8;
515 /* Read bytes forward from the file until the forward_bits is full. */
516 static void mpeg3bits_fill_bits(mpeg3_bits_t* stream, int bits)
518 while(stream->bit_number < bits)
520 stream->bfr <<= 8;
521 if(stream->input_ptr)
523 stream->bfr |= *stream->input_ptr++;
525 else
527 stream->bfr |= mpeg3demux_read_char(stream->demuxer);
529 stream->bit_number += 8;
530 stream->bfr_size += 8;
531 if(stream->bfr_size > 32) stream->bfr_size = 32;
535 /* Return 8 bits, advancing the file position. */
536 static unsigned int mpeg3bits_getbyte_noptr(mpeg3_bits_t* stream)
538 if(stream->bit_number < 8)
540 stream->bfr <<= 8;
541 if(stream->input_ptr)
542 stream->bfr |= *stream->input_ptr++;
543 else
544 stream->bfr |= mpeg3demux_read_char(stream->demuxer);
546 stream->bfr_size += 8;
547 if(stream->bfr_size > 32) stream->bfr_size = 32;
549 return (stream->bfr >> stream->bit_number) & 0xff;
551 return (stream->bfr >> (stream->bit_number -= 8)) & 0xff;
554 static unsigned int mpeg3bits_getbit_noptr(mpeg3_bits_t* stream)
556 if(!stream->bit_number)
558 stream->bfr <<= 8;
559 stream->bfr |= mpeg3demux_read_char(stream->demuxer);
561 stream->bfr_size += 8;
562 if(stream->bfr_size > 32) stream->bfr_size = 32;
564 stream->bit_number = 7;
566 return (stream->bfr >> 7) & 0x1;
568 return (stream->bfr >> (--stream->bit_number)) & (0x1);
571 /* Return n number of bits, advancing the file position. */
572 /* Use in place of flushbits */
573 static unsigned int mpeg3bits_getbits(mpeg3_bits_t* stream, int bits)
575 if(bits <= 0) return 0;
576 mpeg3bits_fill_bits(stream, bits);
577 return (stream->bfr >> (stream->bit_number -= bits)) & (0xffffffff >> (32 - bits));
580 static unsigned int mpeg3bits_showbits24_noptr(mpeg3_bits_t* stream)
582 while(stream->bit_number < 24)
584 stream->bfr <<= 8;
585 stream->bfr |= mpeg3demux_read_char(stream->demuxer);
586 stream->bit_number += 8;
587 stream->bfr_size += 8;
588 if(stream->bfr_size > 32) stream->bfr_size = 32;
590 return (stream->bfr >> (stream->bit_number - 24)) & 0xffffff;
593 static unsigned int mpeg3bits_showbits32_noptr(mpeg3_bits_t* stream)
595 while(stream->bit_number < 32)
597 stream->bfr <<= 8;
598 stream->bfr |= mpeg3demux_read_char(stream->demuxer);
599 stream->bit_number += 8;
600 stream->bfr_size += 8;
601 if(stream->bfr_size > 32) stream->bfr_size = 32;
603 return stream->bfr;
606 static unsigned int mpeg3bits_showbits(mpeg3_bits_t* stream, int bits)
608 mpeg3bits_fill_bits(stream, bits);
609 return (stream->bfr >> (stream->bit_number - bits)) & (0xffffffff >> (32 - bits));
612 static unsigned int mpeg3bits_getbits_reverse(mpeg3_bits_t* stream, int bits)
614 unsigned int result;
615 mpeg3bits_fill_reverse_bits(stream, bits);
616 result = (stream->bfr >> stream->bit_number) & (0xffffffff >> (32 - bits));
617 stream->bit_number += bits;
618 return result;
621 static unsigned int mpeg3bits_showbits_reverse(mpeg3_bits_t* stream, int bits)
623 unsigned int result;
624 mpeg3bits_fill_reverse_bits(stream, bits);
625 result = (stream->bfr >> stream->bit_number) & (0xffffffff >> (32 - bits));
626 return result;
637 // I/O
638 // I/O must be character based so the buffer doesn't get overrun
646 void mpeg3io_read_buffer(mpeg3_fs_t *fs);
648 #define mpeg3io_tell(fs) (((mpeg3_fs_t *)(fs))->current_byte)
650 // End of file
651 #define mpeg3io_eof(fs) (((mpeg3_fs_t *)(fs))->current_byte >= ((mpeg3_fs_t *)(fs))->total_bytes)
653 // Beginning of file
654 #define mpeg3io_bof(fs) (((mpeg3_fs_t *)(fs))->current_byte < 0)
656 #define mpeg3io_get_fd(fs) (fileno(((mpeg3_fs_t *)(fs))->fd))
658 #define mpeg3io_total_bytes(fs) (((mpeg3_fs_t *)(fs))->total_bytes)
660 static int mpeg3io_sync_buffer(mpeg3_fs_t *fs)
662 // Reposition buffer offset
663 if(fs->buffer_position + fs->buffer_offset != fs->current_byte)
665 fs->buffer_offset = fs->current_byte - fs->buffer_position;
668 // Load new buffer
669 if(fs->current_byte < fs->buffer_position ||
670 fs->current_byte >= fs->buffer_position + fs->buffer_size)
672 mpeg3io_read_buffer(fs);
675 return !fs->buffer_size;
678 static unsigned int mpeg3io_read_char(mpeg3_fs_t *fs)
680 unsigned int result;
681 mpeg3io_sync_buffer(fs);
682 result = fs->buffer[fs->buffer_offset++];
683 fs->current_byte++;
684 return result;
687 static unsigned char mpeg3io_next_char(mpeg3_fs_t *fs)
689 unsigned char result;
690 mpeg3io_sync_buffer(fs);
691 result = fs->buffer[fs->buffer_offset];
692 return result;
695 static uint32_t mpeg3io_read_int32(mpeg3_fs_t *fs)
697 int a, b, c, d;
698 uint32_t result;
699 /* Do not fread. This breaks byte ordering. */
700 a = mpeg3io_read_char(fs);
701 b = mpeg3io_read_char(fs);
702 c = mpeg3io_read_char(fs);
703 d = mpeg3io_read_char(fs);
704 result = (a << 24) |
705 (b << 16) |
706 (c << 8) |
707 (d);
708 return result;
711 static uint32_t mpeg3io_read_int24(mpeg3_fs_t *fs)
713 int b, c, d;
714 uint32_t result;
715 /* Do not fread. This breaks byte ordering. */
716 b = mpeg3io_read_char(fs);
717 c = mpeg3io_read_char(fs);
718 d = mpeg3io_read_char(fs);
719 result = (b << 16) |
720 (c << 8) |
721 (d);
722 return result;
725 static uint16_t mpeg3io_read_int16(mpeg3_fs_t *fs)
727 int c, d;
728 uint16_t result;
729 /* Do not fread. This breaks byte ordering. */
730 c = mpeg3io_read_char(fs);
731 d = mpeg3io_read_char(fs);
732 result = (c << 8) |
733 (d);
734 return result;
745 // More bitstream
755 #define mpeg3slice_fillbits(buffer, nbits) \
756 while(((mpeg3_slice_buffer_t*)(buffer))->bits_size < (nbits)) \
758 if(((mpeg3_slice_buffer_t*)(buffer))->current_position < ((mpeg3_slice_buffer_t*)(buffer))->buffer_size) \
760 ((mpeg3_slice_buffer_t*)(buffer))->bits <<= 8; \
761 ((mpeg3_slice_buffer_t*)(buffer))->bits |= ((mpeg3_slice_buffer_t*)(buffer))->data[((mpeg3_slice_buffer_t*)(buffer))->current_position++]; \
763 ((mpeg3_slice_buffer_t*)(buffer))->bits_size += 8; \
766 #define mpeg3slice_flushbits(buffer, nbits) \
768 mpeg3slice_fillbits((buffer), (nbits)); \
769 ((mpeg3_slice_buffer_t*)(buffer))->bits_size -= (nbits); \
772 #define mpeg3slice_flushbit(buffer) \
774 if(((mpeg3_slice_buffer_t*)(buffer))->bits_size) \
775 ((mpeg3_slice_buffer_t*)(buffer))->bits_size--; \
776 else \
777 if(((mpeg3_slice_buffer_t*)(buffer))->current_position < ((mpeg3_slice_buffer_t*)(buffer))->buffer_size) \
779 ((mpeg3_slice_buffer_t*)(buffer))->bits = \
780 ((mpeg3_slice_buffer_t*)(buffer))->data[((mpeg3_slice_buffer_t*)(buffer))->current_position++]; \
781 ((mpeg3_slice_buffer_t*)(buffer))->bits_size = 7; \
785 static unsigned int mpeg3slice_getbit(mpeg3_slice_buffer_t *buffer)
787 if(buffer->bits_size)
788 return (buffer->bits >> (--buffer->bits_size)) & 0x1;
789 else
790 if(buffer->current_position < buffer->buffer_size)
792 buffer->bits = buffer->data[buffer->current_position++];
793 buffer->bits_size = 7;
794 return (buffer->bits >> 7) & 0x1;
796 return 0;
800 static unsigned int mpeg3slice_getbits2(mpeg3_slice_buffer_t *buffer)
802 if(buffer->bits_size >= 2)
803 return (buffer->bits >> (buffer->bits_size -= 2)) & 0x3;
804 else
805 if(buffer->current_position < buffer->buffer_size)
807 buffer->bits <<= 8;
808 buffer->bits |= buffer->data[buffer->current_position++];
809 buffer->bits_size += 6;
810 return (buffer->bits >> buffer->bits_size) & 0x3;
812 return 0;
815 static unsigned int mpeg3slice_getbyte(mpeg3_slice_buffer_t *buffer)
817 if(buffer->bits_size >= 8)
818 return (buffer->bits >> (buffer->bits_size -= 8)) & 0xff;
819 else
820 if(buffer->current_position < buffer->buffer_size)
822 buffer->bits <<= 8;
823 buffer->bits |= buffer->data[buffer->current_position++];
824 return (buffer->bits >> buffer->bits_size) & 0xff;
826 return 0;
830 static unsigned int mpeg3slice_getbits(mpeg3_slice_buffer_t *slice_buffer, int bits)
832 if(bits == 1) return mpeg3slice_getbit(slice_buffer);
833 mpeg3slice_fillbits(slice_buffer, bits);
834 return (slice_buffer->bits >> (slice_buffer->bits_size -= bits)) & (0xffffffff >> (32 - bits));
837 static unsigned int mpeg3slice_showbits16(mpeg3_slice_buffer_t *buffer)
839 if(buffer->bits_size >= 16)
840 return (buffer->bits >> (buffer->bits_size - 16)) & 0xffff;
841 else
842 if(buffer->current_position < buffer->buffer_size)
844 buffer->bits <<= 16;
845 buffer->bits_size += 16;
846 buffer->bits |= (unsigned int)buffer->data[buffer->current_position++] << 8;
847 buffer->bits |= buffer->data[buffer->current_position++];
848 return (buffer->bits >> (buffer->bits_size - 16)) & 0xffff;
850 return 0;
853 static unsigned int mpeg3slice_showbits9(mpeg3_slice_buffer_t *buffer)
855 if(buffer->bits_size >= 9)
856 return (buffer->bits >> (buffer->bits_size - 9)) & 0x1ff;
857 else
858 if(buffer->current_position < buffer->buffer_size)
860 buffer->bits <<= 16;
861 buffer->bits_size += 16;
862 buffer->bits |= (unsigned int)buffer->data[buffer->current_position++] << 8;
863 buffer->bits |= buffer->data[buffer->current_position++];
864 return (buffer->bits >> (buffer->bits_size - 9)) & 0x1ff;
866 return 0;
869 static unsigned int mpeg3slice_showbits5(mpeg3_slice_buffer_t *buffer)
871 if(buffer->bits_size >= 5)
872 return (buffer->bits >> (buffer->bits_size - 5)) & 0x1f;
873 else
874 if(buffer->current_position < buffer->buffer_size)
876 buffer->bits <<= 8;
877 buffer->bits_size += 8;
878 buffer->bits |= buffer->data[buffer->current_position++];
879 return (buffer->bits >> (buffer->bits_size - 5)) & 0x1f;
881 return 0;
884 static unsigned int mpeg3slice_showbits(mpeg3_slice_buffer_t *slice_buffer, int bits)
886 mpeg3slice_fillbits(slice_buffer, bits);
887 return (slice_buffer->bits >> (slice_buffer->bits_size - bits)) & (0xffffffff >> (32 - bits));
895 #endif