summaryrefslogtreecommitdiffstats
path: root/src/file_mpg.c
blob: c0ca71b9bf0e034de988443e35742064a87fd31d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*

    File: file_mpg.c

    Copyright (C) 1998-2005,2007-2009 Christophe GRENIER <grenier@cgsecurity.org>
  
    This software is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
  
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
  
    You should have received a copy of the GNU General Public License along
    with this program; if not, write the Free Software Foundation, Inc., 51
    Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <stdio.h>
#include "types.h"
#include "filegen.h"
#include "log.h"

static void register_header_check_mpg(file_stat_t *file_stat);

const file_hint_t file_hint_mpg= {
  .extension="mpg",
  .description="Moving Picture Experts Group video",
  .max_filesize=PHOTOREC_MAX_FILE_SIZE,
  .recover=1,
  .enable_by_default=1,
  .register_header_check=&register_header_check_mpg
};

static int calculate_packet_size(const unsigned char *buffer)
{
  /* http://dvd.sourceforge.net/dvdinfo/mpeghdrs.html */
  if(buffer[0]!=0 || buffer[1]!=0 || buffer[2]!=1)
    return 0;
  switch(buffer[3])
  {
    /* Pack header: */
    case 0xBA:
      if((buffer[4]&0xc4)==0x44 && (buffer[6]&4)==4 && (buffer[8]&4)==4 &&
	  (buffer[9]&1)==1 && (buffer[12]&3)==3)
	return (buffer[13] & 0x7) + 14;
      if((buffer[4]&0xF1)==0x21 && (buffer[6]&1)==1 && (buffer[8]&1)==1 &&
	  (buffer[9]&0x80)==0x80 && (buffer[11]&1)==1)
	return 12;
      return 0;
      /* Sequence Header */
    case 0xB3:
      if((buffer[10]&0x20)==0x20)
      {
	if((buffer[11]&3)!=0)
	  return 12+64;	/* quantiser matrix */
	return 12;
      }
      return 0;
      /* Extension */
    case 0xB5:
      /* Sequence_Extension */
      if((buffer[4]&0xF0)==0x10 && (buffer[7]&1)==1)
	return 10;
      /* Sequence_Display_Extension without color description */
      if((buffer[4]&0xF0)==0x20 && (buffer[4]&1)==0 && (buffer[6]&2)==2)
	return 9;
      /* Sequence_Display_Extension with color description */
      if((buffer[4]&0xF0)==0x20 && (buffer[4]&1)==1 && (buffer[9]&2)==2)
	return 12;
      /* Picture_Coding_Extension */
      if((buffer[4]&0xF0)==0x40)
      {
	if((buffer[8]&0x40)==0)
	  return 9;
	else
	  return 11;
      }
      return 0;
    case 0xB8: /* Group of Pictures */
      if((buffer[5]&0x40)==0x40)
	return 8;
      return 0;
    case 0xB9: /* EOC */
      return 4;
    case 0xBD:		/* Private stream 1 (non MPEG audio, subpictures) */
    case 0xC0 ... 0xDF: /* Mpeg Audio stream */
    case 0xE0 ... 0xEF: /* Mpeg Video stream */
#if 0
      {
	uint32_t pts = 0;
	// This is mpeg 2:
	if((buffer[6] & 0xC0) == 0x80 &&
	    // PTS DTS flags
	    (buffer[7] >> 7)==1)
	{
	  pts = ((buffer[13] | (buffer[12] << 8) ) >> 1) |
	    ((buffer[11] | (buffer[10] << 8) ) >> 1) << 15;

	  //      log_debug("MPG2 (%u - 0x%02X)PTS is 0x%08X\n", current->id,buffer[3], pts);
	  // This is mpeg 1. The PTS goes right after the header and must
	  // have the bits 0x21 set:
	}
	else if((buffer[6] & 0x21)==0x21)
	{
	  pts = ((buffer[10] | (buffer[9] << 8) ) >> 1) | ((buffer[8] | (buffer[7] << 8) ) >> 1) << 15;
	  //log_debug("MPG1 (%u - 0x%02X)PTS is 0x%08X\n", current->id,buffer[3], pts);
	};
      }
#endif
      return (buffer[4] << 8) + buffer[5] + 6;
    case 0xBB:	/* System header */
    case 0xBE:	/* Padding stream */
    case 0xBF:	/* Private Stream 2 */
      return (buffer[4] << 8) + buffer[5] + 6;
    case 0:
      return 0;
    default:
#ifdef DEBUG_MPG
      log_info("I dont know how to handle 0x%02X\n", buffer[3]);
#endif
      return 0;
  }
}

static data_check_t data_check_mpg(const unsigned char *buffer, const unsigned int buffer_size, file_recovery_t *file_recovery)
{
  while(file_recovery->calculated_file_size + buffer_size/2  >= file_recovery->file_size &&
      file_recovery->calculated_file_size + 14 < file_recovery->file_size + buffer_size/2)
  {
    const unsigned int i=file_recovery->calculated_file_size - file_recovery->file_size + buffer_size/2;
    const unsigned int ret=calculate_packet_size(&buffer[i]);
#ifdef DEBUG_MPG
    log_info("data_check_mpg %llu 0x%02x %u\n", (long long unsigned)file_recovery->calculated_file_size, buffer[i+3], ret);
#endif
    if(ret==0)
      return DC_STOP;
    file_recovery->calculated_file_size+=ret;
  }
  return DC_CONTINUE;
}

static int header_mpg_found(file_recovery_t *file_recovery_new)
{
  reset_file_recovery(file_recovery_new);
  file_recovery_new->extension=file_hint_mpg.extension;
  if(file_recovery_new->blocksize < 14)
    return 1;
  file_recovery_new->data_check=&data_check_mpg;
  file_recovery_new->file_check=&file_check_size;
  return 1;
}

static int header_check_mpg_Pack(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new)
{
  unsigned int i=0;
  while(i<buffer_size && i+14<512)
  {
    const unsigned int ret=calculate_packet_size(&buffer[i]);
    if(ret==0)
      return 0;
    i+=ret;
  }
  /* MPEG-1 http://andrewduncan.ws/MPEG/MPEG-1.ps */
  /* pack start code 0x1BA + MPEG-1 + SCR=0 */
  if((buffer[4]&0xF1)==0x21 && (buffer[6]&1)==1 && (buffer[8]&1)==1 &&
      (buffer[9]&0x80)==0x80 && (buffer[11]&1)==1)
  {
    if(buffer[5]==0 && buffer[6]==1 && buffer[7]==0 && buffer[8]==1)
    {
      return header_mpg_found(file_recovery_new);
    }
    if(file_recovery->file_stat!=NULL && file_recovery->file_stat->file_hint==&file_hint_mpg)
    {
      header_ignored(file_recovery_new);
      return 0;
    }
    return header_mpg_found(file_recovery_new);
  }
  /* MPEG-2 Program stream http://neuron2.net/library/mpeg2/iso13818-1.pdf */
  /* MPEG2 system header start code, several per file */
  if((buffer[4]&0xc4)==0x44 && (buffer[6]&4)==4 && (buffer[8]&4)==4 && (buffer[9]&1)==1 && (buffer[12]&3)==3)
  {
    /*
     *       '01'                                            2	01
     system_clock_reference_base [32..30]            3	00 0
     marker_bit                                      1	1
     system_clock_reference_base [29..15]           15	00		buffer[4]=0x44
     0000 0000	buffer[5]=0x00
     0000 0
     marker_bit                                      1	1
     system_clock_reference_base [14..0]            15	00		buffer[6]=0x04

     0000 0000	buffer[7]=0x00
     0000
     0
     marker_bit                                      1	1
     system_clock_reference_extension                9 uimsbf
     marker_bit                                      1
     => 0100 0100
     */

    if(buffer[4]==0x44 && buffer[5]==0 && buffer[6]==4 && buffer[7]==0 && (buffer[8]&0xfc)==4)
    { /* SCR=0 */
      return header_mpg_found(file_recovery_new);
    }
    if(file_recovery->file_stat!=NULL && file_recovery->file_stat->file_hint==&file_hint_mpg)
    {
      header_ignored(file_recovery_new);
      return 0;
    }
    return header_mpg_found(file_recovery_new);
  }
  return 0;
}

static int header_check_mpg_System(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new)
{
  /* MPEG-1 http://andrewduncan.ws/MPEG/MPEG-1.ps */
  /*   ISO/IEC INTERNATIONAL 13818-1 STANDARD
  	system_header_start_code           32
	header_length                      16
	marker_bit                          1
	rate_bound                         22
	marker_bit                          1
	audio_bound                         6
	fixed_flag                          1
	CSPS_flag                           1
	system_audio_lock_flag              1
	system_video_lock_flag              1
	marker_bit                          1
	video_bound                         5
	packet_rate_restriction_flag        1
	reserved_bits                       7
  */

  /* MPEG-1 system header start code */
  if((buffer[6]&0x80)==0x80 && (buffer[8]&0x01)==0x01 && buffer[11]==0xff)
  {
    unsigned int i=0;
    while(i<buffer_size && i+14<512)
    {
      const unsigned int ret=calculate_packet_size(&buffer[i]);
      if(ret==0)
	return 0;
      i+=ret;
    }
    if(file_recovery->file_stat!=NULL && file_recovery->file_stat->file_hint==&file_hint_mpg)
    {
      header_ignored(file_recovery_new);
      return 0;
    }
    return header_mpg_found(file_recovery_new);
  }
  return 0;
}

static int header_check_mpg_Sequence(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new)
{
  /* MPEG-1 sequence header code 0x1B3 */
  /* horizontal size>0 */
  if((buffer[4]<<4)+(buffer[5]>>4)>0 &&
      /* vertical size>0 */
      ((buffer[5]&0x0f)<<8)+buffer[6]>0 &&
      /* aspect_ratio */
      (buffer[7]>>4)!=0 && (buffer[7]>>4)!=15 &&
      /* picture rate*/
      (buffer[7]&0x0f)!=0 && (buffer[7]&0xf)!=15 &&
      /* bit rate */
      (buffer[8]!=0 || buffer[9]!=0 || (buffer[10]&0xc0)!=0) &&
      /* marker */
      (buffer[10]&0x20)==0x20)
  {
    unsigned int i=0;
    while(i<buffer_size && i+14<512)
    {
      const unsigned int ret=calculate_packet_size(&buffer[i]);
      if(ret==0)
	return 0;
      i+=ret;
    }
    if(file_recovery->file_stat!=NULL && file_recovery->file_stat->file_hint==&file_hint_mpg)
    {
      header_ignored(file_recovery_new);
      return 0;
    }
    return header_mpg_found(file_recovery_new);
  }
  return 0;
}

static int header_check_mpg4_ElemVideo(const unsigned char *buffer, const unsigned int buffer_size, const unsigned int safe_header_only, const file_recovery_t *file_recovery, file_recovery_t *file_recovery_new)
{
  /* ISO/IEC 14496-2 (MPEG-4 video) ELEMENTARY VIDEO HEADER - visual object start code */
  /* is_visual_object_identifier */
  if((buffer[4]&0xf0)==0x80 &&
      /* visual_object_verid */
      (((buffer[4]>>3)&0x0f)==1 || ((buffer[4]>>3)&0x0f)==2) &&
      /* visual_object_priority */
      (buffer[4]&0x7)!=0 &&
      /* visual_object_type */
      (buffer[5]>>4)!=0 && (buffer[5]>>4)!=0x0f
    )
  {
    unsigned int i=0;
    while(i<buffer_size && i+14<512)
    {
      const unsigned int ret=calculate_packet_size(&buffer[i]);
      if(ret==0)
	return 0;
      i+=ret;
    }
    if(file_recovery->file_stat!=NULL && file_recovery->file_stat->file_hint==&file_hint_mpg)
    {
      header_ignored(file_recovery_new);
      return 0;
    }
    return header_mpg_found(file_recovery_new);
  }
  return 0;
}

static void register_header_check_mpg(file_stat_t *file_stat)
{
  static const unsigned char mpg_header_B3[4]= {0x00, 0x00, 0x01, 0xB3};
  static const unsigned char mpg_header_B5[4]= {0x00, 0x00, 0x01, 0xB5};
  static const unsigned char mpg_header_BA[4]= {0x00, 0x00, 0x01, 0xBA};
  static const unsigned char mpg_header_BB[4]= {0x00, 0x00, 0x01, 0xBB};
  register_header_check(0, mpg_header_B3,sizeof(mpg_header_B3), &header_check_mpg_Sequence, file_stat);
  register_header_check(0, mpg_header_B5,sizeof(mpg_header_B5), &header_check_mpg4_ElemVideo, file_stat);
  register_header_check(0, mpg_header_BA,sizeof(mpg_header_BA), &header_check_mpg_Pack, file_stat);
  register_header_check(0, mpg_header_BB,sizeof(mpg_header_BB), &header_check_mpg_System, file_stat);
}